ArrayList snakes = new ArrayList(); void setup(){ size(500,500); strokeWeight(10); /* currentSnake = new Snake(100,100); currentSnake.add(150,150); currentSnake.add(200,100); currentSnake.add(250,150); currentSnake.makeReal(); snakes.add(currentSnake);*/ } void keyPressed(){ snakes = new ArrayList(); currentSnake = null; } Snake currentSnake = null; void mousePressed(){ currentSnake = new Snake(mouseX,mouseY); } void mouseDragged(){ if(currentSnake != null) currentSnake.add(mouseX,mouseY); } void mouseReleased(){ if(currentSnake != null) { currentSnake.makeReal(); snakes.add(currentSnake); } currentSnake = null; } void draw(){ background(50); if(currentSnake != null) currentSnake.drawBasic(); for(Snake s : snakes){ s.drawReal(); } } class Snake{ float t = 0,ts; ArrayList points = new ArrayList(); float x,y; Snake(float px, float py){ x = px; y = py; } void add(float px, float py){ points.add(new Point(px,py)); } void drawBasic(){ stroke(0); float lx = x; float ly = y; for(Point p : points){ line(lx,ly,p.x,p.y); lx = p.x; ly = p.y; } } color c; ArrayList segs = new ArrayList(); void makeReal(){ t = random(-.005,.005); c = color(random(150,255),random(150,255),random(150,255)); float lx = x; float ly = y; for(Point p : points){ float a = atan2(p.y-ly,p.x-lx); float d = dist(p.x,p.y,lx,ly); segs.add(new Seg(d,a)); lx = p.x; ly = p.y; } } void drawReal(){ stroke(c); float ox = x; float oy = y; float nt = t; t +=ts; ts += random(-.01,.01); if(abs(ts) > .008) ts = ts /2; for(Seg s : segs){ s.update(nt); nt += t; float nx = ox + (cos(s.a) * s.d) ; float ny = oy + (sin(s.a) * s.d) ; line(ox,oy,nx,ny); ox = nx; oy = ny; } } } class Seg{ float d,a,as; Seg(float pd, float pa){ d = pd; a = pa; as = 0; } void update(float t){ //as += random(-.1,.1); //a += t; as += pmrandom(.03); a += as; if(abs(as) > .3) as = .3 * as / abs(as); } } class Point{ float x,y; Point(float px, float py){ x = px; y = py; } } float pmrandom(float v){ return random(-v,v); }