1
這比JS問題更具數學意義,但我希望你能幫助我。用JS創建多邊形的路標
我試圖圍繞多邊形創建航點。它應該能夠在上行一行。我需要觀察以下情況下(真表示方式是不可能的):
在我的劇本,我有問題,在同一時間來觀察的情況下2和7的情況下。多邊形是一個包含5個對象(點)的數組。 A和B是紅線的點。在這裏你可以找到我的jsFiddle。
// check if point is in polygon
var intersectLinePolygon = function(A, B, poly){
var result = false;
for (var i = 0; i < poly.length; i++){
var C = { 'x':poly[i].x, 'y':poly[i].y };
var D = {};
// if it's not the last point, take the next
if (i != poly.length-1){ D.x = poly[i+1].x; D.y = poly[i+1].y; }
// if it's the last point, take the first
else { D.x = poly[0].x; D.y = poly[0].y; }
if (intersectLineLine(A, B, C, D)){ result = true; }
}
return result;
};
// check if there is an intersection between two lines
var intersectLineLine = function(A, B, C, D){
if (
(B.x == C.x && B.y == C.y) ||
(B.x == D.x && B.y == D.y) ||
(A.x == C.x && A.y == C.y) ||
(A.x == D.x && A.y == D.y)
){ return false; }
else { return (ccw(A,C,D) != ccw(B,C,D) && ccw(A,B,C) != ccw(A,B,D)); }
};
// helper function for intersectLineLine
var ccw = function(A, B, C){ return ((C.y-A.y)*(B.x-A.x) > (B.y-A.y)*(C.x-A.x)); };
總之,你需要一個算法來測試,如果段相交的非凸多邊形? – leonbloy