# Implementing Circles example

February 4, 2012
By

(This article was first published on R snippets, and kindly contributed to R-bloggers)

This week I reimplemented part of Conic Sections 1 model from NetLogo. In the model turtles seek to to be in target distance from center.

My code takes only one center point, so only circles can be obtained. Apart from turtle location plot given in NetLogo implementation I added:

1. plot showing maximal difference between turtle distance and target distance;
2. decreasing turtle step size.

Here is the plot showing final simulation state, but it is also nice to watch the simulation run:

Below is the code generating the simulation:

`# n: number of turtles# p.x, p.y: location of center# range: turtles have random position from [0,range]#        and will move in random angle a# step: how fast turtles move# target: target distance from center# time: simulation timeinit <- function(n, p.x, p.y, range, step, target, time) {    sim <- list(        turtles = data.frame(x = runif(n, max = range),                             y = runif(n, max = range),                             a = runif(n, max = 2 * pi)),        p.x = p.x, p.y = p.y, step = step, target = target,        time = time, max.dist = rep(NA, time))    # Calculate turtle distance from center    sim\$turtles\$dist <- sqrt((sim\$turtles\$x - p.x) ^ 2 +                             (sim\$turtles\$y - p.y) ^ 2)    return(sim)}step <- function(sim) {    x <- sim\$turtles\$x    y <- sim\$turtles\$y    # Remember last distance and save current distance    o.dist <- sim\$turtles\$dist    n.dist <- sqrt((x - sim\$p.x) ^ 2 + (y - sim\$p.y) ^ 2)    sim\$turtles\$dist <- n.dist    # For turtles that are too far and are moving out    # or too close and are moving in randomly change direction    w.dist <- ((n.dist < o.dist) & (n.dist < sim\$target)) |               ((n.dist > o.dist) & (n.dist > sim\$target))    sim\$turtles\$a[w.dist] <- runif(sum(w.dist), max = 2 * pi)    sim\$turtles\$x <- x + sin(sim\$turtles\$a) * sim\$step    sim\$turtles\$y <- y + cos(sim\$turtles\$a) * sim\$step    return(sim)}do.plot <- function(sim) {    rng <- quantile(c(sim\$turtles\$x, sim\$turtles\$y),                    c(0.05, 0.95))    rng <- round(rng, -1) + c(-10, 10)    par(mai = rep(0.5, 4), mfrow = c(1, 2))    plot(sim\$turtles\$x, sim\$turtles\$y, pch = ".",         xlim = rng, ylim = rng, xlab = "", ylab = "",         main = "Turtle location")    points(sim\$p.x, sim\$p.y, col = "red", pch = 20, cex = 2)    plot(sim\$max.dist, type = "l",         ylim = c(0, max(sim\$max.dist, na.rm = TRUE) + 5),         xlab = "", ylab = "", main = "Max difference from target")}run <- function(sim) {    for (i in 1:sim\$time) {        sim <- step(sim)        sim\$step <- sim\$step * 127 / 128        sim\$max.dist[i] <- max(sim\$turtles\$dist) - sim\$target        do.plot(sim)    }}sim <- init(4096, 128, 128, 256, 2, 128, 512)set.seed(0)run(sim)`

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...