What is the area of this circle that hangs out of the rectangle (imagine the rectangle stretches out to (infinity, infinity) on both ends), and how can I generalize this to any circle of any radius as well as any rectangle of any dimensions.
这个圆在矩形之外的面积是多少(想象这个矩形在两端延伸到(无穷大,无穷大)),我如何将它推广到任何半径的圆以及任何维度的矩形。
I have a coding problem in which I need to calculate what percentage of the circle is in the rectangle for a number of different circles, I am struggling with the code when the corner of the rectangle is covered. Without the corner issue, I could simply see how far away each protruding edge is, then use some circle/trig math to find the area of each outside bit. But once the corner is covered, the code overcounts the corner bit: in the extreme case where the circle is at (0,0) it counts the left half as out and the top half as out, and concludes that the whole circle is out. I've drawn diagrams but I've failed to find a way to discover the overcounted area. I originally tried finding the dimensions of the outer triangle, but I can't figure out that either.
我有一个编码问题,在这个问题中,我需要计算一些不同的圆在矩形中的百分比,当矩形的角被覆盖时,我正在与代码作斗争。没有角的问题,我可以简单地看到每个突出的边缘有多远,然后使用一些圆/三角数学来找到每个外部位的面积。但是,一旦角被覆盖,代码就会过多地计算角位:在圆位于(0,0)的极端情况下,它将左半部分计为out,上半部分计为out,并得出整个圆为out的结论。我画了图表,但找不到发现多算面积的方法。我最初试着找出外三角形的尺寸,但我也想不出来。
I know there is an answer, because there's enough information for only one possibility, but I can't figure out how.
我知道答案是有的,因为只有一种可能性有足够的信息,但我不知道如何解决。
This is the code I have so far, it just counts how much area that the segments perpendicular to the axes cover, thus overcounts when these segments overlap.
xbounds is a tuple (low, high) for the width of the rectangle and ybounds1 (low, high) is a tuple for the height of the rectangle these just say where the rectangle starts and ends on the x and y axes, in practice, the first value is 0 because the rectangle starts at zero and the second values are very large numbers.
Range is the radius of the circle.
Pos is an imaginary number representing the origin where real is the x coord and imag is the y coord.
这是我到目前为止的代码,它只计算垂直于轴的线段覆盖的面积,因此当这些线段重叠时会过多计算。Xbound是矩形宽度的元组(低,高),yrangs1(低,高)是矩形高度的元组,它们只表示矩形在x轴和y轴上的开始和结束位置,在实践中,第一个值是0,因为矩形从零开始,第二个值是非常大的数字。范围是圆的半径。POS是代表原点的虚数,其中REAL是x坐标,IMAG是Y坐标。
r2 = range*range
outarea = 0
if (b:= -pos.real+range+xbounds[0])>0:
theta = 2*acos(1-b/range)
outarea=0.5*r2*(theta-sin(theta))
if (b:= pos.real+range-xbounds[1])>0:
theta = 2*acos(1-b/range)
outarea+=0.5*r2*(theta-sin(theta))
if (b:= -pos.imag+range+ybounds[0])>0:
theta = 2*acos(1-b/range)
outarea+=0.5*r2*(theta-sin(theta))
if (b:= pos.imag+range-ybounds[1])>0:
theta = 2*acos(1-b/range)
outarea+=0.5*r2*(theta-sin(theta))
更多回答
This is a math problem, not a programming problem.
这是一道数学题,不是编程题。
I considered posting on mathoverflow, but all the posts i saw there were highly theoretical and abstract, which made my geometry problem feel out of place. I felt that this forum was better suited for trying to find a good answer, as i was looking for a python code snippet i could use to compute the area.
我考虑在MathOverflow上发帖,但我在那里看到的所有帖子都是高度理论性和抽象的,这让我的几何问题感觉格格不入。我觉得这个论坛更适合试图找到一个好的答案,因为我正在寻找一个可以用来计算面积的Python代码片段。
Thank you for the info, I'll keep that in mind
谢谢你的信息,我会记住的
我是一名优秀的程序员,十分优秀!