/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.ewgZEbTsJde/rev.967
*
* authors:
* Doug Park
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// This sketch builds on a prior work, "Lines of March", created by Doug Park
// http://studio.sketchpad.cc/sp/pad/view/ro.9o7Pwz5qhoLlT/rev.597
// height and width are built in global variables automatically set based on the canvas size.
// Lines Marching
// March 11, 2012
// Lines marching across the screen
// create a line counter to clear the screen every so often
counter = 0;
// This runs once to setup the program
void setup() {
// set canvis size to a big 500x500 pixel
size(500, 500);
// make the background white
background(255);
// call a function to set the initial color of the lines
changeColor();
// setup the line
x1 = random(width);
x2 = random(width);
y1 = random(height);
y2 = random(height);
// setup delta offset, each end point of the line will move independently
dx1 = random(9)+1;
dx2 = random(9)+1;
dy1 = random(9)+1;
dy2 = random(9)+1;
// set the width of the line.
strokeWeight(1);
}
// This runs in a continious loop
void draw() {
// draw a line based on the x1,y1 and x2,y2 endpoints
line (x1, y1, x2, y2);
// Move the endpoints of the line based on their delta offset
x1 = x1 + dx1;
y1 = y1 + dy1;
x2 = x2 + dx2;
y2 = y2 + dy2;
// check if the endpoint of the line hits a screen boundry
// if it does then reverse the delta offset for that line endpoint
// also change the color of the line to keep things interesting
if (x1 <= 0) { dx1 = - dx1; changeColor() };
if (x1 >= width) { dx1 = - dx1; changeColor() };
if (x2 <= 0) { dx2 = - dx2; changeColor() };
if (x2 >= width) { dx2 = - dx2; changeColor() };
if (y1 <= 0) { dy1 = - dy1; changeColor() };
if (y1 >= height) { dy1 = - dy1; changeColor() };
if (y2 <= 0) { dy2 = - dy2; changeColor() };
if (y2 >= height) { dy2 = - dy2; changeColor() };
// check the line counter to see if it is time to clear the screen
if (counter > random(200) + 500) { background(255); counter = 0; };
counter = counter + 1;
}
// change the color of the line everytime an endpoint hits a screen boundry
void changeColor() {
stroke (random(50),random(255), random(255));
}