Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.27 KB | None | 0 0
  1. puddleJumper <- function(nPoints = 3,fraction=0.5){
  2.    
  3.     # Initialise and run checks.
  4.     library(magicaxis)
  5.    
  6.     plotCols = c("#3B9AB2","#53A5B9","#6BB1C1","#8FBBA5","#BDC367","#EBCC2A","#E6C019","#E3B408","#E49100","#EB5500","#F21A00")
  7.    
  8.     if(nPoints < 3){
  9.         stop("Need 3 or more points!")
  10.     }
  11.    
  12.     if(fraction >= 1){
  13.         stop("Fraction must be less than 1!")
  14.     }
  15.    
  16.     # Anchor points.
  17.     anchorPoints = matrix(data=runif(nPoints * 2),nrow=nPoints,ncol=2)
  18.     #anchorPoints = rbind(c(0.1,0.1),c(0.1,0.9),c(0.9,0.1),c(0.9,0.9))
  19.    
  20.     # Starting position.
  21.     startPoint = rbind(runif(n=2))
  22.    
  23.     # Get going!
  24.  
  25.     newPoint = {}
  26.     for(i in 1:10000){
  27.        
  28.         direction = sample(x=1:nPoints,size=1) 
  29.         # New point is the half way point between the current point and where the random number goes.
  30.         temp = c((startPoint[1]+anchorPoints[direction,1])*fraction,(startPoint[2]+anchorPoints[direction,2])*fraction)
  31.         newPoint = rbind(newPoint,temp)
  32.         startPoint = temp
  33.     }
  34.    
  35.     # Plot
  36.  
  37.     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]))
  38.     points(startPoint[1],startPoint[2],pch=20,cex=3,col=plotCols[5])
  39.     points(anchorPoints[,1],anchorPoints[,2],pch=20,cex=3,col=plotCols[1])
  40.    
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement