// A parent is a shape which fully contains another shape public bool IsParentOf(CompositeShapeData otherShape) { if (otherShape.parents.Contains(this)) { return(true); } if (parents.Contains(otherShape)) { return(false); } // check if first point in otherShape is inside this shape. If not, parent test fails. // if yes, then continue to line seg intersection test between the two shapes // (this point test is important because without it, if all line seg intersection tests fail, // we wouldn't know if otherShape is entirely inside or entirely outside of this shape) bool pointInsideShape = false; for (int i = 0; i < triangles.Length; i += 3) { if (Maths2D.PointInTriangle(polygon.points[triangles[i]], polygon.points[triangles[i + 1]], polygon.points[triangles[i + 2]], otherShape.points[0])) { pointInsideShape = true; break; } } if (!pointInsideShape) { return(false); } // Check for intersections between line segs of this shape and otherShape (any intersections will fail the parent test) for (int i = 0; i < points.Length; i++) { LineSegment parentSeg = new LineSegment(points[i], points[(i + 1) % points.Length]); for (int j = 0; j < otherShape.points.Length; j++) { LineSegment childSeg = new LineSegment(otherShape.points[j], otherShape.points[(j + 1) % otherShape.points.Length]); if (Maths2D.LineSegmentsIntersect(parentSeg.a, parentSeg.b, childSeg.a, childSeg.b)) { return(false); } } } return(true); }
// Test if the shapes overlap partially (test will fail if one shape entirely contains other shape, i.e. one is parent of the other). public bool OverlapsPartially(CompositeShapeData otherShape) { // Check for intersections between line segs of this shape and otherShape (any intersection will validate the overlap test) for (int i = 0; i < points.Length; i++) { LineSegment segA = new LineSegment(points[i], points[(i + 1) % points.Length]); for (int j = 0; j < otherShape.points.Length; j++) { LineSegment segB = new LineSegment(otherShape.points[j], otherShape.points[(j + 1) % otherShape.points.Length]); if (Maths2D.LineSegmentsIntersect(segA.a, segA.b, segB.a, segB.b)) { return(true); } } } return(false); }
// Checks if any of the line segments making up this shape intersect public bool IntersectsWithSelf() { for (int i = 0; i < points.Length; i++) { LineSegment segA = new LineSegment(points[i], points[(i + 1) % points.Length]); for (int j = i + 2; j < points.Length; j++) { if ((j + 1) % points.Length == i) { continue; } LineSegment segB = new LineSegment(points[j], points[(j + 1) % points.Length]); if (Maths2D.LineSegmentsIntersect(segA.a, segA.b, segB.a, segB.b)) { return(true); } } } return(false); }