// Code by Antti Tiihonen, 2009 int pixelNum = 0; int pixelX[] = new int[1]; int pixelY[] = new int[1]; int mouseX2 = 0; int mouseY2 = 0; boolean simulationActive = false; void setup() { background(255); size(640, 640, P2D); frameRate(60); } void draw() { if(simulationActive == true) { spawnLine(); } } void spawnLine() { int randomPixelA = int(random(pixelNum)); int randomPixelB = int(random(pixelNum)); drawLine(pixelX[randomPixelA], pixelY[randomPixelA], pixelX[randomPixelB], pixelY[randomPixelB]); } void drawLine(int x1, int y1, int x2, int y2) { // Measure the distance float lineLength = dist(x1, y1, x2, y2); // Lerp between coords and store pixels. for(int i = 1; i < int(lineLength); i++) { float lerpPos = norm(i, 0, lineLength); float x = lerp(x1, x2, lerpPos); float y = lerp(y1, y2, lerpPos); // If a solid line is encountered, give up! int r = ((get(int(x), int(y))) >> 16) & 0xFF; if(r < 2) { i = int(lineLength); x2 = int(lerp(x1, x2, lerpPos)); y2 = int(lerp(y1, y2, lerpPos)); } fillPixel(int(x), int(y)); } // Draw main line stroke(0); strokeWeight(1); line(x1, y1, x2, y2); // Draw the shading line stroke(0, 31); strokeWeight(sq(lineLength * 0.01)); line(x1, y1, x2, y2); } void fillPixel(int xPos, int yPos) { // Increase the size of arrays if needed if(pixelNum >= pixelX.length) { pixelX = expand(pixelX); pixelY = expand(pixelY); } pixelX[pixelNum] = xPos; pixelY[pixelNum] = yPos; pixelNum++; } void mousePressed() { mouseX2 = mouseX; mouseY2 = mouseY; } void mouseReleased() { drawLine(mouseX2, mouseY2, mouseX, mouseY); } void keyPressed() { if(simulationActive == false) { simulationActive = true; } else { reset(); //simulationActive = false; } } void reset() { pixelNum = 0; pixelX = expand(pixelX, 1); pixelY = expand(pixelY, 1); simulationActive = false; background(255); }