public static IEnumerable <Line> ToLines(this Polygon2d _polygon) { List <Line> lines = new List <Line>(); foreach (var seg in _polygon.SegmentItr()) { lines.Add(seg.ToLine()); } return(lines); }
public static bool BiContains(this Polygon2d poly, Segment2d seg) { foreach (Segment2d thisSeg in poly.SegmentItr()) { if (thisSeg.BiEquals(seg)) { return(true); } } return(false); }
public static bool InPolygon2d(this Vector2d vector2d, Polygon2d polygon2d) { //【】判断点是否落在多边形线段上 foreach (Segment2d segment2d in polygon2d.SegmentItr()) { double distance = segment2d.DistanceSquared(vector2d); if (distance.EqualZreo()) { return(false); } } return(polygon2d.Contains(vector2d)); }
/// <summary> /// 直线与线圈相交 /// </summary> /// <returns></returns> public static IEnumerable <Vector2d> IntrLine2Polygon(this Line2d segment2d_p0, Polygon2d regionPolygon2d) { IEnumerable <Segment2d> segment2ds = regionPolygon2d.SegmentItr(); IntrLine2Segment2 intrLine2Segment2 = new IntrLine2Segment2(segment2d_p0, segment2ds.First()); foreach (var item in segment2ds) { intrLine2Segment2.Segment = item; intrLine2Segment2.Compute(); if (intrLine2Segment2.Quantity == 1) { yield return(intrLine2Segment2.Point); } } }
public static CurveLoop ToCurveLoop(this Polygon2d _polygon2d) { _polygon2d = _polygon2d.DelDuplicate(); List <Line> lines = new List <Line>(); foreach (var seg in _polygon2d.SegmentItr()) { lines.Add(seg.ToLine()); } CurveLoop curves = new CurveLoop(); foreach (Line line in lines) { Curve _c = line as Curve; curves.Append(_c); } return(curves); }
/// <summary> /// 线圈是否为直角梯形 /// </summary> /// <returns></returns> public static bool IsTrapezoid_RightAngle(this Polygon2d polygon2d) { int count = polygon2d.VertexCount; if (count != 4) { return(false); } int calAngleCounr = 0; for (int i = 0; i < polygon2d.VertexCount; i++) { if (polygon2d.OpeningAngleDeg(i).EqualPrecision(90)) { calAngleCounr += 1; } } if (calAngleCounr != 2) { return(false); } IEnumerable <Segment2d> segment2ds = polygon2d.SegmentItr(); int segmntCount = segment2ds.Count(); for (int i = 0; i < segmntCount; i++) { for (int j = 0; j < segmntCount; j++) { if (i <= j) { continue; } Segment2d segment2d01 = segment2ds.ElementAt(i); Segment2d segment2d02 = segment2ds.ElementAt(j); if (segment2d01.Direction.Dot(segment2d02.Direction).EqualPrecision(-1)) { return(true); } } } return(false); }
/// <summary> /// 线圈是否为梯形 ——原则:四个角点,一对平行边 /// </summary> /// <returns></returns> public static bool IsTrapezoid(this Polygon2d polygon2d) { int count = polygon2d.VertexCount; if (count != 4) { return(false); } IEnumerable <Segment2d> segment2ds = polygon2d.SegmentItr(); int segmntCount = segment2ds.Count(); int pedCount = 0; for (int i = 0; i < segmntCount; i++) { for (int j = 0; j < segmntCount; j++) { if (i <= j) { continue; } Segment2d segment2d01 = segment2ds.ElementAt(i); Segment2d segment2d02 = segment2ds.ElementAt(j); if (segment2d01.Direction.Dot(segment2d02.Direction).EqualPrecision(-1)) { if (segment2d01.Length > segment2d02.Length) { pedCount += 1; } } } } if (pedCount == 1) { return(true); } return(false); }
/// <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); } }