/** try both mouse buttons and the r, g, b, y, w, p, d. */ Vector points; String state = "normal"; float k = 0; void setup() { size(600, 600); background(0); noStroke(); noCursor(); smooth(); points = new Vector(); } void draw() { if(state == "normal") fill(0,5); else fill(0,50); noStroke(); rect(0,0,width,height); point curr; for (int i =0; i < points.size(); i++){ curr = (point)points.elementAt(i); if(state == "blackhole") { if((abs(curr.lx - mouseX) + abs(curr.ly - mouseY)) < 20) { if((abs(curr.x - mouseX) + abs(curr.y - mouseY)) < 20) points.removeElementAt(i); } } curr.touch(); stroke(curr.r, curr.g, curr.b); strokeWeight(curr.cwidth); line(curr.x,curr.y,curr.lx,curr.ly); if(keyPressed) { if(key == 'r' || key == 'R') { int tmp = int(random(50,255)); curr.r = tmp; curr.g = 0; curr.b = 0; } if(key == 'g' || key == 'G') { int tmp = int(random(50,255)); curr.g = tmp; curr.r = 0; curr.b = 0; } if(key == 'b' || key == 'B') { int tmp = int(random(50,255)); curr.b = tmp; curr.r = 0; curr.g = 0; } if(key == 'y' || key == 'Y') { int tmp = int(random(200,255)); int tmp2 = int(random(150,255)); curr.r = tmp; curr.g = tmp2; curr.b = 0; } if(key == 'w' || key == 'W') { int tmp = int(random(200,255)); curr.r = tmp; curr.g = tmp; curr.b = tmp; } if(key == 'p' || key == 'P') { int tmp = int(random(150,255)); int tmp2 = int(random(200,255)); curr.r = tmp; curr.b = tmp2; curr.g = 0; } if(key == 'd' || key == 'D') { float tmp = random(0,15); if(tmp < 3.0) { curr.r = int(random(150,255)); curr.g = int(random(80,120)); curr.b = int(random(80,120)); } else if(tmp < 6.0) { curr.r = 255; curr.g = int(random(150,255)); } else if(tmp < 9.0) { curr.g = int(random(150,255)); curr.b = int(random(150,255)); } else { curr.r = 255; curr.g = 255; curr.b = 255; } } if(key == ' ') { if(curr.damping > 0.99) curr.damping -= 0.01; curr.power = 500; } } } if(random(0,100) < 3 && mousePressed != true && mouseX > 0 && mouseY > 0 && points.size() < 500) { point tmp = new point(); tmp.dx = random(-1,1); tmp.dy = random(-1,1); if(state == "blackhole") { tmp.damping = 0.99; tmp.power = random(400,800); } points.add(tmp); } else if (random(0,100) < 50 && mousePressed && mouseButton == RIGHT && mouseX > 0 && mouseY > 0 && points.size() < 500) { point tmp = new point(); tmp.dx = random(-1,1); tmp.dy = random(-1,1); if(state == "blackhole") { tmp.damping = 0.99; tmp.power = random(400,800); } points.add(tmp); } noStroke(); if(state == "blackhole") { int z = 100; for(int i = 10; i <= 110; i += 2) { fill(0, z); ellipse(mouseX, mouseY, i, i); z -= 2; } fill(255); ellipse(mouseX, mouseY, sin(k) * 3 + 5, sin(k) * 3 + 5); } else { fill(255); ellipse(mouseX, mouseY, sin(k) + 2, sin(k) + 2); } if(k < TWO_PI) k += 0.1; else k = 0; println(points.size()); } void keyPressed() { if(key == ' ') { if(state == "normal") state = "blackhole"; else state = "normal"; point curr; if(state == "normal") { for(int i = 0; i < points.size(); i++){ curr = (point)points.elementAt(i); curr.damping = 5; } } else { for(int i = 0; i < points.size(); i++){ curr = (point)points.elementAt(i); curr.damping = 0.999; } } } else if(key == 's' || key == 'S') { saveFrame("orbit_###.jpg"); } } void mousePressed() { if(mouseButton == LEFT) { point curr; for(int i = 0; i < points.size(); i++){ curr = (point)points.elementAt(i); if(curr.damping > 0.04) curr.damping -= 0.02; curr.power = 2; } } } void mouseReleased() { point curr; for(int i = 0; i < points.size(); i++){ curr = (point)points.elementAt(i); curr.damping = 1.01; curr.power = random(1500,2000); } } class point{ float x, y; float lx, ly; float cwidth, cheight; float dx = 0, dy = 0; float dx2 = 0, dy2 = 0; float damping; float power; int r = 0, g = 0, b = 0; public point(){ x = mouseX; y = mouseY; cwidth = random(0,3); cheight = cwidth; damping = 1.01; power = random(1500,2000); float tmp = random(0,15); if(tmp < 3.0) { r = int(random(150,255)); g = int(random(80,120)); b = int(random(80,120)); } else if(tmp < 6.0) { r = 255; g = int(random(150,255)); } else if(tmp < 9.0) { g = int(random(150,255)); b = int(random(150,255)); } else { r = 255; g = 255; b = 255; } } public void touch(){ //move towards the mouse dx2 = (x - mouseX) / power; dy2 = (y - mouseY) / power; dx = (dx - dx2) * damping; dy = (dy - dy2) * damping; if(dx > 5) dx = 5; else if(dx < -5) dx = -5; if(dy > 5) dy = 5; else if(dy < -5) dy = -5; lx = x; ly = y; x += dx; y += dy; } }