1.有n个圆相互重叠
2.圆心已知(坐标集),半径相同
3.如何用python求得重叠之后的圆面积的并集
4.多圆重叠如图所示(蓝色区域)
1.有n个圆相互重叠
2.圆心已知(坐标集),半径相同
3.如何用python求得重叠之后的圆面积的并集
4.多圆重叠如图所示(蓝色区域)
自问自答,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()
效果图,白色为多圆重叠区域,蓝色为圆外区域,可用矩形面积*比例得到重叠面积
为对比效果,上其他圆心图形