首页 > 技术文章 > C# 判断点是否在多边形内

sidely 2014-06-11 13:48 原文

/// <summary>
/// 判断点是否在多边形内
/// </summary>
/// <param name="pnt">点</param>
/// <param name="pntlist">区域的点集</param>
/// <returns></returns>
public static bool PointInFeaces(PointF pnt, List<PointF> pntlist)
{
      if (pntlist == null)
  {
    return false;
  }
      int j = 0, cnt = 0;
      for (int i = 0; i < pntlist.Count; i++)
      {
    j = (i == pntlist.Count - 1) ? 0 : j + 1;
    if ((pntlist[i].Y != pntlist[j].Y) && (((pnt.Y >= pntlist[i].Y) && (pnt.Y < pntlist[j].Y)) || ((pnt.Y >= pntlist[j].Y) && (pnt.Y < pntlist[i].Y))) && (pnt.X < (pntlist[j].X - pntlist[i].X) * (pnt.Y - pntlist[i].Y) / (pntlist[j].Y - pntlist[i].Y) + pntlist[i].X)) cnt++;
  }
  return (cnt % 2 > 0) ? true : false;
}

推荐阅读