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
-
Method Summary
Modifier and TypeMethodDescriptionAdds another vector and returns the resultdouble
angle()
Returns the angle of the vector.clone()
Creates a clone of the vectordouble
Calculates the distance to another vector.double
Calculates the squared distance to another vector.double
Calculates the dot product.boolean
static Vector2
fromPolar
(double magnitude, double angle) Constructor method for constructing a vector based on polar coordinates.double
getX()
Returns the x coordinate of the vectordouble
getY()
Returns the y coordinate of the vectorint
hashCode()
double
length()
Calculates the length of the vector.double
lengthSq()
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
Copy constructor for copying a vector.- Parameters:
v
- a vector
-
-
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
Returns the unit vector.- Returns:
- a unit vector
-
normalVector
Returns the normal vector, which is perpendicular to the vector.- Returns:
- a normal vector
-
add
Adds another vector and returns the result- Returns:
- the sum
-
sub
Substracts another vector and returns the result- Returns:
- the difference
-
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
Reverses a vector and returns the result.- Returns:
- the reversed vector
-
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
Creates a clone of the vector- Returns:
- the cloned vector
-
equals
-
hashCode
public int hashCode() -
toString
-