xxxxxxxxxx
96
// require https://cdn.jsdelivr.net/npm/p5@latest/lib/p5.min.js
// Instructions by Sonya Olomskaya
// INSTRUCTIONS: DIAMOND SNAKES
// 1. Have any kind of blank canvas with a drawing tool
// 2. Choose any point at the top of your canvas
// 3. Draw a zigzag line down to any point on the bottom of the canvas without lifting up your pen
// 4. From the bottom of your line, draw a zigzag line back up to the top connecting zigzag corner points to the previous line to create a pattern
// 5. Choose a new point at the top of your page
// 6. Repeat steps 2-3 (the new zigzag line cannot touch any previously drawn lines)
// 7. Continue drawing until there is no space left on the page for each new zigzag pattern to not be touching
const paperWidth = 400;
const paperRatio = 1.4142; // DIN
const snakesNum = 20;
const snakes = [];
function setup() {
createCanvas(paperWidth, paperRatio*paperWidth);
noLoop();
noStroke();
setupSnakes();
}
function draw() {
blendMode(BLEND);
background(0);
blendMode(SCREEN);
for( const snake in snakes ){
snakes[snake].draw();
}
}
function setupSnakes(){
for( let i = 0; i < snakesNum; i++ ){
snakes.push(new DiamondSnake( random(10,50), color( random(255),random(255),random(255) ) ));
}
}
class DiamondSnake {
constructor( count = 30, color = '#f00' ){
this.diamondCount = count;
this.x = random(width);
this.shifter = this.diamondCount / 3 * 2;
this.color = color;
}
draw(){
push();
fill(this.color);
beginShape();
for( let c = 0; c < 2; c++ ){
let shifter = c === 1 ? this.shifter : this.shifter * -1;
for( let i = 0; i <= this.diamondCount; i++ ){
let y = lerp( 0, height, i/this.diamondCount );
if( c === 1 ){
y = lerp( 0, height, i/this.diamondCount );
} else {
y = lerp( height, 0, i/this.diamondCount );
}
const x = i % 2 !== 0 ? this.x + shifter : this.x;
vertex(x,y);
}
}
endShape();
pop();
}
}
function keyPressed() {
if (keyCode === ENTER) {
snakes.splice(0, snakes.length)
setupSnakes();
redraw();
}
}