第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

多邊形中的C#點(diǎn)

多邊形中的C#點(diǎn)

拉莫斯之舞 2019-10-09 15:52:30
我試圖確定一個(gè)點(diǎn)是否在多邊形內(nèi)。多邊形是由Point對(duì)象數(shù)組定義的。我可以很容易地弄清楚該點(diǎn)是否在多邊形的邊界框內(nèi),但是我不確定如何判斷它是否在實(shí)際的多邊形內(nèi)。如果可能的話,我只想使用C#和WinForms。我寧愿不調(diào)用OpenGL或執(zhí)行某些簡(jiǎn)單任務(wù)。這是我到目前為止的代碼: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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

我在這里檢查了代碼,都遇到了問(wèn)題。


最好的方法是:


    /// <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;

    }


查看完整回答
反對(duì) 回復(fù) 2019-10-09
?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊

接受的答案對(duì)我的項(xiàng)目不起作用。我最終使用了在這里找到的代碼。


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;

}

查看完整回答
反對(duì) 回復(fù) 2019-10-09
  • 3 回答
  • 0 關(guān)注
  • 817 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)