【问题标题】:Geometric estimation of area of cropped circle裁剪圆面积的几何估计
【发布时间】:2020-02-24 01:41:00
【问题描述】:

我有一个矩形框架和一个圆心和半径随机生成的圆。中心始终位于框架的范围内,如图所示:

我需要估计位于框架内的圆部分的面积。目前我采用了一个简单的蒙特卡洛估计,效果不错,但我想将其与该区域的精确几何估计进行比较。

是否有库和/或方法可以做到这一点?我对几乎所有可以使用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()

【问题讨论】:

  • 这可能是一个更适合 Math SE 的问题。我的数学能力不如以前那么强了,但我有一种预感,微积分会给我们一个准确的答案。
  • 是的,我相信它会,但我希望在某些库中已经实现了现成的解决方案。
  • +1 用于发布到 Math SE。我认为可以通过将圆分成扇区和三角形来解决几何问题,但是需要考虑很多不同的情况。这是一个图书馆需要解决的非常具体的问题。
  • stackoverflow.com/questions/622287/… 可能会给你一些想法。尽管被标记为 Java,但最佳答案似乎与语言无关。
  • 谢谢凯文(很抱歉回答得太晚了!)我会检查这些方法并尝试提出一个 Python 实现。

标签: python area montecarlo geometry-surface


【解决方案1】:

您可以为此使用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

【讨论】:

  • 这是一个很棒的库,我应该经常使用它。谢谢你瓦西里!
猜你喜欢
  • 1970-01-01
  • 2016-12-13
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多