<p>在数学中,需要将椭圆表示为二元二次方程,然后求解它。我找到一个<a href="http://maptools.home.comcast.net/~maptools/BivariateQuadratics.pdf" rel="noreferrer">doucument</a>。所有计算都在文档中,但用Python实现可能需要一段时间。</p>
<p>因此,另一种方法是将椭圆近似为多段线,并使用shapely查找交点,下面是代码:</p>
<pre><code>import numpy as np
from shapely.geometry.polygon import LinearRing
def ellipse_polyline(ellipses, n=100):
t = linspace(0, 2*np.pi, n, endpoint=False)
st = np.sin(t)
ct = np.cos(t)
result = []
for x0, y0, a, b, angle in ellipses:
angle = np.deg2rad(angle)
sa = np.sin(angle)
ca = np.cos(angle)
p = np.empty((n, 2))
p[:, 0] = x0 + a * ca * ct - b * sa * st
p[:, 1] = y0 + a * sa * ct + b * ca * st
result.append(p)
return result
def intersections(a, b):
ea = LinearRing(a)
eb = LinearRing(b)
mp = ea.intersection(eb)
x = [p.x for p in mp]
y = [p.y for p in mp]
return x, y
ellipses = [(1, 1, 2, 1, 45), (2, 0.5, 5, 1.5, -30)]
a, b = ellipse_polyline(ellipses)
x, y = intersections(a, b)
plot(x, y, "o")
plot(a[:,0], a[:,1])
plot(b[:,0], b[:,1])
</code></pre>
<p>以及输出:</p>
<p><img src="https://i.stack.imgur.com/IQieb.png" alt="enter image description here"/></p>
<p>在我的电脑上大约需要1.5毫秒</p>