I'm using Visual Studio 2010, including reference Dynamic Data Display map. I'm drawing on the map polygon by drawing DragglePoints and Segments between the points. I found a code that find if point(x,y) is in polygon, but on a map it doesnt work.
pt = e.GetPosition(this.plotter.CentralGrid); // Get the mouse position
ps = this.plotter.Viewport.Transform.ScreenToViewport(pt); // Transform the mouse positon to Screen on chartplotter
// Now ps is the point converting to the map point - it works perfect
// I'm using it for another things.(like Painting a dragglepoint or something else.
for (int k = 0; k <= listPolygons.Count - 1; k++)
{
bool ifInside = PointInPolygon(new Point(ps.X, double.Parse(this.plotter.Viewport.Transform.DataTransform.ViewportToData(ps).Y.ToString())), listPolygons[k]); // Sending to the functing pointInPolygon the point and list of polygons we have
if (ifInside)
{
listPolygons[k].removePolygon(listPolygons[k], plotter);
listPolygons.RemoveAt(k);
break;
}
}
and the function PointInPolygon:
private bool PointInPolygon(Point point, Polygon polygon)
{
List<DraggablePoint> points = polygon.getListPoints();
DraggablePoint pointClicked = new DraggablePoint(new Point(point.X, point.Y));
int i, j, nvert = polygon.getNumberOfVertx();
bool c = false;
for(i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((points[i].Position.Y) >= pointClicked.Position.Y) != (points[j].Position.Y >= pointClicked.Position.Y) &&
(pointClicked.Position.X <= (points[j].Position.X - points[i].Position.X) * (pointClicked.Position.Y - points[i].Position.Y) / (points[j].Position.Y - points[i].Position.Y) + points[i].Position.X))
c = !c;
}
return c;
}
Here c
always returns as false
, whether I click inside a polygon or outside.
points[i].Position.Y
and points[i].Position.X
and pointClicked.Position.Y
and pointClicked.Position.X
is given a perfect variables - on the map values.