/// <summary> /// True if it crosses the line. Provides the intersection points. /// </summary> /// <param name="Line">The other line.</param> /// <param name="IntersectionPts">Output. The intersection points.</param> public bool Crosses(C2DLineBase Line, List <C2DPoint> IntersectionPts) { var LineRect = new C2DRect(); Line.GetBoundingRect(LineRect); if (!BoundingRect.Overlaps(LineRect)) { return(false); } Debug.Assert(Lines.Count == LineRects.Count); if (Lines.Count != LineRects.Count) { return(false); } var IntersectionTemp = new C2DPointSet(); var bResult = false; for (var i = 0; i < this.Lines.Count; i++) { if (LineRects[i].Overlaps(LineRect) && Lines[i].Crosses(Line, IntersectionTemp as List <C2DPoint>)) { bResult = true; } } IntersectionPts.InsertRange(0, IntersectionTemp); return(bResult); }
/// <summary> /// True if it crosses the other. /// </summary> /// <param name="Other">The other polygon.</param> public bool Crosses(C2DPolyBase Other) { if (!BoundingRect.Overlaps(Other.BoundingRect)) { return(false); } var Temp = new List <C2DPoint>(); for (var i = 0; i < Lines.Count; i++) { if (Other.Crosses(Lines[i], Temp)) { return(true); } } return(false); }
/// <summary> /// Intersection with another. /// </summary> /// <param name="Other">The other polygon.</param> /// <param name="IntersectionPts">Output. The intersection points.</param> public bool Crosses(C2DPolyBase Other, List <C2DPoint> IntersectionPts) { if (!BoundingRect.Overlaps(Other.BoundingRect)) { return(false); } var IntPtsTemp = new List <C2DPoint>(); var Index1 = new List <int>(); var Index2 = new List <int>(); Lines.GetIntersections(Other.Lines, IntPtsTemp, Index1, Index2, BoundingRect, Other.BoundingRect); var bResult = IntPtsTemp.Count > 0; IntersectionPts.InsertRange(0, IntPtsTemp); return(bResult); }
/// <summary> /// Gets the boolean operation with the other. e.g. union / intersection. /// </summary> /// <param name="Other">The other polygon.</param> /// <param name="HoledPolys">The set to recieve the result.</param> /// <param name="bThisInside">The flag to indicate routes inside.</param> /// <param name="bOtherInside">The flag to indicate routes inside for the other.</param> /// <param name="grid">The degenerate settings.</param> public void GetBoolean(C2DPolyBase Other, List <C2DHoledPolyBase> HoledPolys, bool bThisInside, bool bOtherInside, CGrid grid) { if (BoundingRect.Overlaps(Other.BoundingRect)) { switch (grid.DegenerateHandling) { case CGrid.eDegenerateHandling.None: { var Routes1 = new C2DLineBaseSetSet(); var Routes2 = new C2DLineBaseSetSet(); C2DPolyBase.GetRoutes(this, bThisInside, Other, bOtherInside, Routes1, Routes2); Routes1.ExtractAllOf(Routes2); if (Routes1.Count > 0) { // Add all the joining routes together to form closed routes Routes1.MergeJoining(); // Set up some temporary polygons. var Polygons = new List <C2DPolyBase>(); // Turn the routes into polygons. for (var i = Routes1.Count - 1; i >= 0; i--) { if (Routes1[i].IsClosed(true) && Routes1[i].Count > 2) { Polygons.Add(new C2DPolyBase()); Polygons[Polygons.Count - 1].CreateDirect(Routes1[i]); } else { // Debug.Assert(false); grid.LogDegenerateError(); } } // Set up some temporary holed polygons var NewComPolys = new C2DHoledPolyBaseSet(); // Turn the set of polygons into holed polygons. Not needed for intersection. if (!(bThisInside && bOtherInside)) { C2DHoledPolyBase.PolygonsToHoledPolygons(NewComPolys, Polygons); if (NewComPolys.Count != 1) { // Debug.Assert(false); grid.LogDegenerateError(); } } else { for (var i = 0; i < Polygons.Count; i++) { HoledPolys.Add(new C2DHoledPolyBase(Polygons[i])); } } // Now add them all to the provided set. for (var i = 0; i < NewComPolys.Count; i++) { HoledPolys.Add(NewComPolys[i]); } } } break; case CGrid.eDegenerateHandling.RandomPerturbation: { var OtherCopy = new C2DPolyBase(Other); OtherCopy.RandomPerturb(); grid.DegenerateHandling = CGrid.eDegenerateHandling.None; GetBoolean(OtherCopy, HoledPolys, bThisInside, bOtherInside, grid); grid.DegenerateHandling = CGrid.eDegenerateHandling.RandomPerturbation; } break; case CGrid.eDegenerateHandling.DynamicGrid: { var Rect = new C2DRect(); if (this.BoundingRect.Overlaps(Other.BoundingRect, Rect)) { var dOldGrid = grid.GridSize; grid.SetToMinGridSize(Rect, false); grid.DegenerateHandling = CGrid.eDegenerateHandling.PreDefinedGrid; GetBoolean(Other, HoledPolys, bThisInside, bOtherInside, grid); grid.DegenerateHandling = CGrid.eDegenerateHandling.DynamicGrid; } } break; case CGrid.eDegenerateHandling.PreDefinedGrid: { var P1 = new C2DPolyBase(this); var P2 = new C2DPolyBase(Other); P1.SnapToGrid(grid); P2.SnapToGrid(grid); var V1 = new C2DVector(P1.BoundingRect.TopLeft, P2.BoundingRect.TopLeft); var dPerturbation = grid.GridSize; // ensure it snaps back to original grid positions. if (V1.i > 0) { V1.i = dPerturbation; } else { V1.i = -dPerturbation; // move away slightly if possible } if (V1.j > 0) { V1.j = dPerturbation; } else { V1.j = -dPerturbation; // move away slightly if possible } V1.i *= 0.411923; // ensure it snaps back to original grid positions. V1.j *= 0.313131; // ensure it snaps back to original grid positions. P2.Move(V1); grid.DegenerateHandling = CGrid.eDegenerateHandling.None; P1.GetBoolean(P2, HoledPolys, bThisInside, bOtherInside, grid); for (var i = 0; i < HoledPolys.Count; i++) { HoledPolys[i].SnapToGrid(grid); } grid.DegenerateHandling = CGrid.eDegenerateHandling.PreDefinedGrid; } break; case CGrid.eDegenerateHandling.PreDefinedGridPreSnapped: { var P2 = new C2DPolyBase(Other); var V1 = new C2DVector(this.BoundingRect.TopLeft, P2.BoundingRect.TopLeft); var dPerturbation = grid.GridSize; // ensure it snaps back to original grid positions. if (V1.i > 0) { V1.i = dPerturbation; } else { V1.i = -dPerturbation; // move away slightly if possible } if (V1.j > 0) { V1.j = dPerturbation; } else { V1.j = -dPerturbation; // move away slightly if possible } V1.i *= 0.411923; // ensure it snaps back to original grid positions. V1.j *= 0.313131; // ensure it snaps back to original grid positions. P2.Move(V1); grid.DegenerateHandling = CGrid.eDegenerateHandling.None; GetBoolean(P2, HoledPolys, bThisInside, bOtherInside, grid); for (var i = 0; i < HoledPolys.Count; i++) { HoledPolys[i].SnapToGrid(grid); } grid.DegenerateHandling = CGrid.eDegenerateHandling.PreDefinedGridPreSnapped; } break; } } }