椭圆旋转θ角后椭圆与直线的交点
Points of Intersection of an Ellipse and a line after rotating ellipse by angle theta
我想在将椭圆旋转角度 theta 后找到椭圆和直线的交点。
我写了 python 代码来找到椭圆和直线的交点,但是我不知道如何在将椭圆旋转 θ 角后找到交点。
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)
如果你有轴对齐椭圆的测试和现成的解决方案,那么将定义线的点转换为椭圆系统,找到交点,然后进行反向转换就简单多了。
对于以原点为中心的椭圆和旋转角度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)
我想在将椭圆旋转角度 theta 后找到椭圆和直线的交点。
我写了 python 代码来找到椭圆和直线的交点,但是我不知道如何在将椭圆旋转 θ 角后找到交点。
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)
如果你有轴对齐椭圆的测试和现成的解决方案,那么将定义线的点转换为椭圆系统,找到交点,然后进行反向转换就简单多了。
对于以原点为中心的椭圆和旋转角度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)