椭圆和角度为θ的旋转椭圆后的直线的交点

时间:2019-02-13 10:13:47

标签: python math line intersection ellipse

我想按角度theta旋转椭圆后找到椭圆和直线的交点。

我已经编写了python代码来查找椭圆和直线的交点,但是在将椭圆旋转theta角后,我无法弄清楚如何找到交点。

def intersactionPoints(a,b,h,k,x1,y1,x2,y2):
    #xi1, yi1, xi2, yi2 <- intersection points
    xi1, yi1, xi2, yi2, aa, bb, cc, m = 0, 0, 0, 0, 0, 0, 0, 0
    if x1 != x2:
        m = (y2 - y1)/(x2 - x1)
        c = y1 - m * x1
        aa = b * b + a * a * m * m
        bb = 2 * a * a * c * m - 2 * a * a * k * m - 2 * h * b * b
        cc = b * b * h * h + a * a * c * c - 2 * a * a * k * c + a * a * k * k - a * a * b * b
    else:
        # vertical line case
        aa = a * a
        bb = -2.0 * k * a * a
        cc = -a * a * b * b + b * b * (x1 - h) * (x1 - h)
    d = bb * bb - 4 * aa * cc
    # intersection points : (xi1,yi1) and (xi2,yi2)
    if d > 0:
        if (x1 != x2):
            xi1 = (-bb + (d**0.5)) / (2 * aa)
            xi2 = (-bb - (d**0.5)) / (2 * aa)
            yi1 = y1 + m * (xi1 - x1)
            yi2 = y1 + m * (xi2 - x1)
        else:
            yi1 = (-bb + (d**0.5)) / (2 * aa)
            yi2 = (-bb - (d**0.5)) / (2 * aa)
            xi1 = x1
            xi2 = x1
    return xi1, yi1, xi2, yi2


if __name__ == "__main__":
    a =  #major axis
    b =  #minor axis
    h =  #center x of ellipse
    k =  #center y of ellipse
    x1 =  #line coordinate x1
    y1 =  #line coordinate y1
    x2 = #line coordinate x2
    y2 = #line coordinate y2
    xi1, yi1, xi2, yi2 = intersactionPoints(a,b,h,k,x1,y1,x2,y2)

1 个答案:

答案 0 :(得分:2)

如果您已经测试了可用于轴对齐椭圆的解决方案,则将定义线的点转换为椭圆系统,找到相交点,然后进行逆向转换要简单得多。

以原点为中心的椭圆和旋转角theta

 x1' = x1 * Cos(theta) + y1 * Sin(theta)
 y1' = - x1 * Sin(theta) + y1 * Cos(theta)

对于以cx,cy为中心的椭圆:

 x1' = (x1 - cx) * Cos(theta) + (y1 - cy) * Sin(theta)
 y1' = - (x1 - cx) * Sin(theta) + (y1 - cy) * Cos(theta)