Package arc.math.geom

Class Intersector

java.lang.Object
arc.math.geom.Intersector

public final class Intersector extends Object
Class offering various static methods for intersection testing between different geometric objects.
  • Constructor Details

    • Intersector

      public Intersector()
  • Method Details

    • intersectPolygons

      public static boolean intersectPolygons(float[] p1, float[] p2)
    • isInTriangle

      public static boolean isInTriangle(Vec3 point, Vec3 t1, Vec3 t2, Vec3 t3)
      Returns whether the given point is inside the triangle. This assumes that the point is on the plane of the triangle. No check is performed that this is the case.
      Parameters:
      point - the point
      t1 - the first vertex of the triangle
      t2 - the second vertex of the triangle
      t3 - the third vertex of the triangle
      Returns:
      whether the point is in the triangle
    • isInsideHexagon

      public static boolean isInsideHexagon(float cx, float cy, float d, float x, float y)
      Returns:
      whether x,y is inside the hexagon with radius d centered at cx, cy.
    • isInRegularPolygon

      public static boolean isInRegularPolygon(int sides, float cx, float cy, float radius, float rotation, float x, float y)
      Returns:
      whether the specified x,y is inside a regular polygon.
    • isInTriangle

      public static boolean isInTriangle(Vec2 p, Vec2 a, Vec2 b, Vec2 c)
      Returns true if the given point is inside the triangle.
    • isInTriangle

      public static boolean isInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
      Returns true if the given point is inside the triangle.
    • pointLineSide

      public static int pointLineSide(Vec2 linePoint1, Vec2 linePoint2, Vec2 point)
      Determines on which side of the given line the point is. Returns -1 if the point is on the left side of the line, 0 if the point is on the line and 1 if the point is on the right side of the line. Left and right are relative to the lines direction which is linePoint1 to linePoint2.
    • pointLineSide

      public static int pointLineSide(float linePoint1X, float linePoint1Y, float linePoint2X, float linePoint2Y, float pointX, float pointY)
    • isInPolygon

      public static boolean isInPolygon(Seq<Vec2> polygon, Vec2 point)
      Checks whether the given point is in the polygon.
      Parameters:
      polygon - The polygon vertices passed as an array
      point - The point
      Returns:
      true if the point is in the polygon
    • isInPolygon

      public static boolean isInPolygon(float[] polygon, int offset, int count, float x, float y)
      Returns true if the specified point is in the polygon.
      Parameters:
      offset - Starting polygon index.
      count - Number of array indices to use after offset.
    • intersectPolygons

      public static boolean intersectPolygons(Polygon p1, Polygon p2, Polygon overlap)
      Intersects two convex polygons with clockwise vertices and sets the overlap polygon resulting from the intersection. Follows the Sutherland-Hodgman algorithm.
      Parameters:
      p1 - The polygon that is being clipped
      p2 - The clip polygon
      overlap - The intersection of the two polygons (can be null, if an intersection polygon is not needed)
      Returns:
      Whether the two polygons intersect.
    • distanceLinePoint

      public static float distanceLinePoint(Vec2 start, Vec2 end, Vec2 point)
      Returns the distance between the given line and point. Note the specified line is not a line segment.
    • distanceLinePoint

      public static float distanceLinePoint(float startX, float startY, float endX, float endY, float pointX, float pointY)
      Returns the distance between the given line and point. Note the specified line is not a line segment.
    • distanceSegmentPoint

      public static float distanceSegmentPoint(float startX, float startY, float endX, float endY, float pointX, float pointY)
      Returns the distance between the given segment and point.
    • distanceSegmentPoint

      public static float distanceSegmentPoint(Vec2 start, Vec2 end, Vec2 point)
      Returns the distance between the given segment and point.
    • nearestSegmentPoint

      public static Vec2 nearestSegmentPoint(Vec2 start, Vec2 end, Vec2 point, Vec2 nearest)
      Returns a point on the segment nearest to the specified point.
    • nearestSegmentPoint

      public static Vec2 nearestSegmentPoint(float startX, float startY, float endX, float endY, float pointX, float pointY, Vec2 nearest)
      Returns a point on the segment nearest to the specified point.
    • intersectSegmentCircle

      public static boolean intersectSegmentCircle(Vec2 start, Vec2 end, Vec2 center, float squareRadius)
      Returns whether the given line segment intersects the given circle.
      Parameters:
      start - The start point of the line segment
      end - The end point of the line segment
      center - The center of the circle
      squareRadius - The squared radius of the circle
      Returns:
      Whether the line segment and the circle intersect
    • intersectSegmentCircleDisplace

      public static float intersectSegmentCircleDisplace(Vec2 start, Vec2 end, Vec2 point, float radius, Vec2 displacement)
      Checks whether the line segment and the circle intersect and returns by how much and in what direction the line has to move away from the circle to not intersect.
      Parameters:
      start - The line segment starting point
      end - The line segment end point
      point - The center of the circle
      radius - The radius of the circle
      displacement - The displacement vector set by the method having unit length
      Returns:
      The displacement or Float.POSITIVE_INFINITY if no intersection is present
    • intersectRayRay

      public static float intersectRayRay(Vec2 start1, Vec2 direction1, Vec2 start2, Vec2 direction2)
      Intersect two 2D Rays and return the scalar parameter of the first ray at the intersection point. You can get the intersection point by: Vec2 point(direction1).scl(scalar).add(start1); For more information, check: http://stackoverflow.com/a/565282/1091440
      Parameters:
      start1 - Where the first ray start
      direction1 - The direction the first ray is pointing
      start2 - Where the second ray start
      direction2 - The direction the second ray is pointing
      Returns:
      scalar parameter on the first ray describing the point where the intersection happens. May be negative. In case the rays are collinear, Float.POSITIVE_INFINITY will be returned.
    • intersectLines

      public static boolean intersectLines(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4, Vec2 intersection)
      Intersects the two lines and returns the intersection point in intersection.
      Parameters:
      p1 - The first point of the first line
      p2 - The second point of the first line
      p3 - The first point of the second line
      p4 - The second point of the second line
      intersection - The intersection point. May be null.
      Returns:
      Whether the two lines intersect
    • intersectLines

      public static boolean intersectLines(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Vec2 intersection)
      Intersects the two lines and returns the intersection point in intersection.
      Parameters:
      intersection - The intersection point, or null.
      Returns:
      Whether the two lines intersect
    • intersectLinePolygon

      public static boolean intersectLinePolygon(Vec2 p1, Vec2 p2, Polygon polygon)
      Check whether the given line and Polygon intersect.
      Parameters:
      p1 - The first point of the line
      p2 - The second point of the line
      polygon - The polygon
      Returns:
      Whether polygon and line intersects
    • intersectRectangles

      public static boolean intersectRectangles(Rect rect1, Rect rect2, Rect intersection)
      Determines whether the given rectangles intersect and, if they do, sets the supplied intersection rectangle to the area of overlap.
      Returns:
      Whether the rectangles intersect
    • intersectSegmentRectangle

      public static boolean intersectSegmentRectangle(float startX, float startY, float endX, float endY, Rect rect)
      Determines whether the given rectangle and segment intersect
      Parameters:
      startX - x-coordinate start of line segment
      startY - y-coordinate start of line segment
      endX - y-coordinate end of line segment
      endY - y-coordinate end of line segment
      rect - rectangle that is being tested for collision
      Returns:
      whether the rectangle intersects with the line segment
    • intersectSegmentRectangle

      public static boolean intersectSegmentRectangle(Vec2 start, Vec2 end, Rect rect)
    • intersectSegmentPolygon

      public static boolean intersectSegmentPolygon(Vec2 p1, Vec2 p2, Polygon polygon)
      Check whether the given line segment and Polygon intersect.
      Parameters:
      p1 - The first point of the segment
      p2 - The second point of the segment
      Returns:
      Whether polygon and segment intersect
    • intersectSegments

      public static boolean intersectSegments(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4, Vec2 intersection)
      Intersects the two line segments and returns the intersection point in intersection.
      Parameters:
      p1 - The first point of the first line segment
      p2 - The second point of the first line segment
      p3 - The first point of the second line segment
      p4 - The second point of the second line segment
      intersection - The intersection point. May be null.
      Returns:
      Whether the two line segments intersect
    • intersectSegments

      public static boolean intersectSegments(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Vec2 intersection)
      Parameters:
      intersection - May be null.
    • overlapsRect

      public static boolean overlapsRect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2)
    • overlaps

      public static boolean overlaps(Circle c1, Circle c2)
    • overlaps

      public static boolean overlaps(Rect r1, Rect r2)
    • overlaps

      public static boolean overlaps(Circle c, Rect r)
    • overlapConvexPolygons

      public static boolean overlapConvexPolygons(Polygon p1, Polygon p2)
      Check whether specified counter-clockwise wound convex polygons overlap.
      Parameters:
      p1 - The first polygon.
      p2 - The second polygon.
      Returns:
      Whether polygons overlap.
    • overlapConvexPolygons

      public static boolean overlapConvexPolygons(Polygon p1, Polygon p2, Intersector.MinimumTranslationVector mtv)
      Check whether specified counter-clockwise wound convex polygons overlap. If they do, optionally obtain a Minimum Translation Vector indicating the minimum magnitude vector required to push the polygon p1 out of collision with polygon p2.
      Parameters:
      p1 - The first polygon.
      p2 - The second polygon.
      mtv - A Minimum Translation Vector to fill in the case of a collision, or null (optional).
      Returns:
      Whether polygons overlap.
    • overlapConvexPolygons

      public static boolean overlapConvexPolygons(float[] verts1, float[] verts2, Intersector.MinimumTranslationVector mtv)
      See Also:
    • overlapConvexPolygons

      public static boolean overlapConvexPolygons(float[] verts1, int offset1, int count1, float[] verts2, int offset2, int count2, Intersector.MinimumTranslationVector mtv)
      Check whether polygons defined by the given counter-clockwise wound vertex arrays overlap. If they do, optionally obtain a Minimum Translation Vector indicating the minimum magnitude vector required to push the polygon defined by verts1 out of the collision with the polygon defined by verts2.
      Parameters:
      verts1 - Vertices of the first polygon.
      verts2 - Vertices of the second polygon.
      mtv - A Minimum Translation Vector to fill in the case of a collision, or null (optional).
      Returns:
      Whether polygons overlap.