Last active
August 29, 2015 14:01
-
-
Save bengrosser/752a46450afa1fc95160 to your computer and use it in GitHub Desktop.
a bit of fun with lines in Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// linecrawler | |
// having some fun with lines | |
// -by grosser, 5/18/2014 | |
static int MAXCRAWLERS = 10000; | |
float len; | |
boolean up = true; | |
int x, y; | |
int h = 160; | |
int sat = 5; | |
Crawler[] crawlers = new Crawler[MAXCRAWLERS]; | |
int numcrawlers = 0; | |
int rate; | |
boolean speedup = true; | |
int framecount = 0; | |
boolean cheat = false; | |
boolean whiteout = false; | |
void setup() { | |
size(displayWidth,displayHeight); | |
background(255); | |
noCursor(); | |
len = 10; | |
x = width/2; | |
y = height/2; | |
rate = 30; | |
frameRate(rate); | |
smooth(); | |
} | |
void draw() { | |
if(random(100) > 20) makeNewCrawler(int(random(width)),int(random(height))); | |
for(int i = 0; i < numcrawlers; i++) crawlers[i].draw(); | |
if(framecount > 60) changeFrameRate(); | |
else framecount++; | |
frameRate(rate); | |
//saveFrame("crawler-######.png"); | |
if(cheat) { | |
colorMode(HSB); | |
noStroke(); | |
fill(h,sat,200); | |
rect(20,height-40,20,20); | |
} | |
} | |
void changeFrameRate() { | |
framecount = 0; | |
if(speedup == true) rate++; | |
else rate--; | |
if(rate > 60) speedup = false; | |
if(rate < 20) speedup = true; | |
//println(rate); | |
} | |
void mousePressed() { | |
makeNewCrawler(mouseX,mouseY); | |
} | |
void keyPressed() { | |
if(keyCode == RIGHT) h++; | |
if(keyCode == LEFT) h--; | |
if(keyCode == UP) sat++; | |
if(keyCode == DOWN) sat--; | |
if(key == 'c') cheat = !cheat; | |
if(key == 'w') whiteout = !whiteout; | |
println("hue: "+h+", sat: "+sat); | |
} | |
void makeNewCrawler(int x, int y) { | |
if(numcrawlers == MAXCRAWLERS) numcrawlers = 0; | |
crawlers[numcrawlers] = | |
new Crawler(x,y,map(y,0,height,50,255),map(x,0,width,5,25)); | |
numcrawlers++; | |
} | |
class Crawler { | |
int x, y; | |
float len, s; | |
boolean up = true; | |
float rot; | |
float xinc, yinc; | |
color c; | |
Crawler(int _x, int _y, float _s, float _l) { | |
x = _x; | |
y = _y; | |
s = _s; | |
len = _l; | |
rot = map(x,0,width,1,TWO_PI); | |
xinc = random(0.6,map(x,0,width,1,10)); | |
yinc = random(0.6,map(y,0,height,1.3,10)); | |
colorMode(HSB); | |
//float h = map(mouseX,0,width,0,255); | |
c = color(constrain(random(h-20,h+20),0,255), | |
constrain(random(sat-20,sat+20),0,255), | |
random(20,255)); | |
if(whiteout) { | |
colorMode(RGB); | |
c = color(255,255,255); | |
} | |
//c = color(h,sat,random(20,255)); | |
} | |
void draw() { | |
pushMatrix(); | |
translate(x,y); | |
rotate(map(y,0,height,0,rot)); | |
stroke(s,constrain(len*4,15,80)); | |
stroke(c,constrain(len*4,15,80)); | |
line(-len,-len,len,len); | |
popMatrix(); | |
updateValues(); | |
} | |
void updateValues() { | |
if(up == true) len+=0.1; | |
else len-=0.1; | |
if(len < 0) up = true; | |
else if(len > 20) up = false; | |
x += random(0.5,xinc); | |
y += random(0.5,yinc); | |
if(x > width) x = 0; | |
if(y > height) y = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment