计算polygon面积和判断顺逆时针方向的方法

本文介绍了使用Shapely库计算不规则多边形面积的方法,包括逆时针坐标系下的多边形调整为顺时针,以及通过分解梯形计算面积。提供了Python代码示例,适用于处理地理空间数据和计算机图形学中的面积计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.利用shapely求polygon面积

  import shapely
    from shapely.geometry import Polygon, MultiPoint  # 多边形
    # box1 = [2, 0, 4, 2, 2, 4, 0, 2, 0, 0]
    box1 = [2, 0, 4, 2, 2, 4, 0, 2, 2, 2]
    poly_box1 = Polygon(np.array(box1).reshape(-1,2))
    print(poly_box1)
    print(poly_box1.area)

二.逆时针调整为顺时针

1.四个点 

def polygon_area1():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针
    poly = np.array([[2, 0],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    poly_h = poly.shape[0]
    edge = []
    for i in range(poly_h - 1):
        edge.append((poly[i][0] - poly[i+1][0])*(poly[i][1] + poly[i + 1][1]))
    edge.append((poly[poly_h-1][0] - poly[0][0])*(poly[poly_h-1][1]+poly[0][1]))
    # edge = [
    #     (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
    #     (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
    #     (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
    #     (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
    # ]
    area = np.sum(edge) / 2.
    print('=====The first way======')
    print('area:', area)
    if area <0:
        index = [0] + [i for i in range(poly_h - 1, 0, -1)]
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
def polygon_area2():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针
    poly = np.array([[2, 0],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    edge = []
    poly_h = poly.shape[0]
    for i in range(poly_h-1):
        edge.append(poly[i][0] * poly[i+1][1] - poly[i+1][0] * poly[i][1])
    edge.append(poly[poly_h-1][0] * poly[0][1] - poly[0][0] * poly[poly_h-1][1])
    print('=====The second way======')
    # print('edge:', edge)
    area = np.sum(edge)/2.
    print('area:', area)
    #如果是逆时针 调整为顺时针
    if area < 0:
        index = [0] + [i for i in range(poly_h-1, 0, -1)]
        # print(index)
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
if __name__ == '__main__':
    # show_image()
    # compute_IOU_()
    polygon_area1()
    polygon_area2()

2.多个点

def polygon_area1():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针 凹多边形
    print('凹多边形')
    poly = np.array([[2, 0],
                     [2, 2],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    # 逆时针 凸多边形
    # print('凸多边形')
    # poly = np.array([[2, 0],
    #                  [0, 0],
    #                  [0, 2],
    #                  [2, 4],
    #                  [4, 2]])
    poly_h = poly.shape[0]
    edge = []
    for i in range(poly_h - 1):
        edge.append((poly[i][0] - poly[i+1][0])*(poly[i][1] + poly[i + 1][1]))
    edge.append((poly[poly_h-1][0] - poly[0][0])*(poly[poly_h-1][1]+poly[0][1]))
    # edge = [
    #     (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
    #     (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
    #     (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
    #     (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
    # ]
    area = np.sum(edge) / 2.
    print('=====The first way======')
    print('area:', area)
    if area <0:
        print('此为逆时针,调整为顺时针')
        index = [0] + [i for i in range(poly_h - 1, 0, -1)]
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
def polygon_area2():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针 凹多边形
    print('凹多边形')
    poly = np.array([[2, 0],
                     [2, 2],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    # 逆时针 凸多边形
    # print('凸多边形')
    # poly = np.array([[2, 0],
    #                  [0, 0],
    #                  [0, 2],
    #                  [2, 4],
    #                  [4, 2]])
    edge = []
    poly_h = poly.shape[0]
    for i in range(poly_h-1):
        edge.append(poly[i][0] * poly[i+1][1] - poly[i+1][0] * poly[i][1])
    edge.append(poly[poly_h-1][0] * poly[0][1] - poly[0][0] * poly[poly_h-1][1])
    print('=====The second way======')
    # print('edge:', edge)
    area = np.sum(edge)/2.
    print('area:', area)
    #如果是逆时针 调整为顺时针
    if area < 0:
        print('此为逆时针,调整为顺时针')
        index = [0] + [i for i in range(poly_h-1, 0, -1)]
        # print(index)
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
if __name__ == '__main__':
    # show_image()
    # compute_IOU_()
    polygon_area1()
    polygon_area2()

           

三.计算不规则多边形面积

分解成梯形,面积依次相加

import numpy as np
points = np.array([[0.72, 2.28],
                   [2.66, 4.71],
                   [5,	 3.5],
                   [3.63, 2.52],
                   [4,	1.6],
                   [1.9,	1]])
print(points.shape)
h, w = points.shape
areas = []
for i in range(0, points.shape[0]):
    area = (points[(i+1) % h][-1] + points[i][-1])*(points[(i+1) % h][0] - points[i][0])/2
    areas.append(area)
    print(points[i])
    print(points[(i+1) % h])
print('==areas:', areas)
print(sum(areas))

 

参考:https://www.cnblogs.com/TenosDoIt/p/4047211.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值