javascript - 计算两个旋转矩形/多边形的碰撞和交集面积

标签 javascript collision-detection turfjs

  • 我想计算两个多边形(旋转矩形)的碰撞面积。
  • 我想计算 polyA 的面积位于碰撞区域 (%) 中。

最佳答案

turf.js (浏览器和节点的高级地理空间分析)提供 turf-intersectturf-area包。这些可用于计算两个多边形的碰撞和相交面积。

在草皮中,矩形(多边形)是使用特征来描述的,例如五边形的描述:

var polyA;

polyA = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.801742, 45.48565],
                [-122.801742, 45.60491],
                [-122.584762, 45.60491],
                [-122.584762, 45.48565],
                [-122.801742, 45.48565]
            ]
        ]
    }
};

计算两个多边形(旋转矩形)的碰撞面积

turf.intersect 用于描述特征(多边形)的交集,例如

var polyA,
    polyB,
    polyAPolyBIntersection;

polyA = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.801742, 45.48565],
                [-122.801742, 45.60491],
                [-122.584762, 45.60491],
                [-122.584762, 45.48565],
                [-122.801742, 45.48565]
            ]
        ]
    }
};

polyB = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.520217, 45.535693],
                [-122.64038, 45.553967],
                [-122.720031, 45.526554],
                [-122.669906, 45.507309],
                [-122.723464, 45.446643],
                [-122.532577, 45.408574],
                [-122.487258, 45.477466],
                [-122.520217, 45.535693]
            ]
        ]
    }
};

polyAPolyBIntersection = turf.intersect(polyA, polyB);

console.log('polyAPolyBIntersection', polyAPolyBIntersection);
<script src='//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js'></script>

计算polyA的面积在碰撞区域中(%)。

polyAPolyBIntersection 描述了 polyApolyB 的交集。为了计算polyA的面积在碰撞区域(%)中,我们需要计算polyApolyAPolyBIntersection的碰撞。然后计算产生的碰撞的面积和polyA

var polyA,
    polyAArea,
    polyAPolyBIntersection,
    polyAPolyBIntersectionPolyAIntersection,
    polyAPolyBIntersectionPolyAIntersectionArea;

polyA = {
    type: 'Feature',
    properties: {
        fill: '#0f0'
    },
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.801742, 45.48565],
                [-122.801742, 45.60491],
                [-122.584762, 45.60491],
                [-122.584762, 45.48565],
                [-122.801742, 45.48565]
            ]
        ]
    }
};

// Using "intersection" result from the previous example.

polyAPolyBIntersection = {
    type: 'Feature',
    properties: {},
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.584762,45.545508794628965],
                [-122.584762,45.48565],
                [-122.68902729894835,45.48565],
                [-122.669906,45.507309],
                [-122.720031,45.526554],
                [-122.64038,45.553967],
                [-122.584762,45.545508794628965]
            ]
        ]
    }
};

// Calculate intersection between polyAPolyBIntersection and polyA.

polyAPolyBIntersectionPolyAIntersection = turf.intersect(polyAPolyBIntersection, polyA);

// Calculate area (in meters) of polyA and polyAPolyBIntersectionPolyAIntersection.
// Note that it does not matter what units we use since we want to calculate the relative intersection size (%).

polyAArea = turf.area(polyA);
polyAPolyBIntersectionPolyAIntersectionArea = turf.area(polyAPolyBIntersectionPolyAIntersection);

// Calculate how much of polyA is covered.

polyACoverage = polyAPolyBIntersectionPolyAIntersectionArea / polyAArea;

console.log('polyACoverage', polyACoverage);
<script src='//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js'></script>

polyACoverage 为 0.2533680217675428,这意味着大约 25% 的 polyA 位于 polyAPolyBIntersection 中。

关于javascript - 计算两个旋转矩形/多边形的碰撞和交集面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104083/

相关文章:

javascript - 为什么 Logger.log 看不到第二个参数而 console.log 却看到?

javascript - 我怎样才能使文本区域内容中的外观值取决于所选值

数组中的 Java 对象 - 碰撞检测

compiler-errors - pygame炮弹碰撞检测

javascript - 如何在传单中查找相邻的多边形

leaflet - 在折线上创建缓冲区 - Leaflet

javascript - 如何在javascript中按两个名称对数组进行排序?

javascript - 正则表达式在javaScript中将数字与浮点相匹配

java - 基于图 block 的碰撞;角部碰撞问题

javascript - Turf js绘制具有中心点和半径的完美正方形