Created
December 26, 2018 08:04
-
-
Save LadyScream/6262bddb764d17d7740db5ad99d26d89 to your computer and use it in GitHub Desktop.
Browniand Tree Snowflakes
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
Point current; | |
ArrayList<Point> snowflake; | |
boolean newSnow; | |
int newtimer; | |
boolean recording; | |
void setup() { | |
recording = false; | |
size(720, 720); | |
current = new Point(new PVector(height/2-100, 0)); | |
snowflake = new ArrayList<Point>(); | |
newSnow = false; | |
newtimer = 0; | |
} | |
void draw() { | |
if (newSnow) { | |
newtimer++; | |
if (newtimer > 20) { | |
snowflake = new ArrayList<Point>(); | |
newSnow = false; | |
newtimer = 0; | |
} | |
} | |
translate(width/2, height/2); | |
rotate(PI/2); | |
background(0); | |
noStroke(); | |
while (!current.finished && !current.intersects(snowflake)) { | |
current.update(); | |
} | |
Point last1 = new Point(new PVector(0, 0)); | |
Point last2 = new Point(new PVector(0, 2)); | |
if (snowflake.size() > 2) { | |
last1 = snowflake.get(snowflake.size()-1); | |
last2 = snowflake.get(snowflake.size()-2); | |
} | |
if (snowflake.size() < 2 || !((last1.pos.x == last2.pos.x) && (last1.pos.y == last2.pos.y))){ | |
snowflake.add(current); | |
current = new Point(new PVector(height/2-100, 0)); | |
} else { | |
newSnow = true; | |
} | |
for (int i = 0; i < 6; i++) { | |
rotate(PI/3); | |
int count = 0; | |
for (Point point: snowflake) { | |
fill((float)count/(float)snowflake.size() * 255); | |
point.draw(); | |
count++; | |
} | |
pushMatrix(); | |
count = 0; | |
scale(1, -1); | |
for (Point point: snowflake) { | |
fill((float)count/(float)snowflake.size() * 255); | |
point.draw(); | |
count++; | |
} | |
popMatrix(); | |
} | |
if (recording) { | |
saveFrame("snow-####.gif"); | |
} | |
} | |
class Point { | |
PVector pos; | |
float r; | |
boolean finished; | |
Point(PVector pos_) { | |
pos = pos_; | |
r = 3; | |
finished = false; | |
} | |
void draw() { | |
//ellipse(pos.x, pos.y, r * 2, r * 2); | |
float x1 = sin(TWO_PI * 1/3 + PI/2) * r + pos.x; | |
float y1 = cos(TWO_PI * 1/3 + PI/2) * r + pos.y; | |
float x2 = sin(TWO_PI * 2/3 + PI/2) * r + pos.x; | |
float y2 = cos(TWO_PI * 2/3 + PI/2) * r + pos.y; | |
float x3 = sin(TWO_PI * 3/3 + PI/2) * r + pos.x; | |
float y3 = cos(TWO_PI * 3/3 + PI/2) * r + pos.y; | |
triangle(x1,y1,x2,y2,x3,y3); | |
} | |
void update() { | |
pos.x -= 1; | |
pos.y += random(-3, 3); | |
float angle = pos.heading(); | |
angle = constrain(angle, 0, PI/6); | |
float mag = pos.mag(); | |
pos = PVector.fromAngle(angle); | |
pos.setMag(mag); | |
if (pos.x < 1) { | |
finished = true; | |
} | |
} | |
boolean intersects(ArrayList<Point> array) { | |
boolean doesIt = false; | |
for (int i = 0; i < array.size(); i++) { | |
if (dist(array.get(i).pos.x, array.get(i).pos.y, pos.x, pos.y) < r * 2) { | |
doesIt = true; | |
break; | |
} | |
} | |
return doesIt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment