#' @title Approximation of pi
#'
#' The function approaches the value of pi using a polygon of n sides (n defined by the nuber of files in path)
#'
#' @param path the path to count the files to define n.
#' @param adjacent the circle radius. Or the adjacent side of the set of triangles built at the polygon's centre.
#' @param plot_polygon should the polygon be ploted?
#' @param ... options for plot
#'
#' @return
#' an approximation of pi
#' @export
#' @examples
#' app_pi()
#' app_pi(type = 'n', xaxt = "n", yaxt = 'n', xlab='', ylab ='', bty = 'n')
app_pi <- function(path = ".", adjacent = 5, plot_polygon = TRUE, ...){
psides <- length(list.files(path))
angle <- 2*pi / psides
if(plot_polygon){
plot_polygon(angle, radius = adjacent, ...)
}
if(length(list.files(path)) == 0)
stop("This is the error!")
# Approximate pi
opposite <- adjacent * tan(angle)
perimeter <- opposite * psides
pi_app <- perimeter / (adjacent * 2)
print(paste("There are ", psides, " files in ", path, sep = ""))
print(paste("using a polygon with ", psides,
" sides I approximated pi as = ",
round(pi_app, digits = 3), sep = ""))
pi_app
}
plot_polygon <- function(angle, radius, ...){
angle <- seq(0,2*pi, by = angle)
x <- sin(angle) * radius
y <- cos(angle) * radius
par(pty = 's')
plot(rnorm(1), xlim = c(radius*-1.1,radius*1.1), ylim = c(radius*-1.1,radius*1.1), ...)
polygon(y ~ x, col = "red")
NULL
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.