根据焦点绘制一个椭圆 [英] Draw an ellipse based on its foci
问题描述
有没有办法在R中根据下面的定义(而不是特征值)绘制简单的椭圆?
我想使用的定义是椭圆是平面中的点的集合,其中到两个固定点F 1和F 2的距离之和是常数。
我应该只使用极坐标吗?
这可能是更算法上的问题。作为@DWin的建议,有几种绘制椭圆的实现(例如函数 draw.ellipse
打包 plotrix
)。要找到它们:
RSiteSearch(ellipse,restrict =functions)
这就是说,如果你知道一个小几何,实现你自己的函数相当简单。这里是一个尝试:
ellipse < - function(xf1,yf1,xf2,yf2,k,new = TRUE ,. ..){
#xf1和yf1是你的焦点坐标F1
#xf2和yf2是你焦点的坐标F2
#k是你的常数(到F1和如果函数需要创建一个新的绘图或向现有的绘图添加一个椭圆,则
#new是一个合乎逻辑的说法。
#...是可以传递给函数图或线(col,lwd,lty等)的任何参数
t < - seq(0,2 * pi,by = pi / 100) #改变参数来改变分辨率
k / 2 - > a#主轴
xc < - (xf1 + xf2)/ 2
yc < - (yf1 + yf2)/ 2#中心坐标
dc < - sqrt(( xf1-xf2)^ 2 +(yf1-yf2)^ 2)/ 2#焦点到中心的距离
b < - sqrt(a ^ 2 - dc ^ 2)#小轴
phi < - atan(abs(yf1-yf2)/ abs(xf1-xf2))#长轴与x轴之间的角度
xt <-xc + a * cos(t)* cos(phi ) - b * sin(t)* sin(phi)
yt <-yc + a * cos(t)* sin(phi)+ b * sin(t)* cos(phi)
if(new){plot(xt,yt,type =l,...)}
if(!new){lines(xt,yt,...)}
}
举例:
<$ c(1,2)
plot(rbind(F1,F2),xlim = c(-1,5) ,ylim = c(-1,5),pch = 19)
abline(h = 0,v = 0,col =grey90)
ellipse(F1 [1],F1 [2] ,(F1 [1] + F2 [1])/ 2,(F2 [1],F2 [2],k = 2,new = FALSE,col =red,lwd = 2) F1 [2] + F2 [2])/ 2,pch = 3)
Is there a way to draw a simple ellipse based on the following definition (instead of eigenvalue) in R?
The definition I want to use is that an ellipse is the set of points in a plane for which the sum of the distances to two fixed points F1 and F2 is a constant.
Should I just use a polar cordinate?
This may be more algorithmic question.
As @DWin suggested, there are several implementations for plotting ellipses (such as function draw.ellipse
in package plotrix
). To find them:
RSiteSearch("ellipse", restrict="functions")
That being said, implementing your own function is fairly simple if you know a little geometry. Here is an attempt:
ellipse <- function(xf1, yf1, xf2, yf2, k, new=TRUE,...){
# xf1 and yf1 are the coordinates of your focus F1
# xf2 and yf2 are the coordinates of your focus F2
# k is your constant (sum of distances to F1 and F2 of any points on the ellipse)
# new is a logical saying if the function needs to create a new plot or add an ellipse to an existing plot.
# ... is any arguments you can pass to functions plot or lines (col, lwd, lty, etc.)
t <- seq(0, 2*pi, by=pi/100) # Change the by parameters to change resolution
k/2 -> a # Major axis
xc <- (xf1+xf2)/2
yc <- (yf1+yf2)/2 # Coordinates of the center
dc <- sqrt((xf1-xf2)^2 + (yf1-yf2)^2)/2 # Distance of the foci to the center
b <- sqrt(a^2 - dc^2) # Minor axis
phi <- atan(abs(yf1-yf2)/abs(xf1-xf2)) # Angle between the major axis and the x-axis
xt <- xc + a*cos(t)*cos(phi) - b*sin(t)*sin(phi)
yt <- yc + a*cos(t)*sin(phi) + b*sin(t)*cos(phi)
if(new){ plot(xt,yt,type="l",...) }
if(!new){ lines(xt,yt,...) }
}
An example:
F1 <- c(2,3)
F2 <- c(1,2)
plot(rbind(F1, F2), xlim=c(-1,5), ylim=c(-1, 5), pch=19)
abline(h=0, v=0, col="grey90")
ellipse(F1[1], F1[2], F2[1], F2[2], k=2, new=FALSE, col="red", lwd=2)
points((F1[1]+F2[1])/2, (F1[2]+F2[2])/2, pch=3)
这篇关于根据焦点绘制一个椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!