The method provides a test if a point lies inside an arbitrary shaped convex or concave poylgon. The polygon is defined by a polyline object. The polyline must be closed and must not be self-intersecting or the test may produce incorrect results.
public static void PointInPolygonTest()
{
// Create a closed sample polygon
Polyline pl =
new Polyline
();
pl.
Add(new Edge
(new Point
(2.0000,
0.0000,
0),
new Point
(0.9239,
0.3827,
0)));
pl.
Add(new Edge
(new Point
(0.9239,
0.3827,
0),
new Point
(1.4142,
1.4142,
0)));
pl.
Add(new Edge
(new Point
(1.4142,
1.4142,
0),
new Point
(0.3827,
0.9239,
0)));
pl.
Add(new Edge
(new Point
(0.3827,
0.9239,
0),
new Point
(0.0000,
2.0000,
0)));
pl.
Add(new Edge
(new Point
(0.0000,
2.0000,
0),
new Point
(-0.3827,
0.9239,
0)));
pl.
Add(new Edge
(new Point
(-0.3827,
0.9239,
0),
new Point
(-1.4142,
1.4142,
0)));
pl.
Add(new Edge
(new Point
(-1.4142,
1.4142,
0),
new Point
(-0.9239,
0.3827,
0)));
pl.
Add(new Edge
(new Point
(-0.9239,
0.3827,
0),
new Point
(-2.0000,
0.0000,
0)));
pl.
Add(new Edge
(new Point
(-2.0000,
0.0000,
0),
new Point
(2.0000,
0.0000,
0)));
// Test a point
Point p1 =
new Point
(0.5,
1.5,
0);
Point p2 =
new Point
(0.0,
1.5,
0);
Console.WriteLine(pl.Contains(p1)); //false
Console.WriteLine(pl.Contains(p2)); //true
//Visualize
sc.PointDisplayMode = 3;
Scene.
Layer lay1 =
new Scene.
Layer("Polygon",
1);
Scene.
Layer lay2 =
new Scene.
Layer("Points",
7);
for (int i = 0; i < pl.Count; i++)
{
sc.Add(pl[i], lay1);
}
sc.Add(p1, lay2);
sc.Add(p2, lay2);
sc.WriteDxf(@"e:\polygon.dxf");
Console.ReadLine();
}

- A closed, non-selfintersecting polygon and two points to be tested.
<< back