public List <Vector2D> FindIntersections(Polygon2d o) { List <Vector2D> v = new List <Vector2D>(); if (!this.GetBounds().Intersects(o.GetBounds())) { return(v); } foreach (Segment2d seg in SegmentItr()) { foreach (Segment2d oseg in o.SegmentItr()) { IntrSegment2Segment2 intr = new IntrSegment2Segment2(seg, oseg); if (intr.Find()) { v.Add(intr.Point0); if (intr.Quantity == 2) { v.Add(intr.Point1); } break; } } } return(v); }
/// <summary> /// Find any segment in set that intersects input segment. /// Returns intersection test, and index of segment /// </summary> public IntrSegment2Segment2 FindAnyIntersection(Segment2d seg, out int iSegment) { int N = Segments.Count; for (iSegment = 0; iSegment < N; ++iSegment) { IntrSegment2Segment2 intr = new IntrSegment2Segment2(seg, Segments[iSegment]); if (intr.Find()) { return(intr); } } return(null); }
public bool Intersects(Polygon2d o) { if (!this.GetBounds().Intersects(o.GetBounds())) { return(false); } foreach (Segment2d seg in SegmentItr()) { foreach (Segment2d oseg in o.SegmentItr()) { IntrSegment2Segment2 intr = new IntrSegment2Segment2(seg, oseg); if (intr.Find()) { return(true); } } } return(false); }
public void FindAllIntersections(Segment2d seg, List <double> segmentTs, List <int> indices = null, List <IntrSegment2Segment2> tests = null, bool bOnlySimple = true) { int N = Segments.Count; for (int i = 0; i < N; ++i) { // want to make sure we do not miss any hits, even if it means // we get duplicates... IntrSegment2Segment2 intr = new IntrSegment2Segment2(seg, Segments[i]) { IntervalThreshold = math.MathUtil.ZeroTolerance }; if (intr.Find()) { if (bOnlySimple && intr.IsSimpleIntersection == false) { continue; } if (tests != null) { tests.Add(intr); } if (indices != null) { indices.Add(i); } if (segmentTs != null) { segmentTs.Add(intr.Parameter0); if (!intr.IsSimpleIntersection) { segmentTs.Add(intr.Parameter1); } } } } }
/// <summary> /// Trim / split a segment by a polygon. /// </summary> /// <param name="_type">0 = inside; 1 = outside ; 2 = both sides</param> public static List <Segment2d> Trim(this Polygon2d _polygon, Segment2d _seg, int _type) { var list = new List <Segment2d>(); //find intersections List <KeyValuePair <double, Vector2d> > intPs = new List <KeyValuePair <double, Vector2d> >(); foreach (var edge in _polygon.SegmentItr()) { var segIntr = new IntrSegment2Segment2(_seg, edge); bool b = segIntr.Find(); if (b) { intPs.Add(new KeyValuePair <double, Vector2d>(segIntr.Parameter0, segIntr.Point0)); } } //if no intersection if (intPs.Count == 0) { if (_type == 1) { //return empty list } else { //check if segment is contained inside polygon if (_polygon.Contains(_seg)) { list.Add(_seg); } } return(list); } //sort intersection points by position var sortedIntPs = intPs.OrderBy(x => x.Key).Select(x => x.Value).ToList(); //get two list of segments from intersections var list0 = new List <Segment2d>(); var list1 = new List <Segment2d>() { new Segment2d(_seg.P0, sortedIntPs[0]) }; for (int i = 1; i < sortedIntPs.Count; i += 2) { Vector2d p0, p1, p2; p0 = sortedIntPs[i - 1]; p1 = sortedIntPs[i]; list0.Add(new Segment2d(p0, p1)); if (i + 1 < sortedIntPs.Count) { p2 = sortedIntPs[i + 1]; list1.Add(new Segment2d(p1, p2)); } } //use even/odd of intr num to decide the last segment var last = new Segment2d(sortedIntPs.Last(), _seg.P1); if (sortedIntPs.Count % 2 == 0) { list1.Add(last); } else { list0.Add(last); } //check starting point inside or outside bool startInside = _polygon.Contains(_seg.P0); //return coresponding list of segments if (_type == 0) { return(startInside ? list1 : list0); } else if (_type == 1) { return(startInside ? list0 : list1); } else { list.AddRange(list0); list.AddRange(list1); return(list); } }