R/egg.coords.R

#' Get points on the egg surface
#'
#' Returns points on the egg's surface in speherical coordinates
#'
#' @param egg an object of class \code{egg.fit}
#' @param type whether to collect a series of \code{points} or the vertices of a \code{polygon}
#' @param ... additional parameters passed to the \code{\link[graphics]{lines}} and/or \code{\link[graphics]{points}} functions
#'
#' @return An object of class \code{egg.coords} containing the spherical coordinates of the selected points
#'
#' @examples
#' stop()
#'
#' @export
egg.coords = function(egg, type="points", ...) {
  options(locatorBell=FALSE)
  tol = egg$length/100
  px = c()
  py = c()
  sph = c()
  select = TRUE
  while(select) {
    coords = locator(n=1, type="n")
    if(is.null(coords$x) || is.null(coords$y)) {
      n = length(px)
      if(type=="polygon" && n > 1) {
        if(all(sph[n,] != sph[1,])) {
          sph = rbind(sph, sph[1,])
        }
        egg.geodesic(egg, sph[c(n,1),], n=20, plot=TRUE, return.points=FALSE, ...)
      }
      select = FALSE
    } else {
      px = c(px, coords$x)
      py = c(py, coords$y)
      sph = rbind(sph, get.sph(coords$x, coords$y, egg))
      n = length(px)
      if(type=="polygon" && n > 1) {
        if(sqrt((px[1]-px[n])^2 + (py[1]-py[n])^2) < tol) {
          px[n] = px[1]
          py[n] = py[1]
          sph[n,] = sph[1,]
          select = FALSE
        }
        egg.geodesic(egg, sph[c(n,(n-1)),], n=20, plot=TRUE, return.points=FALSE, ...)
      }
      if(type=="rect" && n==2) {
        px = c(px[1], px[1], px[2], px[2], px[1])
        py = c(py[1], py[2], py[2], py[1], py[1])
        sph = rbind(sph[1,], get.sph(px[2],py[2],egg), sph[2,], get.sph(px[4],py[4],egg), sph[1,])
        for(i in 2:5) {
          egg.geodesic(egg, sph[c(i,(i-1)),], n=20, plot=TRUE, return.points=FALSE, ...)
        }
        points(px, py, type="p", ...)
        select = FALSE
      }
      points(px, py, type="p", ...)
    }
  }
  colnames(sph) = c("r", "theta", "phi")
  class(sph) = "egg.coords"
  return (sph)
}

Try the eggs package in your browser

Any scripts or data that you put into this service are public.

eggs documentation built on May 2, 2019, 5:23 p.m.