/** * Pointillism * by Daniel Shiffman. * * Mouse horizontal location controls size of dots. * Creates a simple pointillist effect using ellipses colored * according to pixels in an image. */ PImage source; int originX; int originY; pointsArray points; boolean render; int minRadius; void setup() { source = loadImage("party.jpg"); size(source.width,source.height); noStroke(); background(source); smooth(); originX = 0; originY = 0; points = new pointsArray(); render = false; minRadius = 5; } void keyPressed() { if( key=='s'){//save the screen saveFrame( "seriousbusiness-####.jpg"); } else if (key=='c'){//clear the screen background(255); } else if (key =='g'){ render = ! render; if(!render){ background(source); } } } void draw() { //reset it all to a new origin point if(mousePressed){ points.addPoint(new oPoint(mouseX, mouseY)); } if (render){ pointilize() ; pointilize() ; pointilize() ; pointilize() ; pointilize() ; } } void pointilize() { //generate a raondom point that we are going to pointillize int x = int(random(source.width)); int y = int(random(source.height)); //what is the smallest distance we are from an origin point float distance = minDist(new oPoint(x,y)); //let's generate a size based on that distance //we want that size to tend to be proportional to the distance //but allow for some random variation in size float circleSize = (distance/8) * produceNormalDistributionNumber(); drawCircle(x, y, max(circleSize, minRadius)); } float minDist(oPoint p){ float smallestDistance = dist(p.x, p.y, points.points[0].x, points.points[0].y); for( int i = 1; i < points.counter; i++){ oPoint current = points.points[i]; smallestDistance = min(smallestDistance, dist(p.x, p.y, current.x, current.y)); } return smallestDistance; } void drawCircle(int x, int y, float radius) { radius = max(ceil(radius), minRadius); fill(getColorFromCircle(x,y,radius), 126); ellipse(x,y,radius,radius); if(radius > minRadius){ drawCircle(x, y, radius -1); } } color getColorFromCircle(int x, int y, float radius){ //we want to get a point that is radius away from x,y //the new radius is actually rand()*(radius - floor(radius)) + floor(radius) radius = random(floor(radius-1), radius) + radius; //let's pic a random angle around the circle. int angle = floor(random(0,360)); int newX = floor((cos(angle) * radius)) + x; int newY = floor((sin(angle) * radius)) + y; return source.get(newX, newY); } class oPoint{ int x; int y; oPoint(int xIn, int yIn){ x = xIn; y = yIn; } } class pointsArray { oPoint[] points; int counter; pointsArray(){ points = new oPoint[5]; counter = 0; } void addPoint(oPoint in){ if( counter == points.length){ oPoint[] temp = points; points = new oPoint[points.length+ 5]; for (int i = 0; i < counter; i++){ points[i] = temp[i]; } } points[counter++] = in; } } float produceNormalDistributionNumber(){ float a = random(0,2); float b = random(0,2); float c = random(0,2); float d = random(0,2); return (a + b + c + d)/4; }