给定R中的一般方程,如何绘制椭圆? [英] how to plot ellipse given a general equation in R?
问题描述
椭圆一般公式:
a * x ^ 2 + b * y ^ 2 + c * x * y + d * x + e * y + f = 0
推荐答案
我们可以从ellipse
的parametric
公式开始(以下一个来自维基百科),我们需要5个参数:center (xc, yc)
在另一种表示法中,轴长度a, b
和x轴与主轴phi
或tau
之间的夹角在另一种表示法中.
We can start from the parametric
equation of an ellipse
(the following one is from wikipedia), we need 5 parameters: the center (xc, yc)
or (h,k)
in another notation, axis lengths a, b
and the angle between x axis and the major axis phi
or tau
in another notation.
xc <- 1 # center x_c or h
yc <- 2 # y_c or k
a <- 5 # major axis length
b <- 2 # minor axis length
phi <- pi/3 # angle of major axis with x axis phi or tau
t <- seq(0, 2*pi, 0.01)
x <- xc + a*cos(t)*cos(phi) - b*sin(t)*sin(phi)
y <- yc + a*cos(t)*cos(phi) + b*sin(t)*cos(phi)
plot(x,y,pch=19, col='blue')
现在,如果要从cartesian conic
公式开始,则需要两个步骤.
Now if we want to start from the cartesian conic
equation, it's a 2-step process.
-
将
cartesian
公式转换为polar
(parametric
),形成形式,我们可以使用以下公式首先使用下图中的5个方程式获得5个参数(取自 http://www.cs.cornell.edu/cv/OtherPdf/Ellipse.pdf ,可以在此处找到详细的数学信息.)
Convert the
cartesian
equation to thepolar
(parametric
), form we can use the following equations to first obtain the 5 parameters using the 5 equations from the below figure (taken from http://www.cs.cornell.edu/cv/OtherPdf/Ellipse.pdf, detailed math can be found there).
使用获得的参数绘制椭圆,如上所示.
Plot the ellipse, by using the parameters obtained, as shown above.
对于步骤(1),我们可以使用以下代码(当我们知道A,B,C,D,E,F
时):
For step (1) we can use the following code (when we have known A,B,C,D,E,F
):
M0 <- matrix(c(F,D/2,E/2, D/2, A, B/2, E/2, B/2, C), nrow=3, byrow=TRUE)
M <- matrix(c(A,B/2,B/2,C), nrow=2)
lambda <- eigen(M)$values
abs(lambda - A)
abs(lambda - C)
# assuming abs(lambda[1] - A) < abs(lambda[1] - C), if not, swap lambda[1] and lambda[2] in the following equations:
a <- sqrt(-det(M0)/(det(M)*lambda[1]))
b <- sqrt(-det(M0)/(det(M)*lambda[2]))
xc <- (B*E-2*C*D)/(4*A*C-B^2)
yc <- (B*D-2*A*E)/(4*A*C-B^2)
phi <- pi/2 - atan((A-C)/B)*2
对于步骤(2),请使用以下代码:
For step (2) use the following code:
t <- seq(0, 2*pi, 0.01)
x <- xc + a*cos(t)*cos(phi) - b*sin(t)*sin(phi)
y <- yc + a*cos(t)*cos(phi) + b*sin(t)*cos(phi)
plot(x,y,pch=19, col='blue')
这篇关于给定R中的一般方程,如何绘制椭圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!