///Returns whether a line segment intersects a polygon (given a list of vertices) public static bool LineSegIntersectsPoly(LineSeg lns, List <Vector2> verts) { //Check to see if either end of the segment is in the polygon if (PointInPoly(lns.CenterA, verts) || PointInPoly(lns.CenterB, verts)) { return(true); } int j = verts.Count - 1; LineSeg lnsCheck = new LineSeg(); //checks every line segment in the polygon until an intersection is found for (int i = 0; i < verts.Count; i++) { lnsCheck.SetA(verts[i]); lnsCheck.SetB(verts[j]); if (LineSegIntersectsLineSeg(lns, lnsCheck)) { return(true); } j = i; } return(false); }
/* Poly methods */ #region ///Returns whether a polygon intersects another polygon (given a list of vertices) public static bool PolyIntersectsPoly(List <Vector2> verts1, List <Vector2> verts2) { //check to see if any vertices exist inside either polygon (more efficient and catches most cases) for (int i = 0; i < verts1.Count; i++) { Vector2 vert = verts1[i]; if (PointInPoly(vert, verts2)) { return(true); } } for (int i = 0; i < verts2.Count; i++) { Vector2 vert = verts2[i]; if (PointInPoly(vert, new List <Vector2>(verts1))) { return(true); } } //check to see if any of the edges of a polygon intersect with the edges of the other //this code ensures concave shapes work too but it's potentially slow LineSeg lnsCheck = new LineSeg(); int j = verts1.Count - 1; for (int i = 0; i < verts1.Count; i++) { lnsCheck.SetA(verts1[i]); lnsCheck.SetB(verts1[j]); if (LineSegIntersectsPoly(lnsCheck, verts2)) { return(true); } j = i; } return(false); }
///Returns whether a point lies on the perimeter of a polygon given its vertices public static bool PointOnPolyPerimeter(Vector2 p, List <Vector2> verts) { LineSeg lineCheck = new LineSeg(); int j = verts.Count - 1; //checks every line segment of the polygon to see if the point lies on any segment for (int i = 0; i < verts.Count; i++) { lineCheck.SetA(verts[i]); lineCheck.SetB(verts[j]); if (PointOnLineSeg(p, lineCheck)) { return(true); } j = i; } return(false); }
///Returns whether a circle intersects a polygon (given a list of vertices) public static bool CircleIntersectsPoly(Circle c, List <Vector2> verts) { //check to see if the center of the circle is in the poly if (PointInPoly(c.WorldCenter, verts)) { return(true); } //check to see if any points are in the circle for (int i = 0; i < verts.Count; i++) { Vector2 vert = verts[i]; if (PointInCircle(vert, c)) { return(true); } } LineSeg lnsCheck = new LineSeg(); int j = verts.Count - 1; //check each line segment of the polygon until an intersection is found for (int i = 0; i < verts.Count; i++) { lnsCheck.SetA(verts[i]); lnsCheck.SetB(verts[j]); if (LineSegIntersectsCircle(lnsCheck, c)) { return(true); } j = i; } return(false); }