Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Created December 23, 2018 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskurgit/c3c974696ca3ce978c3b215443af4ba3 to your computer and use it in GitHub Desktop.
Save fiskurgit/c3c974696ca3ce978c3b215443af4ba3 to your computer and use it in GitHub Desktop.
import processing.pdf.*;
float angleVarianzPIDivider = 3; //PI is divided by this number to define the variance in branching angles
float radius = 170; //randius of the snowflake
float endLength = random(1, 10);
float reduce = random(1.5, 2.5);
float randAngle = 0;
boolean single = true;
boolean record = false;
void setup() {
size(1000, 1000);
noFill();
stroke(0, 75);
strokeWeight(0.45);
}
int count = 0;
String filename = "xxx.pdf";
boolean shouldKeep = false;
boolean refresh = true;
void draw() {
if (refresh) {
background(255);
if (single) {
filename = "Snowflake" + count + ".pdf";
if(record) beginRecord(PDF, filename);
generateSnowflake();
refresh = false;
count++;
if(record) endRecord();
} else {
generateSnowflake();
refresh = false;
}
}
}
void keyPressed() {
shouldKeep = true;
}
void mousePressed() {
if (!shouldKeep) {
File f = new File(filename);
f.delete();
}
shouldKeep = false;
reduce = random(1.5, 2.5);
angleVarianzPIDivider = random(2, 10);
endLength = random(4, 10);
refresh = true;
}
float rndAng = 0;
void generateSnowflake() {
if (random(100) > 50) {
randomBrasnchAngles = true;
}
if (single) {
background(255);
stroke(0);
for (int i = 0; i < 6; ++i) {
//seed = random(255);
PVector origin = new PVector(width / 2, height / 2);
float angle = (TWO_PI / 6) * i;
angle += TWO_PI/12;
generateBranch(origin, radius, angle);
}
} else {
background(0);
stroke(255, 100);
int cols = 8;
int rows = 8;
int colWidth = width/cols;
int colHeight = height/rows;
radius = colWidth;
for(int r = 0 ; r < 2 ; r++){
for (int i = 0; i < cols; i++) {
for (int j = 0; j < cols; j++) {
//radius = random(colWidth);
randAngle = random(TWO_PI);
if (random(100) > 50) {
randomBrasnchAngles = true;
}
reduce = random(1.5, 2.5);
angleVarianzPIDivider = random(2, 10);
endLength = random(4, 10);
float xVar = random(-colWidth, colWidth)/2;
float yVar = random(-colHeight, colHeight)/2;
for (int a = 0; a < 6; ++a) {
PVector origin = new PVector(i * colWidth + colWidth/2 + xVar, j * colHeight + colHeight/2 + yVar);
float angle = (TWO_PI / 6) * a;
angle += TWO_PI/12;
generateBranch(origin, radius/3, angle + randAngle);
}
}
}
}
}
}
boolean randomBrasnchAngles = false;
void generateBranch(PVector origin, float bLength, float angle) {
if (bLength < endLength) {
return;
}
float randomAngle = random(0, PI / angleVarianzPIDivider);
if (!randomBrasnchAngles) {
randomAngle = (TWO_PI / 12);
}
pushMatrix();
translate(origin.x, origin.y);
rotate(angle);
generateBranch(new PVector(0, 0), bLength / reduce, 0);
generateBranch(new PVector(bLength, 0), bLength / reduce, 0);
generateBranch(new PVector(bLength / reduce, 0), bLength / reduce, -randomAngle);
generateBranch(new PVector(bLength / reduce, 0), bLength / reduce, randomAngle);
line(0, 0, bLength, 0);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment