Class Vector2
java.lang.Object
org.openpatch.scratch.extensions.math.Vector2
The
Vector2 class represents a 2D vector with x and y coordinates. It provides various
methods for vector operations such as addition, subtraction, scalar multiplication, dot product,
rotation, and more.
Instances of this class are immutable.
Example usage:
Vector2 v1 = new Vector2(3, 4);
Vector2 v2 = new Vector2(1, 2);
Vector2 sum = v1.add(v2);
double length = v1.length();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds another vector and returns the resultdoubleangle()Returns the angle of the vector.clone()Creates a clone of the vectordoubleCalculates the distance to another vector.doubleCalculates the squared distance to another vector.doubleCalculates the dot product.booleanstatic Vector2fromPolar(double magnitude, double angle) Constructor method for constructing a vector based on polar coordinates.doublegetX()Returns the x coordinate of the vectordoublegetY()Returns the y coordinate of the vectorinthashCode()doublelength()Calculates the length of the vector.doublelengthSq()Calculates the squared length of the vector.multiply(double scalar) Multiplies a scalar and return the result.Returns the normal vector, which is perpendicular to the vector.reverse()Reverses a vector and returns the result.rotateBy(double angle) Rotates the vector by an angle and returns the result.rotateTo(double angle) Rotates the vector to a given angle and returns the result.Substracts another vector and returns the resulttoString()Returns the unit vector.
-
Constructor Details
-
Vector2
public Vector2()Constructor for the null vector. -
Vector2
public Vector2(double x, double y) Constructor for a 2D vector.- Parameters:
x- x-coordinatey- y-coordinate
-
Vector2
-
-
Method Details
-
fromPolar
Constructor method for constructing a vector based on polar coordinates.- Parameters:
magnitude- of the vectorangle- of the vector- Returns:
- a vector
-
length
public double length()Calculates the length of the vector.- Returns:
- the length of the vector
-
lengthSq
public double lengthSq()Calculates the squared length of the vector.- Returns:
- the squared length of the vector
-
distanceSq
Calculates the squared distance to another vector.- Parameters:
v- a vector- Returns:
- the squared distance
-
distance
Calculates the distance to another vector.- Parameters:
v- a vector- Returns:
- the distance
-
angle
public double angle()Returns the angle of the vector.- Returns:
- the angle
-
unitVector
-
normalVector
Returns the normal vector, which is perpendicular to the vector.- Returns:
- a normal vector
-
add
-
sub
-
multiply
Multiplies a scalar and return the result.- Parameters:
scalar- a scalar- Returns:
- the vector
-
dot
Calculates the dot product.- Parameters:
v- a vector- Returns:
- the dot product
-
rotateBy
Rotates the vector by an angle and returns the result.- Parameters:
angle- an angle between [0, ..., 360]- Returns:
- the rotated vector
-
rotateTo
Rotates the vector to a given angle and returns the result.- Parameters:
angle- an angle between [0,...,360]- Returns:
- the rotated vector
-
reverse
-
getX
public double getX()Returns the x coordinate of the vector- Returns:
- the x coordinate
-
getY
public double getY()Returns the y coordinate of the vector- Returns:
- the y coordinate
-
clone
-
equals
-
hashCode
-
toString
-