Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get n points along ellipse given endpoints & minor axis

Tags:

r

I am attempting to draw an ellipse between two endpoints or what this picture calls the vertices:

enter image description here

In the function I want to give the coordinates of the two vertices and specify the distance of what the image calls the minor axis (perpendicular to the line created by the vertices. I brushed off some high school math but am struggling with creating a function that gives me n values along the ellipse. I have looked at the ellipse package but this appears to want a correlation, however, I want the function to return values like ellipse does.

I'm not stuck on any particular approach but would like to specify the arguments in my attempted function below. My search of ellipse and R yielded many hits but not given the arguments I'm attempting to use.

elip <- function(vert1 = c(.25, .45), vert2 = c(.5, .35), minoraxis = .1, n =150) {

    majoraxis <- sqrt(((vert2[1] - vert1[1])^2) + ((vert2[2] - vert1[2])^2))
    center <- c((vert2[1] + vert1[1])/2, (vert2[2] + vert1[2])/2)
    half_ma <- minoraxis/2
    focci_dist <- sqrt(abs((majoraxis ^2) - (half_ma^2)))

}
like image 290
Tyler Rinker Avatar asked Sep 13 '13 05:09

Tyler Rinker


1 Answers

This takes the two vertices on the major axis and the half length of the minor axis (as b) along with the number of points.

halfEllipse <- function(v1, v2, b, n = 100){
    x1 <- v1[1]
    y1 <- v1[2]
    x2 <- v2[1]
    y2 <- v2[2]
    xc <- mean(c(x1,x2))
    yc <- mean(c(y1,y2))
    A <- sqrt((xc-x1)^2 + (yc-y1)^2)
    myangle <- atan((yc-y1)/(xc-x1))

    # Construct half ellipse with desired
    # major and minor axis length
    ts <- seq(0, pi, length.out = n)
    X <- A*cos(ts)
    Y <- b*sin(ts)

    # Rotate to get to desired angle
    Xp <- X*cos(myangle) - Y*sin(myangle) 
    Yp <- X*sin(myangle) + Y*cos(myangle)

    # Shift back to desired center
    Xp <- Xp + xc
    Yp <- Yp + yc

    cbind(Xp,Yp)
}
like image 100
Dason Avatar answered Oct 18 '22 06:10

Dason