// July 2009 // http://www.abandonedart.org // http://www.zenbullets.com // // This work is licensed under a Creative Commons 3.0 License. // (Attribution - NonCommerical - ShareAlike) // http://creativecommons.org/licenses/by-nc-sa/3.0/ // // This basically means, you are free to use it as long as you: // 1. give http://www.zenbullets.com a credit // 2. don't use it for commercial gain // 3. share anything you create with it in the same way I have // // These conditions can be waived if you want to do something groovy with it // though, so feel free to email me via http://www.zenbullets.com int _num = 100; Circle[] _circleArr = {}; // an array of circles void setup() { size(500,300); background(255); smooth(); // default line/fill strokeWeight(1); fill(150, 50); // draw some circles _num = int(30 + random(100)); drawCircles(_num); } void draw() { noStroke(); fill(255, 3); rect(0,0,width, height); // loop through every circle in the array and call updateMe() on it for (int i=0; i<_circleArr.length; i++) { Circle thisCirc = _circleArr[i]; thisCirc.updateMe(); } } void mouseReleased() { // on pressing mouse, clear and randomise _num = int(30 + random(100)); _circleArr = (Circle[])expand(_circleArr, 0); drawCircles(_num); println(_circleArr.length); } void drawCircles(int num) { // draw _num number of circles at random points for (int i=0; i (width+radius)) { x = 0 - radius; } if (x < (0-radius)) { x = width+radius; } if (y > (height+radius)) { y = 0 - radius; } if (y < (0-radius)) { y = height+radius; } // basic collision detection // loop through all other circles and see if they are intersecting // boolean touching = false; for (int i=0; i<_circleArr.length; i++) { Circle otherCirc = _circleArr[i]; if (otherCirc != this) { // don't test against ourself // calculate the distance between them float dis = dist(x, y, otherCirc.x, otherCirc.y); float overlap = dis - radius - otherCirc.radius; if (overlap < 0) { // draw a circle of the intersect // first work out the central point between our two touching circs float intx, inty; if (x < otherCirc.x) { intx = x + (otherCirc.x - x)/2; } else { intx = otherCirc.x + (x - otherCirc.x)/2; } if (y < otherCirc.y) { inty = y + (otherCirc.y - y)/2; } else { inty = otherCirc.y + (y - otherCirc.y)/2; } stroke(0, 5); noFill(); overlap *= -3; // make it into a positive number ellipse(intx, inty, overlap, overlap); } } } drawMe(); } }