本文介绍了计算两个旋转的矩形/多边形的碰撞和相交面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我想计算两个多边形(旋转的矩形)的碰撞面积.
  • 我想计算polyA的哪个区域在碰撞区域(%).
  • I want to calculate collision area of two polygons (rotated rectangles).
  • I want to calculate what area of the polyA is in the collision area (%).

推荐答案

turf.js (针对浏览器的高级地理空间分析和节点)提供草坪相交草皮区域包.这些可用于计算两个多边形的碰撞和相交面积.

turf.js (advanced geospatial analysis for browsers and node) provides turf-intersect and turf-area packages. These can be used to calculate collision and area of intersection of two polygons.

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

In turf, rectangles (polygons) are described using features, e.g. a description of a pentagon:

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用于根据要素(多边形)来描述交集,例如

turf.intersect is used to describe intersection in terms of a feature (polygon), e.g.

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的哪个区域在碰撞区域(%).

To calculate what area of the polyA is in the collision area (%).

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

polyAPolyBIntersection describes the intersection of polyA and polyB. To calculate what area of the polyA is in the collision area (%), we need to calculate the collision of polyA and polyAPolyBIntersection. Then calculate area of the resulting collision and 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,这意味着polyA的约25%位于polyAPolyBIntersection中.

polyACoverage is 0.2533680217675428, which means that ~25% of polyA is in the polyAPolyBIntersection.

这篇关于计算两个旋转的矩形/多边形的碰撞和相交面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 19:54