检查多边形内的点是否不是很精确 - Javascript

时间:2013-12-26 19:53:27

标签: javascript

我有一个函数,检查点是否在javascript中的多边形内,但是这个函数不是很精确,因为我有5个多边形相互粘连,当我点击多边形1时,函数告诉我多边形2是点击,因为他们非常接近。

所以,这是我的功能:

 function isInsidePolygon(testx, testy, face, nvert){
    var coords = getFaceCoords(face);
    var vertx = [ coords[0][0], coords[1][0], coords[2][0], coords[3][0] ];
    var verty = [ coords[0][1], coords[1][1], coords[2][1], coords[3][1] ]; 

    var i, j, c = false;
    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;
}

为了更好地解释问题,我将发布函数“getFaceCoords()”,看看:

// Retorna as coordenadas da face

function getFaceCoords(face) {
    var coords = new Object();

    // todos os pontos do dente
    var x1y1 = [ 34, 27 ];
    var x2y2 = [ 94, 27 ];
    var x3y3 = [ 94, 89 ];
    var x4y4 = [ 34, 89 ];
    var x5y5 = [ 47, 41 ];
    var x6y6 = [ 82, 41 ];
    var x7y7 = [ 82, 75 ];
    var x8y8 = [ 46, 75 ];

    if (face == 'DISTAL') {
        coords = [ x1y1, x5y5, x8y8, x4y4 ];
    } else if (face == 'LINGUAL') {
        coords = [ x1y1, x2y2, x6y6, x5y5 ];
    } else if (face == 'MESIAL') {
        coords = [ x2y2, x3y3, x7y7, x6y6 ];
    } else if (face == 'VESTIBULAR') {
        coords = [ x3y3, x4y4, x8y8, x7y7 ];
    } else if (face == 'OCLUSAL') {
        coords = [ x5y5, x6y6, x7y7, x8y8 ];
    }

    // Configura os pontos conforme parametrização
    for ( var i = 0; i < coords.length; i++) {
        coords[i][0] = coords[i][0] + startPoint;
        coords[i][1] = coords[i][1] + startPoint;
    }

    return coords;

}

“startPoint”变量的值为“-25”。如果你在笛卡尔平面上绘制这个多边形,你会发现它非常接近,卡住了。

所以唯一的问题是精度差。请参阅我的多边形,区域5:http://www.odontoblogia.com.br/wp-content/uploads/2012/04/face-dente.jpg

0 个答案:

没有答案