/** * Two sides of a coin. * * Move the mouse over the coins to view detail * "+" - zoom in * "-" - zoom out * "t" - toggle heat map */ PImage thumb; PImage detail; int scaling = 2; float baseWidth; float baseHeight; int margin; float gridCount = 16; int lastPos = -1; PImage[] coin = new PImage[256]; boolean forceRefresh = false; boolean heatMapOn = false; int[] frequency = new int[256]; int maxReference = 0; PFont f; void setupScaling() { margin = height/2; gridCount = 16 / scaling; baseWidth = width/gridCount; baseHeight = (0.5*height/gridCount); } void printHelp() { colorMode(RGB); noStroke(); fill(0); rect(0,0,width,height/2); fill(255); textFont(f); text("hover mouse on the coin to view\nuse \"+\" to zoom in and \"-\" to zoom out\nuse \"t\" to toggle heat map", 120,100,400,200); } void loadImages() { for (int i = 1; i<=256; i++) { String s = str(i); if (s.length() == 1) //dunno how to format string in processing { s = "0" + s; } coin[i - 1] = loadImage("slices/cent_" + s + ".jpg"); } } void setup() { size(600,600); setupScaling(); background(0); thumb = loadImage("coin_small.jpg"); //tint(255, 100); image(thumb,0,margin,width,height/2); loadImages(); f = loadFont("GillSans-24.vlw"); printHelp(); for (int i=0;i<256;i++) { frequency[i] = 0; } frameRate(30); } void drawDetail(int currentPos) { //println("point:" + currentPos); for (int i = 1; i <= scaling;i++) //in row { for (int j = 1; j <= scaling;j++) //in column { int current = currentPos + (i - 1) * 16 + (j - 1); image( coin[current - 1],(j - 1) * width / scaling,(i - 1) * margin / scaling,width / scaling,margin / scaling); frequency[current - 1] = frequency[current - 1] + 1; if (frequency[current - 1] > maxReference) { maxReference = frequency[current - 1]; } } } } void draw() { if (mouseY >= margin) { int sectionY = int((mouseY - margin) / baseHeight); int sectionX = int(mouseX / baseWidth); int pos = int(sectionY * scaling * 16 + sectionX * scaling + 1); if (heatMapOn) { float hueValue = 0; float bright = 0; noStroke(); colorMode(HSB,100); for (int i = 0; i<256;i++) { hueValue = map(frequency[i],0,maxReference,50,0); bright = map(frequency[i],0,maxReference,0,100); fill(hueValue,99,bright,100); rect((i % 16) * 37.5, (i / 16) * 18.75, 37.5 ,18.75); } } else if (pos != lastPos || forceRefresh) { colorMode(RGB,100); noStroke(); noFill(); image(thumb,0,margin,width,height/2); stroke(255,0,0); rect(sectionX * baseWidth, margin + sectionY * baseHeight, baseWidth, baseHeight); drawDetail(pos); lastPos = pos; //println(sectionX + "," + sectionY);// + ":" + baseWidth + "," + baseHeight); forceRefresh = false; } } } void increaseScale() { if (scaling < 8) { scaling = scaling * 2; setupScaling(); forceRefresh = true; redraw(); } } void decreaseScale() { if (scaling > 1) { scaling = scaling / 2; setupScaling(); forceRefresh = true; redraw(); } } void keyPressed() { if (key == '-') { increaseScale(); } else if (key == '+' || key == '=') { decreaseScale(); } else if (key == 't') { heatMapOn = !heatMapOn; forceRefresh = true; redraw(); } else if (key == 'h') { //printHelp(); } }