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

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

點(diǎn)多邊形算法

點(diǎn)多邊形算法

慕碼人2483693 2019-10-08 14:27:10
我看到以下算法可以檢查該鏈接中的點(diǎn)是否在給定的多邊形中:int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy){  int i, j, c = 0;  for (i = 0, j = nvert-1; i < nvert; j = i++) {    if ( ((verty[i]>testy) != (verty[j]>testy)) &&     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )       c = !c;  }  return c;}我嘗試了這種算法,它實(shí)際上非常完美。但是可惜的是,在花了一些時(shí)間來(lái)理解它之后,我無(wú)法很好地理解它。因此,如果有人能夠理解此算法,請(qǐng)向我解釋一下。謝謝。
查看完整描述

3 回答

?
料青山看我應(yīng)如是

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

Chowlett在各種方式,形狀和形式上都是正確的。該算法假定,如果您的點(diǎn)在多邊形的線上,則該點(diǎn)在外面-在某些情況下,這是錯(cuò)誤的。將兩個(gè)'>'運(yùn)算符更改為'> ='并將'<'更改為'<='將解決此問題。


bool PointInPolygon(Point point, Polygon polygon) {

  vector<Point> points = polygon.getPoints();

  int i, j, nvert = points.size();

  bool c = false;


  for(i = 0, j = nvert - 1; i < nvert; j = i++) {

    if( ( (points[i].y >= point.y ) != (points[j].y >= point.y) ) &&

        (point.x <= (points[j].x - points[i].x) * (point.y - points[i].y) / (points[j].y - points[i].y) + points[i].x)

      )

      c = !c;

  }


  return c;

}


查看完整回答
反對(duì) 回復(fù) 2019-10-08
?
楊__羊羊

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

這可能與在實(shí)際代碼中解釋光線跟蹤算法所獲得的結(jié)果一樣詳細(xì)。它可能未進(jìn)行優(yōu)化,但必須始終在系統(tǒng)完全掌握之后才能進(jìn)行優(yōu)化。


    //method to check if a Coordinate is located in a polygon

public boolean checkIsInPolygon(ArrayList<Coordinate> poly){

    //this method uses the ray tracing algorithm to determine if the point is in the polygon

    int nPoints=poly.size();

    int j=-999;

    int i=-999;

    boolean locatedInPolygon=false;

    for(i=0;i<(nPoints);i++){

        //repeat loop for all sets of points

        if(i==(nPoints-1)){

            //if i is the last vertex, let j be the first vertex

            j= 0;

        }else{

            //for all-else, let j=(i+1)th vertex

            j=i+1;

        }


        float vertY_i= (float)poly.get(i).getY();

        float vertX_i= (float)poly.get(i).getX();

        float vertY_j= (float)poly.get(j).getY();

        float vertX_j= (float)poly.get(j).getX();

        float testX  = (float)this.getX();

        float testY  = (float)this.getY();


        // following statement checks if testPoint.Y is below Y-coord of i-th vertex

        boolean belowLowY=vertY_i>testY;

        // following statement checks if testPoint.Y is below Y-coord of i+1-th vertex

        boolean belowHighY=vertY_j>testY;


        /* following statement is true if testPoint.Y satisfies either (only one is possible) 

        -->(i).Y < testPoint.Y < (i+1).Y        OR  

        -->(i).Y > testPoint.Y > (i+1).Y


        (Note)

        Both of the conditions indicate that a point is located within the edges of the Y-th coordinate

        of the (i)-th and the (i+1)- th vertices of the polygon. If neither of the above

        conditions is satisfied, then it is assured that a semi-infinite horizontal line draw 

        to the right from the testpoint will NOT cross the line that connects vertices i and i+1 

        of the polygon

        */

        boolean withinYsEdges= belowLowY != belowHighY;


        if( withinYsEdges){

            // this is the slope of the line that connects vertices i and i+1 of the polygon

            float slopeOfLine   = ( vertX_j-vertX_i )/ (vertY_j-vertY_i) ;


            // this looks up the x-coord of a point lying on the above line, given its y-coord

            float pointOnLine   = ( slopeOfLine* (testY - vertY_i) )+vertX_i;


            //checks to see if x-coord of testPoint is smaller than the point on the line with the same y-coord

            boolean isLeftToLine= testX < pointOnLine;


            if(isLeftToLine){

                //this statement changes true to false (and vice-versa)

                locatedInPolygon= !locatedInPolygon;

            }//end if (isLeftToLine)

        }//end if (withinYsEdges

    }


    return locatedInPolygon;

}

關(guān)于優(yōu)化的一句話:最短的(和/或最有趣的)代碼實(shí)現(xiàn)最快是不正確的。與在每次需要訪問數(shù)組時(shí)相比,從數(shù)組中讀取和存儲(chǔ)一個(gè)元素并在代碼塊的執(zhí)行過程中(可能)多次使用該元素的過程要快得多。如果陣列非常大,這一點(diǎn)尤其重要。我認(rèn)為,通過將數(shù)組的每個(gè)項(xiàng)存儲(chǔ)在一個(gè)命名良好的變量中,也可以更容易地評(píng)估其目的,從而形成更具可讀性的代碼。只是我的兩分錢


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

添加回答

舉報(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)