/// <summary>
/// 判断两个区域是否存在包含点
/// </summary>
/// <param name="segA"></param>
/// <param name="seqB"></param>
/// <returns></returns>
public static bool isPointInPolygon(Point[] segA, Point[] seqB)
{
bool result = false;
for (int i = 0; i < segA.Length; i++)
{
result = IsPtInPoly(segA[i].Lng, segA[i].Lat, seqB);
if (result) return true;
}
for (int i = 0; i < seqB.Length; i++)
{
result = IsPtInPoly(seqB[i].Lng, seqB[i].Lat, segA);
if (result) return true;
}
return false;
}
/// <summary>
/// 判断点是否在多边形内或多边形上
/// </summary>
/// <param name="ALon">经度</param>
/// <param name="ALat">纬度</param>
/// <param name="Points">多边形边界点集合</param>
/// <returns></returns>
public static bool IsPtInPoly(double ALon, double ALat, Point[] Points)
{
int iSum, iCount, iIndex;
double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
if (Points.Length < 3)
{
return false;
}
iSum = 0;
iCount = Points.Length;
for (iIndex = 0; iIndex < iCount; iIndex++)
{
if (ALon == Points[iIndex].Lng && ALat == Points[iIndex].Lat) //A点在多边形上
return true;
if (iIndex == iCount - 1)
{
dLon1 = Points[iIndex].Lng;
dLat1 = Points[iIndex].Lat;
dLon2 = Points[0].Lng;
dLat2 = Points[0].Lat;
}
else
{
dLon1 = Points[iIndex].Lng;
dLat1 = Points[iIndex].Lat;
dLon2 = Points[iIndex + 1].Lng;
dLat2 = Points[iIndex + 1].Lat;
}
//以下语句判断A点是否在边的两端点的纬度之间,在则可能有交点
if (((ALat > dLat1) && (ALat < dLat2)) || ((ALat > dLat2) && (ALat < dLat1)))
{
if (Math.Abs(dLat1 - dLat2) > 0)
{
//获取A点向左射线与边的交点的x坐标:
dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);
//如果交点在A点左侧,则射线与边的全部交点数加一:
if (dLon < ALon)
{
iSum++;
}
//如果相等,则说明A点在边上
if (dLon == ALon)
return true;
}
}
}
if ((iSum % 2) != 0)
{
return true;
}
return false;
}