Class Vector2D
public class Vector2D
A simple mutable 2D vector with basic linear algebra operations (addition, subtraction, scaling, dot product, length and unit/normal vector).
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds another vector to this one and returns the resulting new vector.doubledotProduct(Vector2D v1) Computes the dot product of this vector with another.doublegetX()Returns the x component of the vector.doublegetY()Returns the y component of the vector.doublelength()Returns the Euclidean length of the vector.Returns a vector perpendicular to this one, obtained by rotating it 90 degrees clockwise.scale(double scaleFactor) Returns this vector scaled by the supplied factor.Subtracts another vector from this one and returns the resulting new vector.toString()Returns the unit vector pointing in the same direction as this vector, or the zero vector if this vector has length zero.
-
Field Details
-
dX
protected double dXThe x component of the vector. -
dY
protected double dYThe y component of the vector.
-
-
Constructor Details
-
Vector2D
public Vector2D()Creates a new zero vector. -
Vector2D
public Vector2D(double dX, double dY) Creates a new vector with the supplied components.- Parameters:
dX- the x componentdY- the y component
-
Vector2D
-
-
Method Details
-
add
-
dotProduct
Computes the dot product of this vector with another.- Parameters:
v1- the other vector- Returns:
- the dot product
-
getX
public double getX()Returns the x component of the vector.- Returns:
- the x component
-
getY
public double getY()Returns the y component of the vector.- Returns:
- the y component
-
length
public double length()Returns the Euclidean length of the vector.- Returns:
- the length
-
normalVector
Returns a vector perpendicular to this one, obtained by rotating it 90 degrees clockwise.- Returns:
- the perpendicular vector
-
scale
Returns this vector scaled by the supplied factor.- Parameters:
scaleFactor- the scale factor- Returns:
- the scaled vector
-
sub
-
toString
-
unitVector
Returns the unit vector pointing in the same direction as this vector, or the zero vector if this vector has length zero.- Returns:
- the unit vector
-