Generative Christmas Baubles 2.0

by lakrok in Circuits > Art

502 Views, 4 Favorites, 0 Comments

Generative Christmas Baubles 2.0

211209_chrismasbaubles.png
Have you seen this minimalistic snowflake pattern on a Christmas bauble? Have you got inspired by the style of it? Fantastic! So now, as we live in a digital world, we would like to create them also digitally.
The special clue about this digital postcard is the generative process of it. Each snowflake on a Christmas bauble has it’s individual pattern and is created randomly.
In this instructable we are going to learn about a programming language called Java including two main techniques called “loops” and “switches”.
It will take you one hour to code this project.
Have fun! :)

Supplies

Processing 4.0 Beta 2

Coding

function setup() {
createCanvas(600, 600); // the size of the canvas
}

function draw() {
background(0); //draw a background
stroke(255); //stroke color
noFill(); //fill color
frameRate(100); // set framerate of the baubles //make a loop
for (let i = 0; i <= width; i = i + 100){ // in a raw
for(let y = 0; y <= height; y = y + 100){ // in a line
circle(i,y,80); // call the circle
rect(i-10, y+50, 20, 20); // draw a holding
rect(i-2, y+30, 3, 19); //draw a holding
fill(random(255),random(255),random(255));
} // fill with color
// draw snowflakes in a loop
for (let i = 0; i <= width; i = i + 100){ // in a raw
for(let y = 0; y <= height; y = y + 100){ // in a line
snowflake(i,y, 10); // call the snowflakes
}
}
}
// make a switch
function snowflake(cen_x, cen_y, r) {
const n = Math.floor(Math.random() * 4) + 1;
//
switch(n){
case 1: // möglicher Wert einer variable im switch
snowflake1(cen_x, cen_y, r);
break; //jedes case braucht einen break
case 2:
snowflake2(cen_x, cen_y, r);
break;
case 3:
snowflake3(cen_x, cen_y, r);
break;
case 4:
snowflake4(cen_x, cen_y, r);
break;
}
}

function snowflake4(cen_x,cen_y,r){
r1 = r;
for(let i = 0;i<6;i++){
for(let n = 0;n<3;n++){
stroke(255);
strokeWeight(1);
let ang = radians(-90+60*i);
let x = cen_x + (r/3*(n+2)) * cos(ang);
let y = cen_y + (r/3*(n+2)) * sin(ang);
line(cen_x,cen_y,x,y);

for(let j = 0;j<3;j++){
let ang1 = radians(-150+60*i +60*j)
let x1 = x + (r1 - r1/3*(n+1)) * cos(ang1);
let y1 = y + (r1 - r1/3*(n+1)) * sin(ang1);
line(x,y,x1,y1);
}
}
}
}

function snowflake3(cen_x,cen_y,r){
r1 = r;
for(let i = 0;i<6;i++){
for(let n = 0;n<3;n++){
stroke(255);
strokeWeight(1);
let ang = radians(-90+60*i);
let x = cen_x + (r/3*(n+1)) * cos(ang);
let y = cen_y + (r/3*(n+1)) * sin(ang);
line(cen_x,cen_y,x,y);

for(let j = 0;j<3;j++){
let ang1 = radians(-150+60*i +60*j)
let x1 = x + (r1/3*(n+1)) * cos(ang1);
let y1 = y + (r1/3*(n+1)) * sin(ang1);
line(x,y,x1,y1);
}
}
}
}

function snowflake2(cen_x,cen_y,r){
r1 = r * 0.5;
for(let i = 0;i<6;i++){
for(let n = 0;n<3;n++){
stroke(255); //stroke color
strokeWeight(1); //stroke weight
let ang = radians(-90+60*i);
let x = cen_x + (r/3*(n+1)) * cos(ang);
let y = cen_y + (r/3*(n+1)) * sin(ang);
line(cen_x,cen_y,x,y);

for(let j = 0;j<3;j++){
let ang1 = radians(-150+60*i +60*j)
let x1 = x + r1 * cos(ang1);
let y1 = y + r1 * sin(ang1);
line(x,y,x1,y1);
}
}
}
}

function snowflake1(cen_x,cen_y,r){
r1 = r * 0.5;
for(let i = 0;i<6;i++){
stroke(255); //stroke color
strokeWeight(1); // stroke weight
let ang = radians(-90+60*i);
let x = cen_x + r * cos(ang);
let y = cen_y + r * sin(ang);
line(cen_x,cen_y,x,y);
for(let j = 0;j<3;j++){
let ang1 = radians(-150+60*i +60*j)
let x1 = x + r1 * cos(ang1);
let y1 = y + r1 * sin(ang1);
line(x,y,x1,y1);
}
}
}

function mousePressed() {
saveFrame("line-######.png"); //save image as a png
}
}

Downloads