Created
December 15, 2012 20:23
-
-
Save druedin/4298885 to your computer and use it in GitHub Desktop.
Execute the code on http://www.statisticsblog.com/2012/12/let-it-snow/ directly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code by Matt Asher for statisticsblog.com | |
# Feel free to modify and redistribute | |
# How many flakes do you want to fall? | |
flakes = 100 | |
# Width and height of your space | |
width = 800 | |
height = 600 | |
# Initial wind | |
wind = 0 | |
# Setup the background of the plot and margins | |
par(bg = "black") | |
par(oma=c(0,0,0,0)) | |
par(mar=c(0,0,0,0)) | |
plot(0, 0, col="black", pch=".", xlim=c(0,width), ylim=c(0,height), axes=F) | |
for(i in 1:flakes) { | |
startY = height | |
startX = runif(1,1,width) | |
xPos = startX | |
yPos = startY | |
for(j in 1:height) { | |
# Optional drift in wind | |
wind = wind + rcauchy(1,0,.05) | |
# Update snowflake position | |
xPos = xPos + rnorm(1,.1,1.5) | |
yPos = yPos - runif(1,4,20) | |
# Are we in the space, if so display it | |
if(xPos > 0 & xPos <= width & yPos > 0 & yPos <= height) { | |
points(round(xPos), round(yPos), col="white", pch=8) | |
# System dely, slows down the flakes | |
Sys.sleep(0.1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment