libgdx Vector2 分析

Vector2 为二维坐标,属性便是
/** the x-component of this vector **/
public float x;
/** the y-component of this vector **/
public float y;

x,y 两个属性。

初始化接口:

public Vector2 () 
Vector2 (float x, float y)
Vector2 (Vector2 v)

下来继续来说下接口:
Vector2 cpy ();
构造一个新的Vector2,使用当前的Vector2。
float len () 
求出x,y 到0,0的距离。
float len2 ()
距离的平方。
Vector2 set (Vector2 v)
Vector2 set (float x, float y)
设置x y变量的值
Vector2 sub (Vector2 v)
Vector2 sub (float x, float y)
当前x等于x-v.x y同理
Vector2 nor () 
x,y 与斜边的比率。比如3,4这个点,则x=3/5 y=4/5
Vector2 add (Vector2 v) 
Vector2 add (float x, float y)


float dot (Vector2 v)
x*v.x y*v.y
Vector2 scl (float scalar)
x*scalar y*scalar
scl (float x, float y)
scl (Vector2 v)
dst (Vector2 v)
dst (float x, float y)
两点直接距离
dst2 (Vector2 v)
dst2 (float x, float y) 
距离平方
Vector2 limit (float limit)
这个则是将当前的长度直接限制在limit里面
比如当前值为x=6,y=8,这时我们计算的长度为10
我们使用5调用这个接口后x=6/10*5=3,y=8/10*5=4;
clamp (float min, float max)
这个便是设定一个范围,如果小于min,则长度变为min,大于max则变为max。
angle
getAngleRad
setAngle
setAngleRad
rotate

rotateRad

完整的接口文件为:

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.math;

/** Encapsulates a general vector. Allows chaining operations by returning a reference to itself in all modification methods. See
 * {@link Vector2} and {@link Vector3} for specific implementations.
 * @author Xoppa */
public interface Vector<T extends Vector<T>> {
	/** @return a copy of this vector */
	T cpy ();

	/** @return The euclidean length */
	float len ();

	/** This method is faster than {@link Vector#len()} because it avoids calculating a square root. It is useful for comparisons,
	 * but not for getting accurate lengths, as the return value is the square of the actual length.
	 * @return The squared euclidean length */
	float len2 ();

	/** Limits this vector's length to given value
	 * @return This vector for chaining */
	T limit (float limit);

	/** Clamps this vector's length to given min and max values
	 * @param min Min length
	 * @param max Max length
	 * @return This vector for chaining */
	T clamp (float min, float max);

	/** Sets this vector from the given vector
	 * @param v The vector
	 * @return This vector for chaining */
	T set (T v);

	/** Subtracts the given vector from this vector.
	 * @param v The vector
	 * @return This vector for chaining */
	T sub (T v);

	/** Normalizes this vector. Does nothing if it is zero.
	 * @return This vector for chaining */
	T nor ();

	/** Adds the given vector to this vector
	 * @param v The vector
	 * @return This vector for chaining */
	T add (T v);

	/** @param v The other vector
	 * @return The dot product between this and the other vector */
	float dot (T v);

	/** Scales this vector by a scalar
	 * @param scalar The scalar
	 * @return This vector for chaining */
	T scl (float scalar);

	/** Scales this vector by another vector
	 * @return This vector for chaining */
	T scl (T v);

	/** @param v The other vector
	 * @return the distance between this and the other vector */
	float dst (T v);

	/** This method is faster than {@link Vector#dst(Vector)} because it avoids calculating a square root. It is useful for
	 * comparisons, but not for getting accurate distances, as the return value is the square of the actual distance.
	 * @param v The other vector
	 * @return the squared distance between this and the other vector */
	float dst2 (T v);

	/** Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored
	 * in this vector.
	 * @param target The target vector
	 * @param alpha The interpolation coefficient
	 * @return This vector for chaining. */
	T lerp (T target, float alpha);

	/** @return Whether this vector is a unit length vector */
	public boolean isUnit ();

	/** @return Whether this vector is a unit length vector within the given margin. */
	public boolean isUnit (final float margin);

	/** @return Whether this vector is a zero vector */
	public boolean isZero ();

	/** @return Whether the length of this vector is smaller than the given margin */
	public boolean isZero (final float margin);

	/** @return Whether this vector is collinear with the given vector. The vectors need to be normalized for this to work. True if
	 *         the normalized dot product is 1.
	 * @param vector the vector to check
	 * @param epsilon a positive small number close to zero */
	public boolean isCollinear (T vector, float epsilon);

	/** @return Whether this vector is collinear with the given vector. The vectors need to be normalized for this to work. True if
	 *         the normalized dot product is 1.
	 * @param vector the vector to check */
	public boolean isCollinear (T vector);

	/** @return Whether this vector is collinear with the given vector but has opposite direction. True if the normalized dot product
	 *         is -1. The vectors need to be normalized for this to work.
	 * @param vector the vector to check
	 * @param epsilon a positive small number close to zero */
	public boolean isCollinearOpposite (T vector, float epsilon);

	/** @return Whether this vector is collinear with the given vector but has opposite direction. True if the normalized dot product
	 *         is -1. The vectors need to be normalized for this to work.
	 * @param vector the vector to check */
	public boolean isCollinearOpposite (T vector);

	/** @return Whether this vector is perpendicular with the given vector. True if the dot product is 0. */
	public boolean isPerpendicular (T vector);

	/** @return Whether this vector is perpendicular with the given vector. True if the dot product is 0.
	 * @param epsilon a positive small number close to zero */
	public boolean isPerpendicular (T vector, float epsilon);

	/** @return Whether this vector has similar direction compared to the given vector. True if the normalized dot product is > 0. */
	public boolean hasSameDirection (T vector);

	/** @return Whether this vector has opposite direction compared to the given vector. True if the normalized dot product is < 0. */
	public boolean hasOppositeDirection (T vector);

	/** Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
	 * @param other
	 * @param epsilon
	 * @return whether the vectors have fuzzy equality. */
	public boolean epsilonEquals (T other, float epsilon);

	/** First scale a supplied vector, then add it to this vector.
	 * @param v addition vector
	 * @param scalar for scaling the addition vector */
	public T mulAdd(T v, float scalar);

	/** First scale a supplied vector, then add it to this vector.
	 * @param v addition vector
	 * @param mulVec vector by whose values the addition vector will be scaled*/
	public T mulAdd(T v, T mulVec);

	// TODO: T crs(T v);
}


你可能感兴趣的:(源码分析,libgdx)