public ConvexHullDlg() { InitializeComponent(); points = PointSet.RandomSet(14, new Vector2(), new Vector2(200, 200)); DisplayUpdate(); }
public ConvexHullJarvisAlgorithm(PointSet ps, int maxIterations) { // Debug output Action<int> Output = (i) => Console.WriteLine("Selected #{0}: ({1}, {2})", i, (int)points[i].x, (int)points[i].y); set = ps; points = ps.points; Console.WriteLine("Running on {0} points...", ps.points.Count); available = new List<int>(ps.points.Count); for (int i = 0; i < ps.points.Count; i++) available.Add(i); // Pick leftmost point int pointOnHull = -1; for (int i = 0; i < points.Count; i++) if (pointOnHull < 0 || points[i].x < points[pointOnHull].x) pointOnHull = i; List<int> hull = new List<int>(); hull.Add(pointOnHull); // the first point should still be available, to close the loop // Output(pointOnHull); int iteration = 0; do { if (iteration++ == maxIterations) break; // Pick hull candidate int selectedIndex = -1; Vector2 org = points[pointOnHull]; for (int i = 0; i < available.Count; i++) { if (available[i] == pointOnHull) continue; if (selectedIndex < 0) { selectedIndex = available[i]; // Console.WriteLine("\tBest: {0}", selectedIndex); } else { Vector2 normal = (points[selectedIndex] - org).Ortho.Normalized; float d = points[available[i]].Dot(normal) - points[selectedIndex].Dot(normal); // Console.WriteLine("\t[{1}]: {0}", d, available[i]); // Test on which side of the plane (normal, dist) point p is located. if (d < 0) { selectedIndex = available[i]; // Console.WriteLine("\tBest: {0}", selectedIndex); } } } if (selectedIndex < 0) { Console.WriteLine("Weird stuff..."); break; } // Output(selectedIndex); pointOnHull = selectedIndex; hull.Add(pointOnHull); TakePoint(pointOnHull); } while (pointOnHull != hull[0] && available.Count>0); result = hull.ToArray(); }
private void buttonRndGraph_Click(object sender, EventArgs e) { points = PointSet.RandomSet(50, new Vector2(), new Vector2(200, 200)); DisplayUpdate(); }