Package arc.math.geom
Class Intersector
java.lang.Object
arc.math.geom.Intersector
Class offering various static methods for intersection testing between different geometric objects.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Minimum translation required to separate two polygons. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic float
distanceLinePoint
(float startX, float startY, float endX, float endY, float pointX, float pointY) Returns the distance between the given line and point.static float
distanceLinePoint
(Vec2 start, Vec2 end, Vec2 point) Returns the distance between the given line and point.static float
distanceSegmentPoint
(float startX, float startY, float endX, float endY, float pointX, float pointY) Returns the distance between the given segment and point.static float
distanceSegmentPoint
(Vec2 start, Vec2 end, Vec2 point) Returns the distance between the given segment and point.static boolean
intersectLinePolygon
(Vec2 p1, Vec2 p2, Polygon polygon) Check whether the given line andPolygon
intersect.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.static boolean
Intersects the two lines and returns the intersection point in intersection.static boolean
intersectPolygons
(float[] p1, float[] p2) 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.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.static boolean
intersectRectangles
(Rect rect1, Rect rect2, Rect intersection) Determines whether the given rectangles intersect and, if they do, sets the suppliedintersection
rectangle to the area of overlap.static boolean
intersectSegmentCircle
(Vec2 start, Vec2 end, Vec2 center, float squareRadius) Returns whether the given line segment intersects the given circle.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.static boolean
intersectSegmentPolygon
(Vec2 p1, Vec2 p2, Polygon polygon) Check whether the given line segment andPolygon
intersect.static boolean
intersectSegmentRectangle
(float startX, float startY, float endX, float endY, Rect rect) Determines whether the given rectangle and segment intersectstatic boolean
intersectSegmentRectangle
(Vec2 start, Vec2 end, Rect rect) static boolean
intersectSegments
(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Vec2 intersection) static boolean
Intersects the two line segments and returns the intersection point in intersection.static boolean
isInPolygon
(float[] polygon, int offset, int count, float x, float y) Returns true if the specified point is in the polygon.static boolean
isInPolygon
(Seq<Vec2> polygon, Vec2 point) Checks whether the given point is in the polygon.static boolean
isInRegularPolygon
(int sides, float cx, float cy, float radius, float rotation, float x, float y) static boolean
isInsideHexagon
(float cx, float cy, float d, float x, float y) 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.static boolean
isInTriangle
(Vec2 p, Vec2 a, Vec2 b, Vec2 c) Returns true if the given point is inside the triangle.static boolean
isInTriangle
(Vec3 point, Vec3 t1, Vec3 t2, Vec3 t3) Returns whether the given point is inside the triangle.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.static Vec2
nearestSegmentPoint
(Vec2 start, Vec2 end, Vec2 point, Vec2 nearest) Returns a point on the segment nearest to the specified point.static boolean
overlapConvexPolygons
(float[] verts1, float[] verts2, Intersector.MinimumTranslationVector mtv) 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.static boolean
overlapConvexPolygons
(Polygon p1, Polygon p2) Check whether specified counter-clockwise wound convex polygons overlap.static boolean
Check whether specified counter-clockwise wound convex polygons overlap.static boolean
static boolean
static boolean
static boolean
overlapsRect
(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) static int
pointLineSide
(float linePoint1X, float linePoint1Y, float linePoint2X, float linePoint2Y, float pointX, float pointY) static int
pointLineSide
(Vec2 linePoint1, Vec2 linePoint2, Vec2 point) Determines on which side of the given line the point is.
-
Constructor Details
-
Intersector
public Intersector()
-
-
Method Details
-
intersectPolygons
public static boolean intersectPolygons(float[] p1, float[] p2) -
isInTriangle
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 pointt1
- the first vertex of the trianglet2
- the second vertex of the trianglet3
- 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
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
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
Checks whether the given point is in the polygon.- Parameters:
polygon
- The polygon vertices passed as an arraypoint
- 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
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 clippedp2
- The clip polygonoverlap
- The intersection of the two polygons (can be null, if an intersection polygon is not needed)- Returns:
- Whether the two polygons intersect.
-
distanceLinePoint
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
Returns the distance between the given segment and point. -
nearestSegmentPoint
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
Returns whether the given line segment intersects the given circle.- Parameters:
start
- The start point of the line segmentend
- The end point of the line segmentcenter
- The center of the circlesquareRadius
- 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 pointend
- The line segment end pointpoint
- The center of the circleradius
- The radius of the circledisplacement
- The displacement vector set by the method having unit length- Returns:
- The displacement or Float.POSITIVE_INFINITY if no intersection is present
-
intersectRayRay
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 startdirection1
- The direction the first ray is pointingstart2
- Where the second ray startdirection2
- 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
Intersects the two lines and returns the intersection point in intersection.- Parameters:
p1
- The first point of the first linep2
- The second point of the first linep3
- The first point of the second linep4
- The second point of the second lineintersection
- 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
Check whether the given line andPolygon
intersect.- Parameters:
p1
- The first point of the linep2
- The second point of the linepolygon
- The polygon- Returns:
- Whether polygon and line intersects
-
intersectRectangles
Determines whether the given rectangles intersect and, if they do, sets the suppliedintersection
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 segmentstartY
- y-coordinate start of line segmentendX
- y-coordinate end of line segmentendY
- y-coordinate end of line segmentrect
- rectangle that is being tested for collision- Returns:
- whether the rectangle intersects with the line segment
-
intersectSegmentRectangle
-
intersectSegmentPolygon
Check whether the given line segment andPolygon
intersect.- Parameters:
p1
- The first point of the segmentp2
- The second point of the segment- Returns:
- Whether polygon and segment intersect
-
intersectSegments
Intersects the two line segments and returns the intersection point in intersection.- Parameters:
p1
- The first point of the first line segmentp2
- The second point of the first line segmentp3
- The first point of the second line segmentp4
- The second point of the second line segmentintersection
- 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
-
overlaps
-
overlaps
-
overlapConvexPolygons
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) -
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.
-