public static bool Polygon(Point2d pt, List<Point2d> polygon)
{
bool inside = false;
Point2d p1, p2;
int count = polygon.Count;
for (int i = 0, j = count - 1; i < count; j = i, i++)
{
p1 = polygon[i];
p2 = polygon[j];
if (pt.y < p2.y)
{
if (p1.y <= pt.y)
{
if ((pt.y - p1.y) * (p2.x - p1.x) > (pt.x - p1.x) * (p2.y - p1.y))
{
inside = !inside;
}
}
}
else if (pt.y < p1.y)
{
if ((pt.y - p1.y) * (p2.x - p1.x) < (pt.x - p1.x) * (p2.y - p1.y))
{
inside = !inside;
}
}
}
return inside;
}