[英]Calculate if point coordinates is inside polygon with concave and convex angles ? Javascript
對不起,我的英語,但我是意大利人...我想創建一個代碼,計算點(帶有兩個坐標)是否在具有凹凸角的多邊形內。 我嘗試了一些代碼,因為太難了。
var polygon = [
[71.99999994,38.999999714],
[71.000000057,38.999999714],
[69.999999998,38.999999714],
[69.999999998,38.000000007],
[68.999999939,38.000000007],
[67.99999988,38.000000007],
[67.99999988,38.999999714],
[67.99999988,39.999999597],
[68.999999939,39.999999597],
[68.999999939,41.000000008],
[69.999999998,41.000000008],
[71.000000057,41.000000008],
[71.99999994,41.000000008],
[71.99999994,39.999999597],
[71.99999994,38.999999714]
];
var point= [68,38.5];
我希望你能幫助我...
非常感謝
該算法基於以下站點: http : //alienryderflex.com/polygon/
它超快,可以處理各種多邊形類型。 我已經將其調整為javascript和您的數據結構。
function IsPointInPolygon(poly_array, test_point) {
var inside = false;
var test_x = test_point[0];
var test_y = test_point[1];
for(var i=0; i<(poly_array.length-1); i++) {
var p1_x = poly_array[i][0];
var p1_y = poly_array[i][1];
var p2_x = poly_array[i+1][0];
var p2_y = poly_array[i+1][1];
if((p1_y<test_y && p2_y>=test_y) || (p2_y<test_y && p1_y>=test_y)) { // this edge is crossing the horizontal ray of testpoint
if((p1_x+(test_y-p1_y)/(p2_y-p1_y)*(p2_x-p1_x)) < test_x) { // checking special cases (holes, self-crossings, self-overlapping, horizontal edges, etc.)
inside=!inside;
}
}
}
return inside;
}
您可以使用變量直接調用它:
if(IsPointInPolygon(polygon, point)) {
alert('Inside');
}
else {
alert('Outside');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.