找到两个椭圆的交点(Python)

时间:2013-03-16 04:25:00

标签: python geometry intersection shapes ellipse

我正在用Python编写一个基本的2D形状库(主要用于操作SVG图纸),我对如何有效地计算两个椭圆的交点感到茫然。

每个椭圆由以下变量(所有浮点数)定义:

c: center point (x, y)
hradius: "horizontal" radius
vradius: "vertical" radius
phi: rotation from coordinate system's x-axis to ellipse's horizontal axis

忽略椭圆相同时,可能有0到4个交点(没有交点,切线,部分重叠,部分重叠,内部切线,完全重叠)。

我找到了一些可能的解决方案:

关于如何计算交叉点的任何建议?速度(它可能需要计算很多交叉点)和优雅是主要标准。代码太棒了,但即使是一个好的方向也会有所帮助。

3 个答案:

答案 0 :(得分:12)

在数学中,你需要将椭圆表示为二元二次方程,然后求解。我找到了doucument。所有的计算都在文档中,但在Python中实现它可能需要一段时间。

所以另一种方法是将椭圆近似为折线,并使用形状来找到交点,这里是代码:

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])

和输出:

enter image description here

我的电脑上需要大约1.5毫秒。

答案 1 :(得分:3)

看着sympy我认为它拥有你需要的一切。 (我试图为你提供示例代码;不幸的是,我在使用gmpy2安装sympy而不是无用的python内置数学时失败了)

你有:

从他们的例子中,我认为绝对可以交叉两个省略号:

>>> from sympy import Ellipse, Point, Line, sqrt
>>> e = Ellipse(Point(0, 0), 5, 7)
...
>>> e.intersection(Ellipse(Point(1, 0), 4, 3))
[Point(0, -3*sqrt(15)/4), Point(0, 3*sqrt(15)/4)]
>>> e.intersection(Ellipse(Point(5, 0), 4, 3))
[Point(2, -3*sqrt(7)/4), Point(2, 3*sqrt(7)/4)]
>>> e.intersection(Ellipse(Point(100500, 0), 4, 3))
[]
>>> e.intersection(Ellipse(Point(0, 0), 3, 4))
[Point(-363/175, -48*sqrt(111)/175), Point(-363/175, 48*sqrt(111)/175),
Point(3, 0)]
>>> e.intersection(Ellipse(Point(-1, 0), 3, 4))
[Point(-17/5, -12/5), Point(-17/5, 12/5), Point(7/5, -12/5),
Point(7/5, 12/5)] 

编辑:因为一般椭圆(ax ^ 2 + bx + cy ^ 2 + dy + exy + f)在sympy中不受支持,

你应该建立方程式并自己转换它们,并使用它们的求解器来找到交点。

答案 2 :(得分:0)

您可以使用此处显示的方法:https://math.stackexchange.com/questions/864070/how-to-determine-if-two-ellipse-have-at-least-one-intersection-point/864186#864186

首先,您应该能够在一个方向上重新缩放椭圆。要做到这一点,你需要将椭圆的系数计算为圆锥截面,重新缩放,然后恢复椭圆的新几何参数:中心,轴,角度。

然后你的问题减少到找到从椭圆到原点的距离。要解决这个问题,您需要进行一些迭代。这是一个可能的自包含实现...

from math import *

def bisect(f,t_0,t_1,err=0.0001,max_iter=100):
    iter = 0
    ft_0 = f(t_0)
    ft_1 = f(t_1)
    assert ft_0*ft_1 <= 0.0
    while True:
        t = 0.5*(t_0+t_1)
        ft = f(t)
        if iter>=max_iter or ft<err:
            return t
        if ft * ft_0 <= 0.0:
            t_1 = t
            ft_1 = ft
        else:
            t_0 = t
            ft_0 = ft
        iter += 1

class Ellipse(object):
    def __init__(self,center,radius,angle=0.0):
        assert len(center) == 2
        assert len(radius) == 2
        self.center = center
        self.radius = radius
        self.angle = angle

    def distance_from_origin(self):
        """                                                                           
        Ellipse equation:                                                             
        (x-center_x)^2/radius_x^2 + (y-center_y)^2/radius_y^2 = 1                     
        x = center_x + radius_x * cos(t)                                              
        y = center_y + radius_y * sin(t)                                              
        """
        center = self.center
        radius = self.radius

        # rotate ellipse of -angle to become axis aligned                             

        c,s = cos(self.angle),sin(self.angle)
        center = (c * center[0] + s * center[1],
                  -s* center[0] + c * center[1])

        f = lambda t: (radius[1]*(center[1] + radius[1]*sin(t))*cos(t) -
                       radius[0]*(center[0] + radius[0]*cos(t))*sin(t))

        if center[0] > 0.0:
            if center[1] > 0.0:
                t_0, t_1 = -pi, -pi/2
            else:
                t_0, t_1 = pi/2, pi
        else:
            if center[1] > 0.0:
                t_0, t_1 = -pi/2, 0
            else:
                t_0, t_1 = 0, pi/2

        t = bisect(f,t_0,t_1)
        x = center[0] + radius[0]*cos(t)
        y = center[1] + radius[1]*sin(t)
        return sqrt(x**2 + y**2)

print Ellipse((1.0,-1.0),(2.0,0.5)).distance_from_origin()
相关问题