public bool PointInsidePolygon(PVector p) { int intersectionCount = 0; int N = Points.Count; Edge pEdge = new Edge(p, new PVector(float.MaxValue, float.MaxValue)); for (int i = 0; i < N; i++) { Edge edge = new Edge(Points[i], Points[(i + 1) % N]); if (pEdge.IntersectionTest(edge)) { intersectionCount++; } } if (intersectionCount % 2 == 1) { return(true); } return(false); }
public static double DistanceFromPointToEdge(PVector p, Edge e) { double edgeDist2 = e.DistanceSquared(); if (edgeDist2 == 0) { return(DistanceFromPointToPoint(p, e.EndPoint1)); } double t = ((p.X - e.EndPoint1.X) * (e.EndPoint2.X - e.EndPoint1.X) + (p.Y - e.EndPoint1.Y) * (e.EndPoint2.Y - e.EndPoint1.Y)) / edgeDist2; t = Math.Max(0, Math.Min(1, t)); double x = e.EndPoint1.X + t * (e.EndPoint2.X - e.EndPoint1.X); double y = e.EndPoint1.Y + t * (e.EndPoint2.Y - e.EndPoint1.Y); double dx = x - p.X; double dy = y - p.Y; double dist = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)); return(dist); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); PVector v3 = new PVector(10, 3); PVector v2 = new PVector(7, 6); PVector v1 = new PVector(4, 3); PVector v0 = new PVector(0, 0); Edge e1 = new Edge(v3, v2); Edge e2 = new Edge(v0, v1); List <PVector> arr = new List <PVector> { v2, v3 }; List <PVector> arr1 = new List <PVector> { v0, v1 }; List <PVector> arr2 = new List <PVector> { v1, v2, v3 }; Polygon poly = new Polygon(arr); Polygon poly1 = new Polygon(arr1); Polygon poly2 = new Polygon(arr2); List <Polygon> polys = new List <Polygon> { poly, poly1 }; Console.WriteLine(DistanceCalculator.DistanceFromPolygonList(polys)); Console.WriteLine(DistanceCalculator.DistanceFromEdgeToEdge(e1, e2)); Console.WriteLine(DistanceCalculator.DistanceFromPointToEdge(v0, e1)); Console.WriteLine(DistanceCalculator.DistanceFromPointToPoint(v1, v3)); Console.WriteLine(DistanceCalculator.DistanceFromPointToPolygon(v1, poly2)); Console.WriteLine(DistanceCalculator.DistanceFromEdgeToPolygon(e2, poly2)); }
public static double Cross(PVector v1, PVector v2) { return(v1.X * v2.Y - v2.X * v1.Y); }
public static PVector Subtract(PVector v1, PVector v2) { return(new PVector(v1.X - v2.X, v1.Y - v2.Y)); }
public static PVector Add(PVector v1, PVector v2) { return(new PVector(v1.X + v2.X, v1.Y + v2.Y)); }
public static double Dot(PVector v1, PVector v2) { return(v1.X * v2.X + v1.Y * v2.Y); }
public static double Dist(PVector v1, PVector v2) { double distSquared = Math.Pow(v1.X - v2.X, 2) + Math.Pow(v1.Y - v2.Y, 2); return(Math.Sqrt(distSquared)); }
public double Cross(PVector v) { return(X * v.Y - v.X * Y); }
public PVector Subtract(PVector v) { return(new PVector(X - v.X, Y - v.Y)); }
public PVector Add(PVector v) { return(new PVector(X + v.X, Y + v.Y)); }
public double Dist(PVector v) { double distSquared = Math.Pow(X - v.X, 2) + Math.Pow(Y - v.Y, 2); return(Math.Sqrt(distSquared)); }
public double Dot(PVector v) { return(X * v.X + Y * v.Y); }
public Edge(PVector endPoint1, PVector endPoint2) { EndPoint1 = endPoint1; EndPoint2 = endPoint2; }
public void AddPoint(PVector p) { Points.Add(p); }