2013-05-17 40 views
1

这比JS问题更具数学意义,但我希望你能帮助我。用JS创建多边形的路标

我试图围绕多边形创建航点。它应该能够在上行一行。我需要观察以下情况下(表示方式是不可能的):

polygon   在我的剧本,我有问题,在同一时间来观察的情况下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)); }; 
+1

总之,你需要一个算法来测试,如果段相交的非凸多边形? – leonbloy

回答