R/bounce.R

Defines functions bounce

Documented in bounce

#' Bouncing Ball
#'
#' Demonstrate bouncing ball.
#'
#' @param n number of time steps to simulate.
#' @param init initial x and y coordinates (vector of length 2).
#' @param vel initial x and y velocity (vector of length 2).
#' @param xlim x-axis limits (vector of length 2).
#' @param ylim y-axis limits (vector of length 2).
#' @param xaxs x-axis style.
#' @param yaxs y-axis style.
#' @param pch point symbol.
#' @param col point color.
#' @param cex point size.
#'
#' @return \code{NULL}, but \code{n} plots are drawn.
#'
#' @importFrom graphics plot
#'
#' @export

bounce <- function(n=5000, init=c(3,1), vel=c(0.03,0.04), xlim=c(0,10),
                   ylim=c(0,10), xaxs="i", yaxs="i", pch=16, col="blue",
                   cex=1.5)
{
  x <- init[1]
  y <- init[2]
  vx <- vel[1]
  vy <- vel[2]

  for(i in 1:n)
  {
    if(x+vx<=xlim[1] || x+vx>=xlim[2])
      vx <- -vx
    if(y+vy<=ylim[1] || y+vy>=ylim[2])
      vy <- -vy
    x <- x + vx
    y <- y + vy
    plot(x, y, xlim=xlim, ylim=ylim, xaxs=xaxs, yaxs=yaxs, ann=FALSE, xaxt="n",
         yaxt="n", pch=pch, col=col, cex=cex)
  }
}
arnima-github/arni documentation built on Oct. 28, 2023, 6:18 p.m.