亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

點多邊形算法

點多邊形算法

慕碼人2483693 2019-10-08 14:27:10
我看到以下算法可以檢查該鏈接中的點是否在給定的多邊形中: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;}我嘗試了這種算法,它實際上非常完美。但是可惜的是,在花了一些時間來理解它之后,我無法很好地理解它。因此,如果有人能夠理解此算法,請向我解釋一下。謝謝。
查看完整描述

3 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

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


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;

}


查看完整回答
反對 回復 2019-10-08
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

這可能與在實際代碼中解釋光線跟蹤算法所獲得的結果一樣詳細。它可能未進行優化,但必須始終在系統完全掌握之后才能進行優化。


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

}

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


查看完整回答
反對 回復 2019-10-08
  • 3 回答
  • 0 關注
  • 780 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號