r - 根据焦点画一个椭圆

有没有办法在 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)

https://stackoverflow.com/questions/11944767/

相关文章:

php - 将 PDF 存储在 MySQL 数据库中

css - 带 float 的无序列表项 :left do not float properly

ruby-on-rails - RAILS3 : to_JSON with multiple obj

php - 使用 maxlength 标签通过 PHP 检查表单输入长度

php - 如何在php中显示有限的单词

bash - 获取 n 个最后记录并更改它们的特定列

ruby-on-rails - 在 Rails 3 的邮件拦截器中设置 header

sql - SQL Server 中的窗口函数

sql - UNION ALL 之后的 CTE

php - 如何使用 SimpleXML 解析忽略错误的 XML