Pont a poligonban
Beküldte eros.tamas - 2020, március 20 - 07:57
Pont-a-poligonban
public static bool IsPointInPolygon(Point p, Point[] polygon){double minX = polygon[0].X;double maxX = polygon[0].X;double minY = polygon[0].Y;double maxY = polygon[0].Y;for (int i = 1; i < polygon.Length; i++){Point q = polygon[i];minX = Math.Min(q.X, minX);maxX = Math.Max(q.X, maxX);minY = Math.Min(q.Y, minY);maxY = Math.Max(q.Y, maxY);}if (p.X < minX || p.X > maxX || p.Y < minY || p.Y > maxY){return false;}bool inside = false;for (int i = 0, j = polygon.Length - 1; i < polygon.Length; j = i++){if ((polygon[i].Y > p.Y) != (polygon[j].Y > p.Y) && p.X < (polygon[j].X - polygon[i].X) * (p.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) + polygon[i].X){inside = !inside;}}return inside;}
WKT poligon átalakítása pontok tömbjévé
public static Point[] wktToPolygon(string wkt) {List<Point> points = new List<Point>();wkt = wkt.Replace("POLYGON ((", "");wkt = wkt.Replace("))", "");wkt = wkt.Replace(", ", ",");string[] array = wkt.Split(',');int count = array.Length;for (int i = 0; i < count; i++) {string[] coords = array[i].Split(' ');double latitude = double.Parse(coords[1].Replace('.',','));double longitude = double.Parse(coords[0].Replace('.',','));Point point = new Point(latitude,longitude);points.Add(point);}return points.ToArray();}
- WKT-t mind a Drupal, mind a Leaflet támogatja
- WKT poligon formátuma: POLYGON ((lon1 lat1, lon2 lat2, ...., lonN latN))
- Mivel a WKT-t általános geometriai és nem csak koordinátás használatra találták ki, így figyelni kell, hogy a koordináta Y,X formátumú, a WKT pedig X,Y formátumot használ. Vagyis a latitude és a longitude értékeket fel kell cserélni mindenhol!
Mobilos kategóriák: