Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- puddleJumper <- function(nPoints = 3,fraction=0.5){
- # Initialise and run checks.
- library(magicaxis)
- plotCols = c("#3B9AB2","#53A5B9","#6BB1C1","#8FBBA5","#BDC367","#EBCC2A","#E6C019","#E3B408","#E49100","#EB5500","#F21A00")
- if(nPoints < 3){
- stop("Need 3 or more points!")
- }
- if(fraction >= 1){
- stop("Fraction must be less than 1!")
- }
- # Anchor points.
- anchorPoints = matrix(data=runif(nPoints * 2),nrow=nPoints,ncol=2)
- #anchorPoints = rbind(c(0.1,0.1),c(0.1,0.9),c(0.9,0.1),c(0.9,0.9))
- # Starting position.
- startPoint = rbind(runif(n=2))
- # Get going!
- newPoint = {}
- for(i in 1:10000){
- direction = sample(x=1:nPoints,size=1)
- # New point is the half way point between the current point and where the random number goes.
- temp = c((startPoint[1]+anchorPoints[direction,1])*fraction,(startPoint[2]+anchorPoints[direction,2])*fraction)
- newPoint = rbind(newPoint,temp)
- startPoint = temp
- }
- # Plot
- magplot(newPoint[,1],newPoint[,2],pch=20,col=plotCols[11],cex=0.5,xlim=range(rbind(newPoint,anchorPoints,startPoint)[,1]),ylim=range(rbind(newPoint,anchorPoints,startPoint)[,2]))
- points(startPoint[1],startPoint[2],pch=20,cex=3,col=plotCols[5])
- points(anchorPoints[,1],anchorPoints[,2],pch=20,cex=3,col=plotCols[1])
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement