裁剪圆的面积的几何估计

问题描述 投票:1回答:1

我有一个矩形框和一个圆,其中心和半径是随机生成的。中心总是位于框架的边界内,如图所示:

enter image description here

我需要估算位于框架内的圆的一部分的面积。目前,我使用了一个可以正常工作的简单蒙特卡洛估计,但我想将此与该区域的精确几何估计进行比较。

是否有执行此操作的库和/或方法?我对condapip可以安装的几乎所有东西都持开放态度。


import numpy as np
import matplotlib.pyplot as plt

def circFrac(cx, cy, rad, x0, x1, y0, y1, N_tot=100000):
    """
    Use Monte Carlo to estimate the fraction of the area of a circle centered
    in (cx, cy) with a radius of 'rad', that is located within the frame given
    by the limits 'x0, x1, y0, y1'.
    """

    # Source: https://stackoverflow.com/a/50746409/1391441
    r = rad * np.sqrt(np.random.uniform(0., 1., N_tot))
    theta = np.random.uniform(0., 1., N_tot) * 2 * np.pi
    xr = cx + r * np.cos(theta)
    yr = cy + r * np.sin(theta)

    # Points within the circle that are within the frame.
    msk_xy = (xr > x0) & (xr < x1) & (yr > y0) & (yr < y1)

    # The area is the points within circle and frame over the points within
    # circle.
    return msk_xy.sum() / N_tot


for _ in range(10):

    # Random (x, y) limits of the frame
    x0, y0 = np.random.uniform(0., 500., 2)
    x1, y1 = np.random.uniform(500., 1000., 2)

    # Random center coordinates *always* within the frame
    cx = np.random.uniform(x0, x1)
    cy = np.random.uniform(y0, y1)
    # Random radius
    rad = np.random.uniform(10., 500)

    frac = circFrac(cx, cy, rad, x0, x1, y0, y1)

    plt.xlim(x0, x1)
    plt.ylim(y0, y1)
    circle = plt.Circle((cx, cy), rad, fill=False)
    plt.gca().add_artist(circle)
    plt.scatter(
        cx, cy, marker='x', c='r', label="({:.0f}, {:.0f}), r={:.0f}".format(
            cx, cy, rad))
    plt.legend()
    plt.title("Fraction of circle inside frame: {:.2f}".format(frac))
    plt.axes().set_aspect('equal')
    plt.show()
python area montecarlo geometry-surface
1个回答
0
投票

您可以使用shapely

import shapely.geometry as g

xr,yr,r = 0,0,5
circle = g.Point(xr,yr).buffer(r)

x1,y1,x2,y2 = -1,-2,3,5
rectangle = g.Polygon([(x1,y1),(x1,y2),(x2,y2),(x2,y1)])

intersection = rectangle.intersection(circle)
intersection.area

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.