[英]How to find the equation for an ellipse
I am looking to find the equation for an ellipse given five or six points using the general equation for a conic: A x2 + B xy + C y2 + D x + E y + F = 0. 我正在寻找使用圆锥的一般方程式给出给定五个或六个点的椭圆方程式:A x2 + B xy + C y2 + D x + E y + F = 0。
At first I tried using six points. 一开始我尝试使用六点。 Here is my python code:
这是我的python代码:
import numpy as np
def conic_section(p1, p2, p3, p4, p5, p6):
def row(point):
return [point[0]*point[0], point[0]*point[1], point[1]*point[1],
point[0], point[1], 1]
matrix=np.matrix([row(p1),row(p2),row(p3),row(p4),row(p5), row(p6)])
b=[0,0,0,0,0,0]
return np.linalg.solve(matrix,b)
print conic_section(np.array([6,5]), np.array([2,9]), np.array([0,0]),
np.array([11, 5.5]), np.array([6, 7]), np.array([-1,-1]))
The problem is that this will return the solution [0,0,0,0,0,0] because the right hand side of my equation is the zero vector. 问题在于,这将返回解[0,0,0,0,0,0],因为我方程的右侧是零向量。
I then attempted to change the conic by subtracting the F and dividing it through: 然后,我尝试通过减去F并将其除以更改圆锥曲线:
A x2 + B xy + C y2 + D x + E y + F = 0 A x2 + B xy + C y2 + D x + E y + F = 0
A x2 + B xy + C y2 + D x + E y = -F A x2 + B xy + C y2 + D x + E y = -F
A' x2 + B xy + C' y2 + D' x + E' y = -1. A'x2 + B xy + C'y2 + D'x + E'y = -1。
The reason that this doesn't work though is that if one of my point is (0,0), then I would end up with a Matrix that has a row of zeros, yet the right hand side of the equation would have a -1 for the entries in the vector. 但这不起作用的原因是,如果我的观点之一是(0,0),那么我最终将得到一个矩阵,该矩阵具有零行,而等式的右侧将具有-向量中的条目为1。 In other words, if one of my points is (0,0) - then "F" should be 0, and so I can't divide it out.
换句话说,如果我的观点之一是(0,0)-那么“ F”应为0,所以我无法将其除掉。
Any help would be appreciated. 任何帮助,将不胜感激。 Thank You.
谢谢。
It seems that you have exact points on ellipse, don't need approximation, and use Braikenridge-Maclaurin construction for conic sections by some strange way. 看来您在椭圆上有确切的点,不需要近似,并以某种奇怪的方式对圆锥形截面使用Braikenridge-Maclaurin构造。
Five points (x[i],y[i])
determines ellipse with this explicit equation ( mathworld page, eq. 8 ) 五点
(x[i],y[i])
用这个明确的方程式确定椭圆( mathworld页面,等式8 )
So to find ellipse equation, you can build cofactor expansion of the determinant by minors for the first row. 因此,要找到椭圆方程,您可以为第一行的未成年人建立行列式的辅因子展开 。 For example, coefficient
A
is determinant value for submatrix from x1y1
to the right bottom corner, coefficient B
is negated value of determinant for submatrix without xiyi
column and so on. 例如,系数
A
是从x1y1
到右下角的子矩阵的行列式值,系数B
是没有xiyi
列的子矩阵的行列式的取反值,依此类推。
Ellipse equation (without translation and rotation): 椭圆方程(不带平移和旋转):
The goal is to resolve this linear equation in variable A
through F
: 目的是解决变量
A
到F
线性方程 :
Use: 采用:
from math import sin, cos, pi, sqrt
import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import eig, inv
# basis parameters of the ellipse
a = 7
b = 4
def ellipse(t, a, b):
return a*cos(t), b*sin(t)
points = [ellipse(t, a, b) for t in np.linspace(0, 2*pi, 100)]
x, y = [np.array(v) for v in list(zip(*points))]
fig = plt.figure()
plt.scatter(x, y)
plt.show()
def fit_ellipse(x, y):
x = x[:, np.newaxis]
y = y[:, np.newaxis]
D = np.column_stack((x**2, x*y, y**2, x, y, np.ones_like(x)))
S = np.dot(D.T, D)
C = np.zeros([6,6])
C[0, 2] = C[2, 0] = 2
C[1, 1] = -1
E, V = eig(np.dot(inv(S), C))
n = np.argmax(np.abs(E))
return V[:, n]
A, B, C, D, E, F = fit_ellipse(x, y)
K = D**2/(4*A) + E**2/(4*C) - F
# a, b
print('a:', sqrt(K/A), 'b:', sqrt(K/C))
Output: 输出:
a: 6.999999999999998 b: 4.0 a:6.999999999999998 b:4.0
See: 看到:
http://mathworld.wolfram.com/ConicSection.html https://fr.wikipedia.org/wiki/Ellipse_(math%C3%A9matiques)#Forme_matricielle http://nicky.vanforeest.com/misc/fitEllipse/fitEllipse.html http://mathworld.wolfram.com/ConicSection.html https://fr.wikipedia.org/wiki/Ellipse_(math%C3%A9matiques)#Forme_matricielle http://nicky.vanforeest.com/misc/fitEllipse/fitEllipse html的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.