C#方法:計算一個點是否在多邊形裡,引數:點,多邊形陣列
阿新 • • 發佈:2019-01-02
//計算一個點是否在多邊形裡,引數:點,多邊形陣列
public bool IsWithin(Point pt, Point[] polygon, bool noneZeroMode)
{
int ptNum = polygon.Count();
if (ptNum < 3)
{
return false;
}
int j = ptNum - 1;
bool oddNodes = false;
int zeroState = 0;
for (int k = 0; k < ptNum; k++)
{
Point ptK = polygon[k];
Point ptJ = polygon[j];
if (((ptK.Y > pt.Y) != (ptJ.Y > pt.Y)) && (pt.X < (ptJ.X - ptK.X) * (pt.Y - ptK.Y) / (ptJ.Y - ptK.Y) + ptK.X))
{
oddNodes = !oddNodes;
if (ptK.Y > ptJ.Y)
{
zeroState++;
}
else
{
zeroState--;
}
}
j = k;
}
return noneZeroMode ? zeroState != 0 : oddNodes;
}
public bool IsWithin(Point pt, Point[] polygon, bool noneZeroMode)
{
int ptNum = polygon.Count();
if (ptNum < 3)
{
return false;
}
int j = ptNum - 1;
bool oddNodes = false;
int zeroState = 0;
for (int k = 0; k < ptNum; k++)
{
Point ptK = polygon[k];
Point ptJ = polygon[j];
if (((ptK.Y > pt.Y) != (ptJ.Y > pt.Y)) && (pt.X < (ptJ.X - ptK.X) * (pt.Y - ptK.Y) / (ptJ.Y - ptK.Y) + ptK.X))
{
oddNodes = !oddNodes;
if (ptK.Y > ptJ.Y)
{
zeroState++;
}
else
{
zeroState--;
}
}
j = k;
}
return noneZeroMode ? zeroState != 0 : oddNodes;
}