/// <summary> /// Tests if the supplied Polygon and this Polygon are coincident in any way when compared on a shared plane. /// </summary> /// <param name="polygon">The Polygon to compare to this Polygon.</param> /// <returns> /// Returns true if the supplied Polygon do not intersect or touch this Polygon when compared on a shared plane or if the supplied Polygon is null. /// </returns> public bool Disjoint(Polygon polygon) { if (polygon == null) { return(true); } var thisPath = this.ToClipperPath(); var polyPath = polygon.ToClipperPath(); foreach (IntPoint vertex in thisPath) { if (Clipper.PointInPolygon(vertex, polyPath) != 0) { return(false); } } foreach (IntPoint vertex in polyPath) { if (Clipper.PointInPolygon(vertex, thisPath) != 0) { return(false); } } return(true); }
/// <summary> /// Tests if the supplied Polygon is within this Polygon with or without edge coincident vertices when compared on a shared plane. /// </summary> /// <param name="polygon">The Polygon to compare to this Polygon.</param> /// <returns> /// Returns true if every vertex of the supplied Polygon is within this Polygon or coincident with an edge when compared on a shared plane. Returns false if any vertex of the supplied Polygon is outside this Polygon, or if the supplied Polygon is null. /// </returns> public bool Covers(Polygon polygon) { if (polygon == null) { return(false); } var clipper = new Clipper(); var solution = new List <List <IntPoint> >(); clipper.AddPath(this.ToClipperPath(), PolyType.ptSubject, true); clipper.AddPath(polygon.ToClipperPath(), PolyType.ptClip, true); clipper.Execute(ClipType.ctIntersection, solution); if (solution.Count != 1) { return(false); } return(solution.First().ToPolygon().Area == polygon.ToClipperPath().ToPolygon().Area); }
/// <summary> /// Constructs the geometric union between this Polygon and the supplied Polygon. /// </summary> /// <param name="polygon">The Polygon to be combined with this Polygon.</param> /// <returns> /// Returns a single Polygon from a successful union. /// Returns null if a union cannot be performed on the two Polygons. /// </returns> public Polygon Union(Polygon polygon) { var thisPath = this.ToClipperPath(); var polyPath = polygon.ToClipperPath(); Clipper clipper = new Clipper(); clipper.AddPath(thisPath, PolyType.ptSubject, true); clipper.AddPath(polyPath, PolyType.ptClip, true); var solution = new List <List <IntPoint> >(); clipper.Execute(ClipType.ctUnion, solution); if (solution.Count > 1) { return(null); } return(solution.First().ToPolygon()); }
/// <summary> /// Tests if at least one point of an edge of the supplied Polygon is shared with an edge of this Polygon without the Polygons interesecting when compared on a shared plane. /// </summary> /// <param name="polygon">The Polygon to compare to this Polygon.</param> /// <returns> /// Returns true if the supplied Polygon shares at least one edge point with this Polygon without the Polygons intersecting when compared on a shared plane. Returns false if the Polygons intersect, are disjoint, or if the supplied Polygon is null. /// </returns> public bool Touches(Polygon polygon) { if (polygon == null || this.Intersects(polygon)) { return(false); } var thisPath = this.ToClipperPath(); var polyPath = polygon.ToClipperPath(); foreach (IntPoint vertex in thisPath) { if (Clipper.PointInPolygon(vertex, polyPath) == -1) { return(true); } } return(false); }
/// <summary> /// Returns Polygons representing the symmetric difference between this Polygon and the supplied Polygon. /// </summary> /// <param name="polygon">The intersecting polygon.</param> /// <returns> /// Returns a list of Polygons representing the symmetric difference of this Polygon and the supplied Polygon. /// Returns a representation of this Polygon and the supplied Polygon if the Polygons do not intersect. /// </returns> public IList <Polygon> XOR(Polygon polygon) { var thisPath = this.ToClipperPath(); var polyPath = polygon.ToClipperPath(); Clipper clipper = new Clipper(); clipper.AddPath(thisPath, PolyType.ptSubject, true); clipper.AddPath(polyPath, PolyType.ptClip, true); var solution = new List <List <IntPoint> >(); clipper.Execute(ClipType.ctUnion, solution); var polygons = new List <Polygon>(); foreach (List <IntPoint> path in solution) { polygons.Add(PolygonExtensions.ToPolygon(path)); } return(polygons); }