Class Vector

java.lang.Object
me.nabdev.pathfinding.structures.Vector

public class Vector extends Object
Class to represent a two dimensional vector
  • Field Details

    • x

      public double x
      The x component of the vector
    • y

      public double y
      The 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

      public static Vertex calculateRayIntersection(Vertex P1, Vector V1, Vertex P2, Vector V2)
      Calculate the intersection of two rays, given their starting points and directions.
      Parameters:
      P1 - The starting point of the first ray
      V1 - The direction of the first ray
      P2 - The starting point of the second ray
      V2 - The direction of the second ray
      Returns:
      The intersection point of the two rays, or null if they are parallel or coincident
    • subtract

      public Vector subtract(Vector v2)
      Subtract two vectors
      Parameters:
      v2 - The vector to subtract from this vector
      Returns:
      The result of subtracting v2 from this vector
    • add

      public Vector add(Vector v2)
      Add two vectors
      Parameters:
      v2 - The vector to add to this vector
      Returns:
      The result of adding v2 to this vector
    • normalize

      public 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

      public Vector scale(double factor)
      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

      public Vector calculateNormal()
      Calculate the normal (perpendicular) of this vector (-y, x)
      Returns:
      The normal of this vector
    • calculateNormalWithRespectToShape

      public static Vector calculateNormalWithRespectToShape(Vertex shapeCenter, Vertex v1, Vertex v2)
      Calculate the normal, but invert if the normal is not facing away from the shape center
      Parameters:
      shapeCenter - The center of the shape
      v1 - The first vertex of the vector to calculate the normal of
      v2 - The second vertex of the vector to calculate the normal of
      Returns:
      The normal of the vector
    • dotProduct

      public double dotProduct(Vector v2)
      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

      public double crossProduct(Vector v2)
      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

      public 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 c2
      Parameters:
      d1 - The first vertex of the first line segment
      d2 - The second vertex of the first line segment
      c1 - The first vertex of the second line segment
      c2 - The second vertex of the second line segment
      Returns:
      True if the line segments intersect, false otherwise
    • dotIntersect

      @Deprecated public static 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. Use dotIntersectFast(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 segment
      d2 - The second vertex of the first line segment
      c1 - The first vertex of the second line segment
      c2 - 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

      public Vector ensureNotNaN(String warning)
      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

      public Vector rotate(double angle)
      Rotate this vector by a given angle
      Parameters:
      angle - The angle to rotate this vector by
      Returns:
      The rotated vector
    • average

      public Vector average(Vector vec2)
      Calculate the average of this vector and another vector
      Parameters:
      vec2 - The other vector
      Returns:
      The average of this vector and vec2
    • toString

      public String toString()
      Get a string representation of this vector
      Overrides:
      toString in class Object
      Returns:
      "(x, y)"