protected override void OnMouseMove(MouseEventArgs args) { base.OnMouseMove(args); args.Handled = true; PointD cursor = args.GetPosition(OutputBox).ToPointD(); // determine relative location of cursor PolygonLocation location = (_tolerance == 0m ? GeoAlgorithms.PointInPolygon(cursor, _polygon) : GeoAlgorithms.PointInPolygon(cursor, _polygon, (double)_tolerance)); LocationLabel.Content = location; }
public void PointInPolygonEpsilon() { PointD[] p = { new PointD(0, 0), new PointD(1, 1), new PointD(2, 0) }; Assert.AreEqual(PolygonLocation.Inside, GeoAlgorithms.PointInPolygon(new PointD(1.0, 0.5), p, 0.2)); Assert.AreEqual(PolygonLocation.Outside, GeoAlgorithms.PointInPolygon(new PointD(1.0, -0.5), p, 0.2)); Assert.AreEqual(PolygonLocation.Outside, GeoAlgorithms.PointInPolygon(new PointD(0.0, 0.5), p, 0.2)); Assert.AreEqual(PolygonLocation.Outside, GeoAlgorithms.PointInPolygon(new PointD(2.0, 0.5), p, 0.2)); Assert.AreEqual(PolygonLocation.Vertex, GeoAlgorithms.PointInPolygon(new PointD(1.0, 0.9), p, 0.2)); Assert.AreEqual(PolygonLocation.Vertex, GeoAlgorithms.PointInPolygon(new PointD(0.0, 0.1), p, 0.2)); Assert.AreEqual(PolygonLocation.Vertex, GeoAlgorithms.PointInPolygon(new PointD(2.1, 0.0), p, 0.2)); Assert.AreEqual(PolygonLocation.Edge, GeoAlgorithms.PointInPolygon(new PointD(1.0, -0.1), p, 0.2)); Assert.AreEqual(PolygonLocation.Edge, GeoAlgorithms.PointInPolygon(new PointD(0.6, 0.5), p, 0.2)); Assert.AreEqual(PolygonLocation.Edge, GeoAlgorithms.PointInPolygon(new PointD(1.4, 0.5), p, 0.2)); }
private void GeometryBasicTest() { Stopwatch timer = new Stopwatch(); long polyTicks = 0, polyEpsilonTicks = 0, lineTicks = 0, lineEpsilonTicks = 0; const double epsilon = 1e-10; const int outerLoop = 10000, innerLoop = 1000; const int iterations = outerLoop * innerLoop; for (int i = 0; i < outerLoop; i++) { PointD[] polygon = GeoAlgorithms.RandomPolygon(0, 0, 1000, 1000); LineD line = GeoAlgorithms.RandomLine(0, 0, 1000, 1000); LineD line2 = GeoAlgorithms.RandomLine(0, 0, 1000, 1000); PointD q = GeoAlgorithms.RandomPoint(0, 0, 1000, 1000); // trigger JIT compilation if (i == 0) { GeoAlgorithms.PointInPolygon(q, polygon); GeoAlgorithms.PointInPolygon(q, polygon, epsilon); line.Intersect(line2); line.Intersect(line2, epsilon); } timer.Restart(); for (int j = 0; j < innerLoop; j++) { line.Intersect(line2); } timer.Stop(); lineTicks += timer.ElapsedTicks; timer.Restart(); for (int j = 0; j < innerLoop; j++) { line.Intersect(line2, epsilon); } timer.Stop(); lineEpsilonTicks += timer.ElapsedTicks; timer.Restart(); for (int j = 0; j < innerLoop; j++) { GeoAlgorithms.PointInPolygon(q, polygon); } timer.Stop(); polyTicks += timer.ElapsedTicks; timer.Restart(); for (int j = 0; j < innerLoop; j++) { GeoAlgorithms.PointInPolygon(q, polygon, epsilon); } timer.Stop(); polyEpsilonTicks += timer.ElapsedTicks; } Output(" "); Output(String.Format("{0,12}", "Exact")); Output(String.Format("{0,12}", "Epsilon")); Output("\nLine Intersection "); Output(String.Format("{0,12:N2}", 1000 * AverageMicrosecs(lineTicks, iterations))); Output(String.Format("{0,12:N2}", 1000 * AverageMicrosecs(lineEpsilonTicks, iterations))); Output("\nPoint in Polygon "); Output(String.Format("{0,12:N2}", 1000 * AverageMicrosecs(polyTicks, iterations))); Output(String.Format("{0,12:N2}", 1000 * AverageMicrosecs(polyEpsilonTicks, iterations))); Output("\nTimes are nsec averages for exact and epsilon comparisons.\n"); Output("Point in Polygon uses random polygons with 3-60 vertices.\n"); }