java.lang.Object
technology.sola.math.linear.Vector2D
The Vector2D class is an implementation of a linear algebra vector.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCalculates the sum of two vectors and returns the sum as a new vector object.floatCalculates the distance between two vectors.floatdistanceSq(Vector2D vector2D) Calculates the distance squared between two vectors.floatCalculates the dot product of two vectors.booleaninthashCode()static Vector2DheadingVectorFromAngle(double angle) Creates a vector from a heading angle.floatCalculates the magnitude of this vector (the distance from origin).floatCalculates the magnitude squared of this vector.voidmutateAdd(float x, float y) Adds the desired x and y values to this vector.voidAdds the desired vector to this vector.voidmutateScalar(float scalar) Multiplies this vector by the desired scalar.voidmutateSubtract(float x, float y) Subtracts the desired x and y values from this vector.voidmutateSubtract(Vector2D vector2D) Subtracts the desired vector from this vector.voidmutateX(float x) Sets the x coordinate of this vector.voidmutateY(float y) Sets the y coordinate of this vector.Calculates the normalized vector (where the vector has a length of one).Calculates the reflected vector off of desiredVector2Dnormal.rotate(double angle) Calculates the rotation for a vector about the origin.scalar(float scalar) Calculates the scalar of this vector and returns the result as a new vector object.Calculates the difference of two vectors and returns the sum as a new vector object.toString()floatx()floaty()static Vector2D
-
Constructor Details
-
Vector2D
public Vector2D(float x, float y) Create a Vector2D instance with x and y set.- Parameters:
x- the x coordinatey- the y coordinate
-
-
Method Details
-
headingVectorFromAngle
Creates a vector from a heading angle.- Parameters:
angle- angle in radians- Returns:
- the heading vector
-
zeroVector
- Returns:
- A
Vector2Dwith 0 for the x and y.
-
x
public float x()- Returns:
- the x coordinate
-
y
public float y()- Returns:
- the y coordinate
-
mutateX
public void mutateX(float x) Sets the x coordinate of this vector.- Parameters:
x- the new x coordinate
-
mutateY
public void mutateY(float y) Sets the y coordinate of this vector.- Parameters:
y- the new y coordinate
-
mutateAdd
Adds the desired vector to this vector.- Parameters:
vector2D- the vector to add to this vector
-
mutateAdd
public void mutateAdd(float x, float y) Adds the desired x and y values to this vector.- Parameters:
x- the x value to addy- the y value to add
-
mutateSubtract
Subtracts the desired vector from this vector.- Parameters:
vector2D- the vector to subtract from this vector
-
mutateSubtract
public void mutateSubtract(float x, float y) Subtracts the desired x and y values from this vector.- Parameters:
x- the x value to subtracty- the y value to subtract
-
mutateScalar
public void mutateScalar(float scalar) Multiplies this vector by the desired scalar.- Parameters:
scalar- the scalar to multiply this vector by
-
add
Calculates the sum of two vectors and returns the sum as a new vector object.- Parameters:
vector2D- the vector to add to this vector- Returns:
- a new vector with the result of this + vector2D
-
subtract
Calculates the difference of two vectors and returns the sum as a new vector object.- Parameters:
vector2D- the vector to subtract the value of- Returns:
- a new vector with the result of this - vector2D
-
scalar
Calculates the scalar of this vector and returns the result as a new vector object.- Parameters:
scalar- the scalar- Returns:
- a new vector with the result of scalar applied to this
-
magnitude
public float magnitude()Calculates the magnitude of this vector (the distance from origin).- Returns:
- the magnitude of the vector
-
magnitudeSq
public float magnitudeSq()Calculates the magnitude squared of this vector.- Returns:
- the magnitude squared of this vector
-
distance
Calculates the distance between two vectors.- Parameters:
vector2D- the vector to get the distance between- Returns:
- the distance between the vectors
-
distanceSq
Calculates the distance squared between two vectors. This is faster than calculating the distance since a sqrt call is not made.- Parameters:
vector2D- the vector to get the distance squared between- Returns:
- the distance squared between the vectors
-
normalize
Calculates the normalized vector (where the vector has a length of one).- Returns:
- the normalized vector as a new object
-
dot
Calculates the dot product of two vectors.- Parameters:
vector2D- the vector to calculate the dot product with- Returns:
- the calculated dot product as a new vector object
-
rotate
Calculates the rotation for a vector about the origin.- Parameters:
angle- the angle to rotate the vector in radians- Returns:
- the calculated rotation vector as a new vector object
-
reflect
Calculates the reflected vector off of desiredVector2Dnormal.- Parameters:
normal- the normal to reflect off of- Returns:
- the reflected vector
-
toString
-
hashCode
public int hashCode() -
equals
-