/// <summary> /// Whether ths line segment intersects a polygon. /// </summary> /// <param name="polygon">the polygon.</param> /// <returns>true if intersects.</returns> public bool Intersects(Polygon2D polygon) { bool bResult = false; for (int i = 0; i < polygon.VertexCount; i++) { LineSegment2D lineSegment = polygon.GetEdge(i); //bResult |= this.TrulyIntersects(polygon.GetEdge(i)); bResult |= (this.TrulyLineIntersects(lineSegment) && lineSegment.Intersects(this)); if (bResult) { return(true); } } return(bResult); }
/// <summary> /// Whether ths line segment intersects a polygon. /// </summary> /// <param name="polygon">the polygon.</param> /// <returns>true if intersects.</returns> public bool Intersects(Polygon2D polygon) { bool bResult = false; for (int i = 0; i < polygon.VertexCount; i++) { LineSegment2D lineSegment = polygon.GetEdge(i); //bResult |= this.TrulyIntersects(polygon.GetEdge(i)); bResult |= (this.TrulyLineIntersects(lineSegment) && lineSegment.Intersects(this)); if (bResult) { return true; } } return bResult; }
/// <summary> /// Divide a sub polygon by a line segment. /// </summary> /// <param name="lineSegment">the line segment.</param> /// <param name="poly">the polygon to divide.</param> /// <returns>one of the sub polygon divided from the polygon.</returns> internal Polygon2D DividedBy(LineSegment2D lineSegment, Polygon2D poly) { if (poly.isDiagonal(lineSegment)) { int a = poly.HasVertex(lineSegment.FirstPoint); int b = poly.HasVertex(lineSegment.LastPoint); if (a > b) { int tmp = a; a = b; b = tmp; } Point2DCollection p2 = new Point2DCollection(b - a + 1); Point2DCollection p1 = new Point2DCollection(poly.VertexCount - p2.Size + 2); for (int i = 0; i < poly.VertexCount; i++) { if (i <= a || i >= b) { p1.Add(poly.GetPoint(i)); } if (i >= a && i <= b) { p2.Add(poly.GetPoint(i)); } } if (p1.Count > p2.Count) { SubDivision.Add(new Polygon2D(p1)); return(new Polygon2D(p2)); } else { SubDivision.Add(new Polygon2D(p2)); return(new Polygon2D(p1)); } } else if (lineSegment.Intersects(poly)) { Point2DCollection Points = new Point2DCollection(2); for (int i = 0; i < poly.VertexCount; i++) { LineSegment2D border = poly.GetEdge(i); Point2D p = lineSegment.GetIntersectPoint(border); if (p.isRegular) { Points.DistinctAdd(p); } } Debug.Assert(Points.Count == 2); if (poly.HasVertex(Points[0]) == Polygon2D.NoSuchPoint) { poly.Add(Points[0], poly.OnEdge(Points[0])); Parent.AddInner(Points[0]); this.InnerPoints.DistinctAdd(Points[0]); } if (poly.HasVertex(Points[1]) == Polygon2D.NoSuchPoint) { poly.Add(Points[1], poly.OnEdge(Points[1])); Parent.AddInner(Points[1]); this.InnerPoints.DistinctAdd(Points[1]); } LineSegment2D line = new LineSegment2D(Points[0], Points[1]); return(DividedBy(line, poly)); } else { return(poly); } }
/// <summary> /// Divide a sub polygon by a line segment. /// </summary> /// <param name="lineSegment">the line segment.</param> /// <param name="poly">the polygon to divide.</param> /// <returns>one of the sub polygon divided from the polygon.</returns> internal Polygon2D DividedBy(LineSegment2D lineSegment, Polygon2D poly) { if (poly.isDiagonal(lineSegment)) { int a = poly.HasVertex(lineSegment.FirstPoint); int b = poly.HasVertex(lineSegment.LastPoint); if (a > b) { int tmp = a; a = b; b = tmp; } Point2DCollection p2 = new Point2DCollection(b - a + 1); Point2DCollection p1 = new Point2DCollection(poly.VertexCount - p2.Size + 2); for (int i = 0; i < poly.VertexCount; i++) { if (i <= a || i >= b) { p1.Add(poly.GetPoint(i)); } if (i >= a && i <= b) { p2.Add(poly.GetPoint(i)); } } if (p1.Count > p2.Count) { SubDivision.Add(new Polygon2D(p1)); return new Polygon2D(p2); } else { SubDivision.Add(new Polygon2D(p2)); return new Polygon2D(p1); } } else if (lineSegment.Intersects(poly)) { Point2DCollection Points = new Point2DCollection(2); for (int i = 0; i < poly.VertexCount; i++) { LineSegment2D border = poly.GetEdge(i); Point2D p = lineSegment.GetIntersectPoint(border); if (p.isRegular) { Points.DistinctAdd(p); } } Debug.Assert(Points.Count == 2); if (poly.HasVertex(Points[0]) == Polygon2D.NoSuchPoint) { poly.Add(Points[0], poly.OnEdge(Points[0])); Parent.AddInner(Points[0]); this.InnerPoints.DistinctAdd(Points[0]); } if (poly.HasVertex(Points[1]) == Polygon2D.NoSuchPoint) { poly.Add(Points[1], poly.OnEdge(Points[1])); Parent.AddInner(Points[1]); this.InnerPoints.DistinctAdd(Points[1]); } LineSegment2D line = new LineSegment2D(Points[0], Points[1]); return DividedBy(line, poly); } else { return poly; } }
private void Divide(Polygon2D poly) { for (int i = 0; i < poly.VertexCount; i++) //Find Convex Angle { if (poly.isConvexAngle(i)) { continue; } int index = i; double min_dist = 10000; for (int j = 0; j < poly.VertexCount; j++) //Divide Polygon { if (j == i) { continue; } LineSegment2D line = new LineSegment2D(poly.GetPoint(i), poly.GetPoint(j)); if (poly.isDiagonal(line)) { //double dist = line.Length; Vector2D v1 = line.ToVector(); LineSegment2D line1 = poly.GetEdge(i); line1.SwapPoints(); LineSegment2D line2 = poly.GetEdge((i + 1) % poly.VertexCount); Vector2D v2 = line1.ToVector(); Vector2D v3 = line2.ToVector(); double angle1 = Vector2D.Angle(v2, v1); double angle2 = Vector2D.Angle(v1, v3); //ѡȡ���ֽǶ�����ȵĵ����ָ�� if (angle1 < 0) { angle1 += Math.PI * 2; } if (angle2 < 0) { angle2 += Math.PI * 2; } double dist = Math.Max(angle1, angle2); if (!poly.isConvexAngle(j) && dist < Math.PI) { dist -= Math.PI; } if(dist < min_dist) { index = j; min_dist = dist; } } } if (index != i) { this.DividedBy(new LineSegment2D(poly.GetPoint(i), poly.GetPoint(index))); return; } } throw (new ArgumentException()); }