计算两个旋转矩形/多边形的碰撞和交叉区域

时间:2015-08-19 19:30:08

标签: javascript collision-detection turfjs

  • 我想计算两个多边形的碰撞区域(旋转的矩形)。
  • 我想计算碰撞区域中p的哪个区域(%)。

1 个答案:

答案 0 :(得分:1)

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

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

polyA

计算两个多边形(旋转矩形)的碰撞区域

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描述了polyAPolyBIntersectionpolyA的交集。要计算polyB在碰撞区域(%)中的哪个区域,我们需要计算polyApolyA的碰撞。然后计算产生的碰撞的面积和polyAPolyBIntersection

&#13;
&#13;
polyA
&#13;
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);
&#13;
&#13;
&#13;

<script src='//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js'></script>是0.2533680217675428,这意味着polyACoverage中约有25%polyA位于polyAPolyBIntersection