为了账号安全,请及时绑定邮箱和手机立即绑定

多边形中的C#点

多边形中的C#点

拉莫斯之舞 2019-10-09 15:52:30
我试图确定一个点是否在多边形内。多边形是由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;}
查看完整描述

3 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

我在这里检查了代码,都遇到了问题。


最好的方法是:


    /// <summary>

    /// Determines if the given point is inside the polygon

    /// </summary>

    /// <param name="polygon">the vertices of polygon</param>

    /// <param name="testPoint">the given point</param>

    /// <returns>true if the point is inside the polygon; otherwise, false</returns>

    public static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint)

    {

        bool result = false;

        int j = polygon.Count() - 1;

        for (int i = 0; i < polygon.Count(); i++)

        {

            if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y)

            {

                if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X)

                {

                    result = !result;

                }

            }

            j = i;

        }

        return result;

    }


查看完整回答
反对 回复 2019-10-09
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

接受的答案对我的项目不起作用。我最终使用了在这里找到的代码。


public static bool IsInPolygon(Point[] poly, Point p)

{

    Point p1, p2;

    bool inside = false;


    if (poly.Length < 3)

    {

        return inside;

    }


    var oldPoint = new Point(

        poly[poly.Length - 1].X, poly[poly.Length - 1].Y);


    for (int i = 0; i < poly.Length; i++)

    {

        var newPoint = new Point(poly[i].X, poly[i].Y);


        if (newPoint.X > oldPoint.X)

        {

            p1 = oldPoint;

            p2 = newPoint;

        }

        else

        {

            p1 = newPoint;

            p2 = oldPoint;

        }


        if ((newPoint.X < p.X) == (p.X <= oldPoint.X)

            && (p.Y - (long) p1.Y)*(p2.X - p1.X)

            < (p2.Y - (long) p1.Y)*(p.X - p1.X))

        {

            inside = !inside;

        }


        oldPoint = newPoint;

    }


    return inside;

}

查看完整回答
反对 回复 2019-10-09
  • 3 回答
  • 0 关注
  • 729 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信