/// <summary> /// The validate method check if the XYPolyline is valid. The checks /// made are: /// - is number of points >= 3 /// - is the length of all line segments positiv /// - do any lines cross /// - is the area positiv /// Exception is raised if the constraints are not met. /// </summary> public new void Validate() { if (Points.Count < 3) { throw new System.Exception("Number of vertices in polygon element is less than 3."); } if (GetArea() <= 0) { throw new System.Exception("Area of polygon is negative or zero. XYPolygons must be ordered counter clockwise."); } for (int j = 0; j < Points.Count; j++) { if (GetLine(j).GetLength() == 0) { throw new System.Exception("Length of line segment no: " + j.ToString() + " (0-based) of XYPolygon is zero."); } } for (int j = 0; j < Points.Count; j++) { for (int m = 0; m < j; m++) { if (XYGeometryTools.DoLineSegmentsIntersect(GetLine(j), GetLine(m))) { throw new System.Exception("Line no: " + j.ToString() + " and line no: " + m.ToString() + " of XYPolygon crosses."); } } } }
/// <summary> /// Returns true if the polygon is inside or on the edge of this polygon /// </summary> /// <param name="p"></param> /// <returns></returns> public bool Contains(IXYPolygon p) { IEnumerable <IXYPoint> points; if (p is XYPolygon) { points = ((XYPolygon)p).Points; } else if (p is MultiPartPolygon) { points = ((MultiPartPolygon)p).Polygons.SelectMany(pp => pp.Points); } else { return(false); } //First check that all points are within the bounding box foreach (var point in points) { if (!XYGeometryTools.IsPointInPolygonOrOnEdge(point.X, point.Y, BoundingBox)) { return(false); } } foreach (var point in points) { if (!XYGeometryTools.IsPointInPolygonOrOnEdge(point.X, point.Y, this)) { return(false); } } return(true); }
public bool Contains(double X, double Y) { if (XYGeometryTools.IsPointInPolygon(X, Y, BoundingBox)) //Check the bounding box first { return(XYGeometryTools.IsPointInPolygon(X, Y, this)); } else { return(false); } }
private bool OverLaps(XYPolygon Poly) { //Are any of the other polygon bounding box corners with in this polygon if (Poly.BoundingBox.Points.Any(p => this.BoundingBox.Contains(p))) { foreach (var P in Poly.Points) { if (Contains(P)) { return(true); } } } // Are any of this bounding box corners in the other polygon if (this.BoundingBox.Points.Any(p => Poly.BoundingBox.Contains(p))) { foreach (var P in Points) { if (Poly.Contains(P)) { return(true); } } } //Do any of the bounding box lines intersect/ for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (XYGeometryTools.DoLineSegmentsIntersect(Poly.BoundingBox.Points[i], Poly.BoundingBox.Points[i + 1], BoundingBox.Points[j], BoundingBox.Points[j + 1])) { foreach (var P in Points) { if (Poly.Contains(P)) { return(true); } } foreach (var P in Poly.Points) { if (Contains(P)) { return(true); } } } } } return(false); }
/// <summary> /// The method decides if the triangle formed by P(i-1), P(i) and /// P(i+1) from Polygon are intersected by any of the other points /// of the polygon. /// </summary> /// <param name="i">Middle index for the three points that forms the triangle</param> /// <returns> /// <p>true: If the triangle P(i-1), P(i), P(i+1) is intersected by other parts of Polygon</p> /// <p>false: otherwise</p> /// </returns> protected bool IsIntersected(int i) { double x = 0; double y = 0; int n = Points.Count; int im1 = i - 1; int ip1 = i + 1; if (i == 0) { im1 = n - 1; } else if (i == n - 1) { ip1 = 0; } XYPoint nodeim1 = new XYPoint((XYPoint)Points[im1]); XYPoint nodei = new XYPoint((XYPoint)Points[i]); XYPoint nodeip1 = new XYPoint((XYPoint)Points[ip1]); XYPolygon localPolygon = new XYPolygon(); localPolygon.Points.Add(nodeim1); localPolygon.Points.Add(nodei); localPolygon.Points.Add(nodeip1); int j = 0; bool intersected = false; while (((j < n - 1) && (!intersected))) { x = Points[j].X; y = Points[j].Y; if (((((j != im1) && (j != i)) && (j != ip1)) && XYGeometryTools.IsPointInPolygon(x, y, localPolygon))) { return(true); } else { j++; } } return(false); }