3 回答

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

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)估其目的,從而形成更具可讀性的代碼。只是我的兩分錢
添加回答
舉報(bào)