【问题标题】:Finding intersection points of two ellipses (Python)查找两个椭圆的交点(Python)
【发布时间】:2013-03-04 22:38:12
【问题描述】:

我正在用 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 个相交点(无相交、相切、部分重叠、部分重叠和内部相切、完全重叠)。

我找到了一些潜在的解决方案:

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

【问题讨论】:

  • 这可以简化为求解二元二次方程
  • 大多数 ellipse-vs.-circle 解决方案对您不起作用,因为通常您通过参数化椭圆然后找出它与圆心的距离等于圆的半径来做到这一点。 (如果您知道以同步方式参数化两个椭圆的正确相位,您可以这样做……但在我的脑海中,我怀疑这并不比让椭圆相交更容易……)
  • 另外,我认为这属于math.stackexchange.com 而不是这里——事实上这是一个已经迁移到那里的问题math.stackexchange.com/questions/197982/…
  • 它可能不属于那里(并且可能不是一个副本)的唯一原因是因为这是明确用于编程的,因此可能不会建议使用以下@HYRY 之类的折线解决方案

标签: python geometry intersection shapes ellipse


【解决方案1】:

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

所以另一种方法是将椭圆近似为折线,并使用shapely找到交点,代码如下:

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

和输出:

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

【讨论】:

  • 我担心我的两个选项是 a) 复杂或 b) 依赖项(尤其是非 Python)。为这个特性需要一个完整的库似乎太可惜了。
【解决方案2】:

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

你有:

从他们的例子中,我认为两个椭圆绝对有可能相交:

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

编辑:由于 sympy 不支持通用椭圆 (ax^2 + bx + cy^2 + dy + exy + f),

您应该自己构建方程并对其进行变换,并使用它们的求解器来找到交点。

【讨论】:

  • 注意:由于不支持通用椭圆,所以椭圆的轴不会旋转。只有中心旋转到新位置。所以,sympy 找不到一般的椭圆交集。
  • sympy 的问题不在于几何库,而在于系统构建时使用大量小数点的浮点数的性能不佳
【解决方案3】:

您可以使用这里显示的方法: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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多