[R] Semi-random movement inside a circle

Yihui Xie xieyihui at gmail.com
Thu Dec 18 16:52:13 CET 2008


Hi,

I've simplified the "bouncing off" part as choosing another place to
go (with no consideration of the bouncing details):

##
set.seed(1234)
par(pty = "s", pch = 19, ann = FALSE, xaxt = "n",
    yaxt = "n", bty = "n")
theta = seq(0, 2 * pi, length = 512)
n = 20
nmax = 500
x = runif(n, -1, 1)
y = runif(n, -1, 1)
palette(rainbow(n))
for (i in 1:nmax) {
    plot(sin(theta), cos(theta), type = "l", lwd = 5)
    x = x + rnorm(n, 0, 0.1)
    y = y + rnorm(n, 0, 0.1)
    cond = x^2 + y^2 > 1
    r = sqrt(x[cond]^2 + y[cond]^2)
    x[cond] = x[cond]/r/1.1  # use 1.1 due to the size of points
    y[cond] = y[cond]/r/1.1
    points(x, y, col = 1:n, cex = 2)
    Sys.sleep(0.1)
}

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China



On Tue, Dec 16, 2008 at 6:16 AM, Struve, Juliane
<j.struve at imperial.ac.uk> wrote:
> Dear list,
>
> I am trying to program semi-random movement within a circle, with no particles leaving the circle. I would like them to bounce back when they come to close to the wall, but I don't seem to be able to get this right.  Would somebody kindly give me a hint ? This is my code so far, the particle starts at some point and moves towards the wall, but I don't get the "bouncing off" part right .
>
> Any help would be much appreciated.
>
> Juliane
>
> days=10
> circularspace=data.frame(day=c(0:days),xcoord=1,ycoord=1,xvelocity=1,yvelocity=1,xdistwall=0, ydistwall=0, wallxvel=0, wallyvel=0,stochasticxvel=0,stochasticyvel=0)
> xmax=10
> xmin=-10
> ymax=10
> ymin=-10
> mindist=8
> plot(xmin:xmax, ymin:ymax, type = "n")
> circularspace
> radius=10
> timesteplength=1
> weightfactor=1
> for(i in 1:days)
> {
> #This is the stochastic component of the movement
> circularspace$stochasticxvel[day=i+1]=runif(1,min=-1,max=1)
> circularspace$stochasticyvel[day=i+1]=runif(1,min=-1,max=1)
> #This is calculating the movment speed
> circularspace$xvelocity[day=i+1]=weightfactor*(circularspace$xvelocity[day=i])+ (1-weightfactor)*circularspace$stochasticxvel[day=i+1]
> circularspace$yvelocity[day=i+1]=weightfactor*(circularspace$yvelocity[day=i])+ (1-weightfactor)*circularspace$stochasticyvel[day=i+1]
> #This is calculating the new position
> circularspace$xcoord[day=i+1]=circularspace$xcoord[day=i]+circularspace$xvelocity[day=i]/timesteplength
> circularspace$ycoord[day=i+1]=circularspace$ycoord[day=i]+circularspace$yvelocity[day=i]/timesteplength
> #Tis is checking the distance from the wall
> circularspace$xdistwall[day=i+1]=xmax-abs(circularspace$xcoord[day=i])
> circularspace$ydistwall[day=i+1]=ymax-abs(circularspace$ycoord[day=i])
> #This is updating the movement if distance to wall too small
> if (circularspace$xdistwall[day=i+1] <= mindist){
> circularspace$wallxvel[day=i+1]= -1*(circularspace$xcoord[day=i+1])
> circularspace$xvelocity[day=i+1]=weightfactor*circularspace$xvelocity[day=i]+ (1-weightfactor)*circularspace$stochasticxvel[day=i+1]+ circularspace$wallxvel[day=i+1]
> circularspace$xcoord[day=i+1]=circularspace$xcoord[day=i]+circularspace$xvelocity[day=i]/timesteplength
> }
> if (circularspace$ydistwall[day=i+1] <= mindist){
> circularspace$wallyvel[day=i+1]= -1*(circularspace$ycoord[day=i+1])
> circularspace$yvelocity[day=i+1]=weightfactor*circularspace$yvelocity[day=i]+ (1-weightfactor)*circularspace$stochasticyvel[day=i+1]+ circularspace$wallyvel[day=i+1]
> circularspace$ycoord[day=i+1]=circularspace$ycoord[day=i]+circularspace$yvelocity[day=i]/timesteplength
> }
> points(circularspace$xcoord[day=i+1],circularspace$ycoord[day=i+1])
> symbols(0,0,circles=radius,inches=F,add=T)
> symbols(0,0,circles=radius-2,inches=F,add=T)
> }
> circularspace
>
>        [[alternative HTML version deleted]]
>



More information about the R-help mailing list