public static Point PolygonCentre(IPolygonReader poly) { if (poly == null) { throw (new ArgumentNullException("PolygonCentre(poly)")); } Point ret = new Point(); double[] v = new double[3] { 0, 0, 0 }; double[,] p = poly.CopyTo(); double dx, dy; int b = p.GetLength(0) - 1; for (int e = 0; e < p.GetLength(0); e++) { dx = p[e, 0] - p[b, 0]; dy = p[e, 1] - p[b, 1]; v[2] += (p[b, 0] * dy - p[b, 1] * dx) / 2.0; v[0] += -dx * (3.0 * p[b, 1] * p[b, 1] + 3.0 * p[b, 1] * dy + dy * dy) / 6.0; v[1] += dy * (3.0 * p[b, 0] * p[b, 0] + 3.0 * p[b, 0] * dx + dx * dx) / 6.0; b = e; } ret.X = v[1] / v[2]; ret.Y = v[0] / v[2]; return(ret); }
public PolygonSelfCrossing(IPolygonReader poly, bool isClosed = true) { IsClosed = isClosed; if (poly == null) { throw (new ArgumentNullException("PolygonSelfCrossing(poly)")); } myCrossId = null; mySplits = null; myPoly = poly.CopyTo(); int length = myPoly.GetLength(0); myData = new MyLine[length]; for (int i = 0; i < myData.Length; i++) { myData[i] = new MyLine(i, i == (myData.Length - 1)); } int begin = 0; if (!IsClosed) { length -= 2; begin = 1; } for (int i = begin; i < length; i++) { for (int j = i + 2; j < length; j++) { if (myData[i].TestCrossing(myData[j], myPoly)) { if (myCrossId == null) { myCrossId = new List <int[]>(); } myCrossId.Add(new int[] { i, j }); } } } for (int i = 0; i < myData.Length; i++) { myData[i].Sort(); } }
// vrati relativni souradnice pruseciku primky s polygonem public static List <double> PolygonLineCrossingRel(IPolygonReader poly, bool closed, Point pt, Vector v) { if (poly == null) { throw (new ArgumentNullException("PolygonLineCrossing_rel(poly,..)")); } List <double> ret = new List <double>(); int b, e; double[,] p = poly.CopyTo(); if (closed) { e = poly.Length - 1; b = 0; } else { e = 0; b = 1; } Point p1, p2; for (int i = b; i < poly.Length; i++) { b = e; e = i; p1 = new Point(p[b, 0], p[b, 1]); p2 = new Point(p[e, 0], p[e, 1]); if (TwoLine2D.CrossRel(pt, v, p1, p2.Minus(p1), out Point pr)) { if (TwoLine2D.TestRelOnLine(TwoLine2D.CrossStatus.And, pr.Y)) { ret.Add(pr.X); } } } ret.Sort(); return(ret); }
// polygons private static double f_PolygonArea(IPolygonReader poly) { if (poly == null) { throw (new ArgumentNullException("f_PolygonArea(poly)")); } if (poly.Length < 3) { return(0); } double dx, dy, a = 0; double[,] p = poly.CopyTo(); int b = p.GetLength(0) - 1; for (int e = 0; e < p.GetLength(0); e++) { dx = p[e, 0] - p[b, 0]; dy = p[e, 1] - p[b, 1]; a += (p[b, 0] * dy - p[b, 1] * dx) / 2.0; b = e; } return(a); }