Package me.nabdev.pathfinding.structures
Class Vector
java.lang.Object
me.nabdev.pathfinding.structures.Vector
Class to represent a two dimensional vector
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionVector
(double _x, double _y) Creates a new vector with the given x and y components -
Method Summary
Modifier and TypeMethodDescriptionAdd two vectorsCalculate the average of this vector and another vectorCalculate the normal (perpendicular) of this vector (-y, x)static Vector
calculateNormalWithRespectToShape
(Vertex shapeCenter, Vertex v1, Vertex v2) Calculate the normal, but invert if the normal is not facing away from the shape centerstatic Vertex
calculateRayIntersection
(Vertex P1, Vector V1, Vertex P2, Vector V2) Calculate the intersection of two rays, given their starting points and directions.double
crossProduct
(Vector v2) Calculate the cross product of this vector and another vectorstatic boolean
dotIntersect
(Vertex d1, Vertex d2, Vertex c1, Vertex c2) Deprecated.This method is left here to clarify how dotIntersectFast works, but it creates a lot of garbage and is slow.static boolean
dotIntersectFast
(Vertex d1, Vertex d2, Vertex c1, Vertex c2) Whether or not the line segment from d1 to d2 intersects the line segment from c1 to c2double
dotProduct
(Vector v2) Calculate the dot product of this vector and another vectorensureNotNaN
(String warning) Check if any component of this vector is NaNdouble
Calculate the magnitude of this vectorNormalize this vectorrotate
(double angle) Rotate this vector by a given anglescale
(double factor) Scale this vector by a factor (To set a specific magnitude, use normalize() first)Subtract two vectorstoString()
Get a string representation of this vectorboolean
zero()
Check if this vector has zero x and y components
-
Field Details
-
x
public double xThe x component of the vector -
y
public double yThe y component of the vector
-
-
Constructor Details
-
Vector
public Vector(double _x, double _y) Creates a new vector with the given x and y components- Parameters:
_x
- The x component of the vector_y
- The y component of the vector
-
-
Method Details
-
calculateRayIntersection
Calculate the intersection of two rays, given their starting points and directions.- Parameters:
P1
- The starting point of the first rayV1
- The direction of the first rayP2
- The starting point of the second rayV2
- The direction of the second ray- Returns:
- The intersection point of the two rays, or null if they are parallel or coincident
-
subtract
Subtract two vectors- Parameters:
v2
- The vector to subtract from this vector- Returns:
- The result of subtracting v2 from this vector
-
add
Add two vectors- Parameters:
v2
- The vector to add to this vector- Returns:
- The result of adding v2 to this vector
-
normalize
Normalize this vector- Returns:
- The normalized vector
-
magnitude
public double magnitude()Calculate the magnitude of this vector- Returns:
- The magnitude of this vector
-
scale
Scale this vector by a factor (To set a specific magnitude, use normalize() first)- Parameters:
factor
- The factor to scale this vector by- Returns:
- The scaled vector
-
calculateNormal
Calculate the normal (perpendicular) of this vector (-y, x)- Returns:
- The normal of this vector
-
calculateNormalWithRespectToShape
Calculate the normal, but invert if the normal is not facing away from the shape center- Parameters:
shapeCenter
- The center of the shapev1
- The first vertex of the vector to calculate the normal ofv2
- The second vertex of the vector to calculate the normal of- Returns:
- The normal of the vector
-
dotProduct
Calculate the dot product of this vector and another vector- Parameters:
v2
- The other vector- Returns:
- The dot product of this vector and v2
-
crossProduct
Calculate the cross product of this vector and another vector- Parameters:
v2
- The other vector- Returns:
- The cross product of this vector and v2
-
dotIntersectFast
Whether or not the line segment from d1 to d2 intersects the line segment from c1 to c2- Parameters:
d1
- The first vertex of the first line segmentd2
- The second vertex of the first line segmentc1
- The first vertex of the second line segmentc2
- The second vertex of the second line segment- Returns:
- True if the line segments intersect, false otherwise
-
dotIntersect
Deprecated.This method is left here to clarify how dotIntersectFast works, but it creates a lot of garbage and is slow. UsedotIntersectFast(me.nabdev.pathfinding.structures.Vertex, me.nabdev.pathfinding.structures.Vertex, me.nabdev.pathfinding.structures.Vertex, me.nabdev.pathfinding.structures.Vertex)
instead.Whether or not the line segment from d1 to d2 intersects the line segment from c1 to c2- Parameters:
d1
- The first vertex of the first line segmentd2
- The second vertex of the first line segmentc1
- The first vertex of the second line segmentc2
- The second vertex of the second line segment- Returns:
- True if the line segments intersect, false otherwise
-
zero
public boolean zero()Check if this vector has zero x and y components- Returns:
- True if this vector has zero x and y components, false otherwise
-
ensureNotNaN
Check if any component of this vector is NaN- Parameters:
warning
- The warning to print if this vector is NaN- Returns:
- True if this vector is NaN, false otherwise
-
rotate
Rotate this vector by a given angle- Parameters:
angle
- The angle to rotate this vector by- Returns:
- The rotated vector
-
average
Calculate the average of this vector and another vector- Parameters:
vec2
- The other vector- Returns:
- The average of this vector and vec2
-
toString
Get a string representation of this vector
-