import java.awt.Point; import processing.core.PApplet; import processing.core.PGraphics; import processing.core.PGraphics2; public class PICNorthernLights extends PApplet { int frame = (int)random(0,100000); float a = 100f; float b = 98f; float c = 50f; int[] sinLookupTable = new int[1024]; PCB[] pcbs = new PCB[4]; LED[] allLeds = new LED[8*pcbs.length]; PGraphics2 tempG = new PGraphics2(50,50,null); public void setup() { for(int i = 0; i < 1024; i++) { float var = (float)(i) * ((PI / 2) / 1024f); sinLookupTable[i] = (int)(255 * sin(var)); } frame = 0; tempG.defaults(); size (500,500); pcbs[0] = new PCB(new Point(0,0)); pcbs[1] = new PCB(new Point(tempG.width/2,0)); pcbs[2] = new PCB(new Point(0,tempG.height/2)); pcbs[3] = new PCB(new Point(tempG.width/2,tempG.height/2)); for(int i = 0; i < allLeds.length; i++) { if(i/8 <= 2) { allLeds[i] = pcbs[i/8].leds[i%8]; } else { allLeds[i] = pcbs[i/8].leds[7-i%8]; } float r = random(1f); if (r < .45) { allLeds[i].blue = 255; allLeds[i].green = 0; allLeds[i].red = 0; } else if(r < .9) { allLeds[i].blue = 0; allLeds[i].green = 255; allLeds[i].red = 0; } else { allLeds[i].blue = 255; allLeds[i].green = 200; allLeds[i].red = 0; } } framerate(24); // smooth(); } public void draw() { tempG.background(0); c = (float)(275 - (sinLookup(frame/10))); b = (float)(100 - (sinLookup(frame/50))/3f); a = (float)(100 - (sinLookup(frame/100))/3f); int rate = 125; c /= 10f; b /= 10f; a /= 10f; for(int i = 0; i < allLeds.length; i++) { allLeds[i].brightness = (sinLookup((int)(rate*(frame+3*i^2)/a)) * sinLookup((int)(rate*(frame+3*i^2)/b)) * cosLookup((int)(rate*(frame+3*i^2)/c)))/(255*255); //allLeds[i].brightness /= 2; // println(sinLookup((frame+10*i^2)/a)); } for(int i = 0; i < pcbs.length; i++) { pcbs[i].render(); } tempG.filter(BLUR,5); image(tempG.get(0,0, tempG.width, tempG.height),0,0,width,height); frame+=3; if(frame % 100 == 0) { println(frame + " a=" + a + " b=" + b + " c=" + c); } } public void mouseReleased() { for(int i = 0; i < allLeds.length; i++) { float r = random(1f); if (r < .45) { allLeds[i].blue = 255; allLeds[i].green = 0; allLeds[i].red = 0; } else if(r < .9) { allLeds[i].blue = 0; allLeds[i].green = 255; allLeds[i].red = 0; } else { allLeds[i].blue = 255; allLeds[i].green = 200; allLeds[i].red = 0; } } } int sinLookup(int x) { int tableLength = sinLookupTable.length; if((x / tableLength) % 2 != 1) { // ascending return sinLookupTable[x%tableLength]; } else { return sinLookupTable[tableLength - (x%tableLength) - 1]; } } int cosLookup(int x) { int tableLength = sinLookupTable.length; if((x / tableLength) % 2 == 1) { // ascending return sinLookupTable[(x%tableLength)]; } else { return sinLookupTable[tableLength - (x%tableLength) - 1]; } } static public void main(String args[]) { PApplet.main(new String[] { "PICNorthernLights" }); } public class PCB { Point basePosition; float scale = 3.65f; int yOffset = 5; int xOffset = 5; LED[] leds = new LED[8]; PCB(Point _basePosition) { basePosition = _basePosition; leds[0] = new LED(new Point((int)(scale * .7f) + xOffset,(int)(scale * 3.1f) + yOffset),0,255,0,this); leds[1] = new LED(new Point((int)(scale * 2f) + xOffset,(int)(scale * 3.1f) + yOffset),0,0,255,this); leds[2] = new LED(new Point((int)(scale * 3.4f) + xOffset,(int)(scale * 3.1f) + yOffset),0,255,0,this); leds[3] = new LED(new Point((int)(scale * 1.3f) + xOffset,(int)(scale * 1.6f) + yOffset),0,255,0,this); leds[4] = new LED(new Point((int)(scale * 2.7f) + xOffset,(int)(scale * 1.6f) + yOffset),0,200,255,this); leds[5] = new LED(new Point((int)(scale * .7f) + xOffset,(int)(scale * .04f) + yOffset),0,255,0,this); leds[6] = new LED(new Point((int)(scale * 2f) + xOffset,(int)(scale * .04f) + yOffset),0,0,255,this); leds[7] = new LED(new Point((int)(scale * 3.4f) + xOffset,(int)(scale * .04f) + yOffset),0,0,255,this); } void render() { for(int i = 0; i < leds.length; i++) { leds[i].render(); } } } public class LED { PCB board; Point position; int red; int green; int blue; int brightness; int size = 12; LED(Point _position,int _red,int _green,int _blue, PCB _board) { position = _position; red = _red; green = _green; blue = _blue; board = _board; brightness = 127; } Point getAbsolutePosition() { return new Point(position.x + board.basePosition.x, position.y + board.basePosition.y); } void render() { Point pos = getAbsolutePosition(); float fBrightness = (float)(brightness)/255f; tempG.fill(fBrightness*red,fBrightness*green,fBrightness*blue); tempG.ellipse(pos.x,pos.y,size,size); } } }