xxxxxxxxxx
//
// draw a sine wave, amplified
// program for lectures on "The Wrong Way to Draw a Line"
//
// turn the sin wave into random grass by
// creating my own random and noise functions
//
// schien@mail.ncku.edu.tw, DPD 2020
//
let xnoise = 0.0;
function setup() {
createCanvas(500, 100);
frameRate(12);
//noLoop();
}
function draw() {
background(255);
strokeWeight(5);
stroke(0, 30);
line(20, 50, 480, 50); // draw a line
stroke(20, 50, 70, 180);
let step = 10;
let lastx = -999;
let lasty = -999;
let y = 20;
let border = 70;
for (let x=border; x<=width-border; x+=step) {
let x1=x+20;
let y1=y+20;
strokeWeight(1);
stroke(225,20,10);
y = 50 - customRandom()*30;
if (lastx > -999) {
line(x1, y1+20, x, 70);
}
lastx = x1;
lasty = y1;
}
for (let x=border; x<=width-border; x+=step) {
strokeWeight(1);
stroke(225,20,80);
y = 50 - customRandom()*30;
let newx = x + customNoise()*step;
line(x*2, y*2, x, y);
if (lastx > -999) {
line(newx, y, x, 50);
}
lastx = x;
lasty = y;
}
}
// my own random function
function customRandom() {
let retValue = 1 - pow(random(1), 5);
return retValue;
}
function customNoise() {
let rand = random(2) - 1;
xnoise += rand;
let retValue = noise(xnoise);
return retValue;
}
function keyPressed() {
if (key === 's' || key === 'S') {
save('grasses.png');
}
}