计算椭圆之间的相交面积-python

计算椭圆之间的相交面积-python,python,matplotlib,intersection,Python,Matplotlib,Intersection,我的目标是计算两个椭圆相交的面积。下面计算两个固定坐标之间的面积,但我希望使用一个椭圆的移动坐标来迭代这个过程,同时保持第二个椭圆固定 使用下面的命令,ellipse1将保持不变,ellipse2将使用df中X和Y的坐标 期望的输出将返回每个时间点的面积,四舍五入到小数点后2位,作为一个单独的列标记为“area” df = pd.DataFrame({ 'Time' : [1,2,3,4,5], 'X'

我的目标是计算两个椭圆相交的面积。下面计算两个固定坐标之间的面积,但我希望使用一个椭圆的移动坐标来迭代这个过程,同时保持第二个椭圆固定

使用下面的命令,
ellipse1
将保持不变,
ellipse2
将使用
df中
X
Y
的坐标

期望的输出将返回每个时间点的面积,四舍五入到小数点后2位,作为一个单独的列标记为
“area”

df = pd.DataFrame({        
    'Time' : [1,2,3,4,5],                               
    'X' : [3.0,4.5,5.0,10.0,3.0],
    'Y' : [1.0,0.0,-1.0,-2.0,-3.0],         
    })

from matplotlib import pyplot as plt
from shapely.geometry.point import Point
from shapely import affinity
from matplotlib.patches import Polygon
import numpy as np

def create_ellipse(center, lengths, angle = 0):

    """
    create a shapely ellipse. adapted from
    https://gis.stackexchange.com/a/243462
    """
    circ = Point(center).buffer(1)
    ell = affinity.scale(circ, int(lengths[0]), int(lengths[1]))
    ellr = affinity.rotate(ell, angle)

    return ellr

fig,ax = plt.subplots(figsize = (6,6))

ax.set_xlim([-20,20])
ax.set_ylim([-20,20])

ellipse1 = create_ellipse((0,0),(5,10),0)
verts1 = np.array(ellipse1.exterior.coords.xy)
patch1 = Polygon(verts1.T,color = 'red', alpha = 0.5)
ax.add_patch(patch1)

ellipse2 = create_ellipse((3.0,1.0),(5,5),0)
verts2 = np.array(ellipse2.exterior.coords.xy)
patch2 = Polygon(verts2.T,color = 'blue', alpha = 0.5)
ax.add_patch(patch2)

##compute intersect 
intersect = ellipse1.intersection(ellipse2)
verts3 = np.array(intersect.exterior.coords.xy)
patch3 = Polygon(verts3.T, facecolor = 'none', edgecolor = 'black')
ax.add_patch(patch3)

##compute intersected area
print(intersect.area)
预期产出:

   Time     X    Y   Area
0     1   3.0  1.0  56.51
1     2   4.5  0.0  46.99
2     3   5.0 -1.0  36.75
3     4  10.0 -2.0   0.00
4     5   3.0 -3.0  54.03

如果我明白你的意思,你就快到了;您只需要在数据帧中使用一种循环。即使这可能因为应用而不是非常有效,这也可以做到(更好的方法可能是跳到geopandas,但我认为这种方法对您来说更容易理解):

返回

   Time     X    Y       Area
0     1   3.0  1.0  56.518719
1     2   4.5  0.0  41.997447
2     3   5.0 -1.0  36.746078
3     4  10.0 -2.0   0.000000
4     5   3.0 -3.0  54.029912

圆是椭圆,为什么不使用两个椭圆的情况呢?不管怎样,我看这里没有问题。有什么问题?并解释你发布的代码有什么问题。这不是小事。真诚的道歉。问题现已更新,以包含更清晰的内容
   Time     X    Y       Area
0     1   3.0  1.0  56.518719
1     2   4.5  0.0  41.997447
2     3   5.0 -1.0  36.746078
3     4  10.0 -2.0   0.000000
4     5   3.0 -3.0  54.029912