gpt4 book ai didi

r - 根据焦点画一个椭圆

转载 作者:行者123 更新时间:2023-12-05 08:35:06 25 4
gpt4 key购买 nike

有没有办法在 R 中根据以下定义(而不是特征值)绘制一个简单的椭圆?

我想使用的定义是,椭圆是平面中到两个固定点 F1 和 F2 是一个常数。

我应该只使用极坐标吗?

这可能是更多的算法问题。

最佳答案

正如@DWin 所建议的,有几种绘制椭圆的实现(例如 plotrix 包中的函数 draw.ellipse)。要找到它们:

 RSiteSearch("ellipse", restrict="functions")

也就是说,如果您了解一点几何知识,实现您自己的功能会相当简单。这是一个尝试:

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,...) }
}

一个例子:

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)

enter image description here

关于r - 根据焦点画一个椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11944767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com