判断一个坐标点是否在已知4个顶点的四边形范围内

网上抄的 记录一下
        public bool pInQuadrangle(PointF a, PointF b, PointF c, PointF d, PointF p)
        {
            double dTriangle   = triangleArea(a, b, p) + triangleArea(b, c, p) + triangleArea(c, d, p) + triangleArea(d, a, p);
            double dQuadrangle = triangleArea(a, b, c) + triangleArea(c, d, a);// s5 + s6;
            if(Math.Abs(dTriangle - dQuadrangle)<1)    //说明理论上应该是相等的,但因为计算本身的原因可能会有细微不同,所以选择小于1,根据实际情况来设置
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        double dis(PointF p1, PointF p2)
        {
            return Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
        }

        //计算三角形面积
        double triangleArea(PointF p1, PointF p2, PointF p3)
        {

            double a = dis(p1, p2);
            double b = dis(p2, p3);
            double c = dis(p3, p1);
            double p = (a + b + c) * 0.5;
            return Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        }

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Marzipano 3D 场景中判断一个点是否在给定区域,可以采用射线法或多边形顶点法。 射线法:通过 Marzipano 3D 场景中的 API 获取到需要判断区域的坐标系,然后获取鼠标击事件的坐标,使用射线与区域的边相交的次数来判断是否在区域内。具体实现方法如下: ```javascript viewer.addEventListener('click', function(event) { const coords = viewer.view().screenToCoordinates({x: event.clientX, y: event.clientY}); const isInside = isPointInsidePoly(coords, vertices); if (isInside) { // 在区域内部 } else { // 在区域外部 } }); function isPointInsidePoly(point, vertices) { let inside = false; const n = vertices.length; for (let i = 0, j = n - 1; i < n; j = i++) { const vi = vertices[i], vj = vertices[j]; const intersect = ((vi.y > point.y) != (vj.y > point.y)) && (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x); if (intersect) inside = !inside; } return inside; } ``` 多边形顶点法:同样通过 Marzipano 3D 场景中的 API 获取到需要判断区域的坐标系,然后获取鼠标击事件的坐标,使用多边形的顶点连接起来形成一系列边,判断这个是否在这些边的左侧。具体实现方法如下: ```javascript viewer.addEventListener('click', function(event) { const coords = viewer.view().screenToCoordinates({x: event.clientX, y: event.clientY}); const isInside = isPointInsidePoly(coords, vertices); if (isInside) { // 在区域内部 } else { // 在区域外部 } }); function isPointInsidePoly(point, vertices) { let inside = false; const n = vertices.length; for (let i = 0, j = n - 1; i < n; j = i++) { const vi = vertices[i], vj = vertices[j]; const intersect = ((vi.y > point.y) != (vj.y > point.y)) && (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x); if (intersect) inside = !inside; } return inside; } ``` 以上是两种常见的方法,具体实现可以根据实际情况选择适合的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值