java.lang.Object
java.lang.Record
technology.sola.math.linear.Vector2D
- Record Components:
x
- the x coordinatey
- the y coordinate
The Vector2D class is an implementation of a linear algebra vector.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCalculates the sum of two vectors and returns the sum as a new vector object.float
Calculates the distance between two vectors.float
distanceSq
(Vector2D vector2D) Calculates the distance squared between two vectors.float
Calculates the dot product of two vectors.final boolean
Indicates whether some other object is "equal to" this one.final int
hashCode()
Returns a hash code value for this object.static Vector2D
headingVectorFromAngle
(double angle) Creates a vector from a heading angle.float
Calculates the magnitude of this vector (the distance from origin).float
Calculates the magnitude squared of this vector.Calculates the normalized vector (where the vector has a length of one).Calculates the reflected vector off of desiredVector2D
normal.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()
Returns a string representation of this record class.float
x()
Returns the value of thex
record component.float
y()
Returns the value of they
record component.
-
Field Details
-
ZERO_VECTOR
AVector2D
with 0 for the x and y.
-
-
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
-
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, not null- 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, not null- 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 desiredVector2D
normal.- Parameters:
normal
- the normal to reflect off of- Returns:
- the reflected vector
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='. -
x
public float x()Returns the value of thex
record component.- Returns:
- the value of the
x
record component
-
y
public float y()Returns the value of they
record component.- Returns:
- the value of the
y
record component
-