//判断点在线的一边
public int isLeft(v_points P0, v_points P1, v_points P2)
{
int abc = (int)((P1.X – P0.X) * (P2.Y – P0.Y) – (P2.X – P0.X) * (P1.Y – P0.Y));
return abc;
}
public static bool Contains(Point[] points, v_points p)
{
bool result = false;
for (int i = 0; i < points.Length – 1; i++)
{
if ((((points[i + 1].Y <= p.Y) && (p.Y < points[i].Y)) || ((points[i].Y <= p.Y) && (p.Y < points[i + 1].Y))) && (p.X < (points[i].X – points[i + 1].X) * (p.Y – points[i + 1].Y) / (points[i].Y – points[i + 1].Y) + points[i + 1].X))
{
result = !result;
}
}
return result;
}
//判断点pnt能否在region内主程序
public bool SOSCheckLocation(v_points pnt, List<v_points> region)
{
int wn = 0, j = 0; //wn 计数器 j第二个点指针
for (int i = 0; i < region.Count; i++)
{
//开始循环
if (i == region.Count – 1)
{
j = 0;//假如 循环到最后一点 第二个指针指向第一点
}
else
{
j = j + 1; //假如不是 ,则找下一点
}
if (region[i].Y <= pnt.Y) // 假如多边形的点 小于等于 选定点的 Y 坐标
{
if (region[j].Y > pnt.Y) // 假如多边形的下一点 大于于 选定点的 Y 坐标
{
if (isLeft(region[i], region[j], pnt) > 0)
{
wn++;
}
}
}
else
{
if (region[j].Y <= pnt.Y)
{
if (isLeft(region[i], region[j], pnt) < 0)
{
wn–;
}
}
}
}
if (wn == 0)
{
return false;
}
else
{
return true;
}
}
这种方法好像总是返回FALSE,求高手给个算法,网上大多不靠谱
public int isLeft(v_points P0, v_points P1, v_points P2)
{
int abc = (int)((P1.X – P0.X) * (P2.Y – P0.Y) – (P2.X – P0.X) * (P1.Y – P0.Y));
return abc;
}
public static bool Contains(Point[] points, v_points p)
{
bool result = false;
for (int i = 0; i < points.Length – 1; i++)
{
if ((((points[i + 1].Y <= p.Y) && (p.Y < points[i].Y)) || ((points[i].Y <= p.Y) && (p.Y < points[i + 1].Y))) && (p.X < (points[i].X – points[i + 1].X) * (p.Y – points[i + 1].Y) / (points[i].Y – points[i + 1].Y) + points[i + 1].X))
{
result = !result;
}
}
return result;
}
//判断点pnt能否在region内主程序
public bool SOSCheckLocation(v_points pnt, List<v_points> region)
{
int wn = 0, j = 0; //wn 计数器 j第二个点指针
for (int i = 0; i < region.Count; i++)
{
//开始循环
if (i == region.Count – 1)
{
j = 0;//假如 循环到最后一点 第二个指针指向第一点
}
else
{
j = j + 1; //假如不是 ,则找下一点
}
if (region[i].Y <= pnt.Y) // 假如多边形的点 小于等于 选定点的 Y 坐标
{
if (region[j].Y > pnt.Y) // 假如多边形的下一点 大于于 选定点的 Y 坐标
{
if (isLeft(region[i], region[j], pnt) > 0)
{
wn++;
}
}
}
else
{
if (region[j].Y <= pnt.Y)
{
if (isLeft(region[i], region[j], pnt) < 0)
{
wn–;
}
}
}
}
if (wn == 0)
{
return false;
}
else
{
return true;
}
}
这种方法好像总是返回FALSE,求高手给个算法,网上大多不靠谱
解决方案
5
本人记得是过这个点,随便划条线,两边交点都是奇数个就在多边形内部。
20
private bool pointInPolygon(float x, float y, PointF[] polygon) { int i; int j = polygon.Length - 1; bool oddNodes = false; for (i = 0; i < polygon.Length; i++) { if ((polygon[i].Y < y && polygon[j].Y >= y || polygon[j].Y < y && polygon[i].Y >= y) && (polygon[i].X <= x || polygon[j].X <= x)) { if (polygon[i].X + (y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < x) { oddNodes = !oddNodes; } } j = i; } return oddNodes; }
5
你断点进去看看是不是这里出现问题了
if ((((points[i + 1].Y <= p.Y) && (p.Y < points[i].Y)) || ((points[i].Y <= p.Y) && (p.Y < points[i + 1].Y))) && (p.X < (points[i].X – points[i + 1].X) * (p.Y – points[i + 1].Y) / (points[i].Y – points[i + 1].Y) + points[i + 1].X))
{
result = !result;
}
if ((((points[i + 1].Y <= p.Y) && (p.Y < points[i].Y)) || ((points[i].Y <= p.Y) && (p.Y < points[i + 1].Y))) && (p.X < (points[i].X – points[i + 1].X) * (p.Y – points[i + 1].Y) / (points[i].Y – points[i + 1].Y) + points[i + 1].X))
{
result = !result;
}
10
http://erich.realtimerendering.com/ptinpoly/
http://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon
http://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon