sme2457 2021-06-16 21:02 采纳率: 0%
浏览 370

python如何计算n个圆面积的并集?

1.有n个圆相互重叠

2.圆心已知(坐标集),半径相同

3.如何用python求得重叠之后的圆面积的并集

4.多圆重叠如图所示(蓝色区域)

  • 写回答

3条回答 默认 最新

  • sme2457 2021-07-15 12:12
    关注

    自问自答,interesting

    from random import uniform
    from math import sqrt
    from matplotlib import pyplot as plt
    
    # 投点总数
    total = 100000
    
    # 统计点数初始值
    inn = out = 0.0
    
    # 三个圆心坐标
    location = [[123.75, 31.6], [123.71, 32.3], [124.09, 31.66]]
    num_ship = len(location)
    
    # 矩形区域坐标
    rect = {'left_bottom': [123.5, 31.5], 'right_up': [125, 32.5],
            'line_lon': [123.5, 125], 'line_lat': [31.5, 32.5],
            'line_lat_up': [32.5, 32.5], 'line_lat_down': [31.5, 31.5],
            'line_lon_left': [123.5, 123.5], 'line_lon_right': [125, 125]}
    
    # 船舶节点坐标,圆心坐标
    center_x = [i[0] for i in location]
    center_y = [i[1] for i in location]
    
    # 圆外坐标列表
    x_out_list = []
    y_out_list = []
    
    for j in range(total):  # 先取随机点坐标
        x = uniform(rect['left_bottom'][0], rect['right_up'][0])
        y = uniform(rect['left_bottom'][1], rect['right_up'][1])
    
        for i in range(len(center_x)):  # 再取圆心坐标
            ox = center_x[i]
            oy = center_y[i]
            judge_range = sqrt((x - ox) * (x - ox) + (y - oy) * (y - oy))  # 计算圆半径公式
            if judge_range < 0.5:  # 判断随机点是否在圆范围内,圆半径为0.5
                break  # 如果在,退出循环,继续与下一个圆心对比
            else:  # 如果不在,添加到圆外列表,并计数
                if i == len(center_x) - 1:
                    x_out_list.append(x)
                    y_out_list.append(y)
                    out += 1.0  # 圆外节点计数
                continue
    inn = total - out
    print(total, inn, out)
    print(inn / total)  # 重叠区域投点比例
    
    fig = plt.figure(dpi=200)
    ax = fig.add_subplot(111)
    plt.xlim(123.5, 125)
    plt.ylim(31.5, 32.5)
    # plt.scatter(x_in_list, y_in_list, color="r")
    # 绘图,蓝色区域为圆外面积
    # todo 反向填充颜色,优化绘图
    plt.scatter(x_out_list, y_out_list, color="blue")
    # plt.savefig("./img/area_proportion")
    plt.show()
    

    效果图,白色为多圆重叠区域,蓝色为圆外区域,可用矩形面积*比例得到重叠面积
    img

    为对比效果,上其他圆心图形

    img

    img

    评论

报告相同问题?

悬赏问题

  • ¥15 嵌入式设备网口down后再up时不能link?
  • ¥15 关于区块链和边缘计算
  • ¥15 做一个简单项目,用Python分析共享单车的数据
  • ¥20 在使用CESM2.2.0模型进行case.submit过程中出现如下错误(关键词-Map)
  • ¥15 有办法改变通过wifi进入的网站的设置吗
  • ¥15 所以到底怎么算!算到凌晨五点都算不出来!
  • ¥15 label_studio
  • ¥15 请教如何phython发邮件
  • ¥15 linux系统安装问题
  • ¥15 路径规划如何采用矢量法让他们尽量在一个方向