Created
March 16, 2018 13:18
-
-
Save KrabCode/08c82c0c5deff63aeefe7b9d9dc70303 to your computer and use it in GitHub Desktop.
fun with snowflakes in processing
This file contains hidden or 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
| ArrayList<Line> lines = new ArrayList<Line>(); | |
| void setup(){ | |
| size(800,800); | |
| background(200); | |
| frameRate(1); | |
| createBaseShape(); | |
| strokeWeight(1); | |
| drawLines(); | |
| } | |
| void draw(){ | |
| background(200); | |
| drawLines(); | |
| applyRule(); | |
| } | |
| void createBaseShape(){ | |
| int padding = 200; | |
| PVector a = new PVector(width/2, padding); | |
| PVector b = new PVector(padding, height-padding); | |
| PVector c = new PVector(width-padding, height - padding); | |
| lines.add(new Line(a, b)); | |
| lines.add(new Line(b, c)); | |
| lines.add(new Line(c,a)); | |
| } | |
| void applyRule(){ | |
| ArrayList<Line> newGen = new ArrayList<Line>(); | |
| for(Line l : lines){ | |
| float dist = l.origin.dist(l.target); | |
| float angle = getAngle(l.origin, l.target); | |
| PVector a = l.origin; | |
| PVector b = getPVectorAtAngle(a,dist/3,angle); | |
| PVector c = getPVectorAtAngle(b,dist/3,angle+60); | |
| PVector d = getPVectorAtAngle(a,dist-dist/3,angle); | |
| PVector e = l.target; | |
| newGen.add(new Line(a, b)); | |
| newGen.add(new Line(b, c)); | |
| newGen.add(new Line(c, d)); | |
| newGen.add(new Line(d, e)); | |
| } | |
| lines = newGen; | |
| } | |
| void drawLines(){ | |
| for(Line l : lines){ | |
| l.draw(); | |
| } | |
| } | |
| class Line{ | |
| PVector origin, target; | |
| public Line(PVector origin, PVector target){ | |
| this.origin = origin; | |
| this.target = target; | |
| } | |
| void draw(){ | |
| line(origin.x, origin.y, target.x, target.y); | |
| } | |
| } | |
| public PVector getPVectorAtAngle(PVector center, float radius, float angle) { | |
| return new PVector(center.x + radius * cos(angle * PI / 180), center.y + radius * sin(angle * PI / 180)); | |
| } | |
| public float getAngle(PVector origin, PVector end) { | |
| return degrees(atan2(end.y - origin.y, end.x - origin.x)); | |
| } |
Author
KrabCode
commented
Mar 16, 2018

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment