多边形中的C#点

我试图确定一个点是否在多边形内。多边形是由Point对象数组定义的。我可以很容易地弄清楚该点是否在多边形的边界框内,但是我不确定如何判断它是否在实际的多边形内。如果可能的话,我只想使用C#和WinForms。我宁愿不调用OpenGL或执行某些简单任务。


这是我到目前为止的代码:


private void CalculateOuterBounds()

{

    //m_aptVertices is a Point[] which holds the vertices of the polygon.

    // and X/Y min/max are just ints

    Xmin = Xmax = m_aptVertices[0].X;

    Ymin = Ymax = m_aptVertices[0].Y;


    foreach(Point pt in m_aptVertices)

    {

        if(Xmin > pt.X)

            Xmin = pt.X;


        if(Xmax < pt.X)

            Xmax = pt.X;


        if(Ymin > pt.Y)

            Ymin = pt.Y;


        if(Ymax < pt.Y)

            Ymax = pt.Y;

    }

}


public bool Contains(Point pt)

{

    bool bContains = true; //obviously wrong at the moment :)


    if(pt.X < Xmin || pt.X > Xmax || pt.Y < Ymin || pt.Y > Ymax)

        bContains = false;

    else

    {

        //figure out if the point is in the polygon

    }


    return bContains;

}


拉莫斯之舞
浏览 771回答 3
3回答

RISEBY

我在这里检查了代码,都遇到了问题。最好的方法是:&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Determines if the given point is inside the polygon&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="polygon">the vertices of polygon</param>&nbsp; &nbsp; /// <param name="testPoint">the given point</param>&nbsp; &nbsp; /// <returns>true if the point is inside the polygon; otherwise, false</returns>&nbsp; &nbsp; public static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; bool result = false;&nbsp; &nbsp; &nbsp; &nbsp; int j = polygon.Count() - 1;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < polygon.Count(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = !result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }

芜湖不芜

接受的答案对我的项目不起作用。我最终使用了在这里找到的代码。public static bool IsInPolygon(Point[] poly, Point p){&nbsp; &nbsp; Point p1, p2;&nbsp; &nbsp; bool inside = false;&nbsp; &nbsp; if (poly.Length < 3)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return inside;&nbsp; &nbsp; }&nbsp; &nbsp; var oldPoint = new Point(&nbsp; &nbsp; &nbsp; &nbsp; poly[poly.Length - 1].X, poly[poly.Length - 1].Y);&nbsp; &nbsp; for (int i = 0; i < poly.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var newPoint = new Point(poly[i].X, poly[i].Y);&nbsp; &nbsp; &nbsp; &nbsp; if (newPoint.X > oldPoint.X)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p1 = oldPoint;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p2 = newPoint;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p1 = newPoint;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p2 = oldPoint;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ((newPoint.X < p.X) == (p.X <= oldPoint.X)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && (p.Y - (long) p1.Y)*(p2.X - p1.X)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; < (p2.Y - (long) p1.Y)*(p.X - p1.X))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inside = !inside;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; oldPoint = newPoint;&nbsp; &nbsp; }&nbsp; &nbsp; return inside;}
打开App,查看更多内容
随时随地看视频慕课网APP