Draw an ellipse based on its tricks

Is there a way to draw a simple ellipse based on the following definition (instead of an eigenvalue) in R?

The definition I want to use is that the ellipse is the set of points in the plane for which the sum of the distances to two fixed points F 1 and F 2 is a constant.

Should I use a polar core?

This may be a more algorithmic question.

+1
source share
1 answer

As @DWin suggested, there are several implementations for building ellipses (such as the draw.ellipse function in the plotrix package). To find them:

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

Moreover, the implementation of your own function is quite 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,...) } } 

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) 

enter image description here

+11
source

Source: https://habr.com/ru/post/1469260/


All Articles