javascript - 计算点坐标是否在具有凹 Angular 和凸 Angular 的多边形内? Javascript

标签 javascript arrays geometry point-in-polygon

抱歉我的英语不好,但我是意大利人... 我想创建一个代码来计算点(具有两个坐标)是否在具有凹 Angular 和凸 Angular 的多边形内。我尝试了一些代码,因为太难了..

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

关于javascript - 计算点坐标是否在具有凹 Angular 和凸 Angular 的多边形内? Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42457842/

相关文章:

c - 使用三角函数绘制实心圆

python - 如何检查笛卡尔坐标是否有效构成矩形?

python - 计算测地网格三角点的经纬度点(最好用python)

javascript - 替换特定开始结束位置的字符串 javascript

javascript - 图片加载后何时触发滚动

javascript - 如何重新定位 dz-remove 链接 Dropzone.js

java - 计数出现次数未添加

javascript - 如何在 onSubmit 调用的函数内使用状态?

python - 查找一个数组中的相同值以及另一个数组中的等价对

java - 压缩 double 组中的值(不是内存压缩)