public static bool PolygonContains(MyPolygon polygon, MyPoint point)
{
if (polygon == null)
{
return false;
}
//删除最后一个顶点
int polygonVerticesCount = polygon.Length;
var polygonVertices = polygon.Points;
if (polygonVertices[0].Equals(polygonVertices[polygonVertices.Count - 1]))
{
polygonVerticesCount -= 1;
}
double A, B, C, D;
int count = 0;
for (int i = 0; i < polygonVerticesCount; i++)
{//[0,1]..[n-2,n-1]
var p1 = polygonVertices[i] as MyPoint;
var p2 = polygonVertices[(i + 1) % polygonVertices.Count] as MyPoint;
bool t1 = p1.X > p2.X;
bool t2 = p1.X > point.X;
bool t3 = p2.X > point.X;
if (t2 != t3)
{
A = p2.Y - p1.Y;
B = p1.X - p2.X;
C = -A * p1.X - B * p1.Y;
D = A * point.X + B * point.Y + C;
if (D < 0 && t1)
{
count++;
}
if (D > 0 && t1 == false)
{
count++;
}
}
else if ((t2 == false) && (t3 == false))
{
if (t1 && p2.X == point.X)
{
count++;
}
if ((t1 == false) && p1.X == point.X)
{
count++;
}
}
}
if (count % 2 == 1)
{
return true;
}
return false;
}