//radiolaria.pde import processing.pdf.*; import traer.physics.*; ParticleSystem pSys = new ParticleSystem(0,.25); ArrayList particles = new ArrayList(); ArrayList forces = new ArrayList(); ArrayList springs = new ArrayList(); //size of grid int max_x = 60; int max_y = 54; int[][] grid = new int[max_x][max_y]; int[][] springMatrix = new int[max_x*max_y][max_x*max_y]; //spring strength float K = .2;//.4 float D = 1.5; //spring rest length float rest =.3; //.8 int NODE_SIZE= 5; int ZONE_SIZE=30; //COLORS float rrr= 200; float ggg= 200; float bbb= 200; color buttonColor = color(200); color attractColor=color(255,0,0); color repulseColor=color(0,0,255); color springColor = color(255,255,255); color springShadowColor= color(80); color columnColor= buttonColor; color borderColor = color(200); color buttonOutline= -1; color bottomButtonColor= color(255,0,0); int borderWeight =50; int buttonPadding = 0; int buttonWidth = 50; //int columnWidth = buttonWidth+buttonPadding; int columnWidth = 140; int springWidth = 500; int screenWidth = springWidth; int strokeWidth = 2; int springPadding = 25; int springPaddingTop = 50; int springPaddingBottom = 25; int fontSize = 12; int currTool = -1; PFont bFont; PFont pixel8; PFont pixel16; PFont arial11; PFont arialBold; ToolButton attButton; ToolButton repButton; ToolButton delButton; ToolButton cutButton; ToolButton healButton; PlayButton playButton; ClearButton clearButton; ShowButton showButton; SaveButton saveButton; float tick = .3; boolean paused = false; boolean show = true; boolean showCut = true; boolean record = false; float x_off = 0; float y_off = 0; float Scale = 12; float magnitude = .15; ArrayList hexes; void setup() { size(springWidth+2*springPadding,springWidth+columnWidth+2*springPadding); smooth(); strokeWeight(strokeWidth); hexes = new ArrayList(); println(width); clear(); x_off = -10+springPadding; y_off = springPadding; bFont = loadFont("Swiss.vlw"); pixel8= loadFont("pixel-8.vlw"); pixel16=loadFont("Arial-Black-16.vlw"); arial11=loadFont("arial-11.vlw"); arialBold = loadFont("Arial-Black-14.vlw"); textAlign(CENTER); for(int i = 0; i < max_x/2; i++) { for(int j = 0; j < max_y/2;j++) { if(i%2 == 0) { addHex(i*2,j*2); } else { addHex(i*2,j*2+1); } } } int buttonX = 25+buttonPadding; int buttonY = springWidth+buttonPadding+springPaddingTop+springPaddingBottom+20; attButton = new ToolButton(250+buttonX,buttonY,buttonWidth,buttonWidth,0); attButton.imageURL = "http://n-e-r-v-o-u-s.com/images/applet/attract.png"; attButton.fillColor = buttonColor; attButton.strokeColor = -1; repButton = new ToolButton(251+buttonX+buttonWidth,buttonY,buttonWidth,buttonWidth,1); repButton.imageURL = "http://n-e-r-v-o-u-s.com/images/applet/repulse.png"; repButton.fillColor = buttonColor; repButton.strokeColor = -1; int buttonH = 24; delButton = new ToolButton(252+buttonX+2*buttonWidth,buttonY,buttonWidth,buttonWidth,2); delButton.imageURL = "http://n-e-r-v-o-u-s.com/images/applet/delete.png"; delButton.fillColor = buttonColor; delButton.strokeColor = -1; cutButton = new ToolButton(253+buttonX+3*buttonWidth,buttonY,buttonWidth,buttonWidth,3); cutButton.imageURL = "http://n-e-r-v-o-u-s.com/images/applet/scissors.png"; cutButton.fillColor = buttonColor; cutButton.strokeColor = -1; healButton = new ToolButton(254+buttonX+4*buttonWidth,buttonY,buttonWidth,buttonWidth,4); healButton.imageURL = "http://n-e-r-v-o-u-s.com/images/applet/bandaid.png"; healButton.fillColor = buttonColor; healButton.strokeColor = -1; playButton = new PlayButton(buttonX, 12, 50,buttonH); playButton.name = "PLAY"; playButton.fillColor = bottomButtonColor; playButton.strokeColor = buttonOutline; clearButton = new ClearButton(buttonX+153, 12, 50,buttonH); clearButton.name = "CLEAR"; clearButton.fillColor = bottomButtonColor; clearButton.strokeColor = buttonOutline; showButton = new ShowButton(buttonX+51, 12, 50,buttonH); showButton.name = "SHOW"; showButton.fillColor = bottomButtonColor; showButton.strokeColor = buttonOutline; saveButton = new SaveButton(buttonX+102, 12, 50,buttonH); saveButton.name = "SAVE"; saveButton.fillColor = bottomButtonColor; saveButton.strokeColor = buttonOutline; } void drawDescription() { fill(0); textFont(pixel16,16); String title= "nervous system \nradiolaria applet"; textAlign(LEFT); text(title,springPadding,springWidth+buttonPadding+springPaddingTop+springPaddingBottom,250,40); textFont(arial11,11); String description = "A physics simulation featuring particles linked together by springs into a hexagonal grid. Use the tools at right to distort and animate the web."; fill(0); text(description,springPadding,springWidth+buttonPadding+springPaddingTop+springPaddingBottom+40,250,200); } void draw() { if(!paused) pSys.tick(tick); if (paused) playButton.name = "PLAY"; else playButton.name = "PAUSE"; if (show) showButton.name = "HIDE"; else showButton.name = "SHOW"; if(record) { beginRecord(PDF, "radiolaria-####.pdf"); } background(rrr,ggg,bbb); drawNetwork(); if(record) { record = false; endRecord(); } textAlign(CENTER); drawInterface(); drawDescription(); } void drawNetwork() { noFill(); /* for ( int i = 0; i < pSys.numberOfSprings(); ++i ) { strokeWeight(strokeWidth); Spring e = pSys.getSpring( i ); stroke(springShadowColor); if (e.isOff() && showCut) { strokeWeight(1); stroke(255,0,0); } if (e.isOn() || showCut) { Particle a = e.getOneEnd(); Particle b = e.getTheOtherEnd(); if((x_off+(a.position().x())*Scale >= springPadding && x_off+(a.position().x())*Scale <= springWidth+springPadding && y_off+(a.position().y())*Scale >=springPaddingTop && y_off+(a.position().y())*Scale <= springWidth+springPaddingTop) || (x_off+(b.position().x())*Scale <= springWidth+springPadding && x_off+(b.position().x())*Scale >= springPadding && y_off+(b.position().y())*Scale >=springPaddingTop && y_off+(b.position().y())*Scale <= springWidth+springPaddingTop)) { line( x_off+(a.position().x())*Scale, y_off+(a.position().y())*Scale+2,x_off+(b.position().x())*Scale, y_off+(b.position().y())*Scale+2); } } } */ strokeWeight(strokeWidth); stroke(springColor); for ( int i = 0; i < pSys.numberOfSprings(); ++i ) { Spring e = pSys.getSpring( i ); stroke(255); if (!e.isOn()) { stroke(255,0,0); } if (e.isOn() || show) { Particle a = e.getOneEnd(); Particle b = e.getTheOtherEnd(); if((x_off+(a.position().x())*Scale >= springPadding && x_off+(a.position().x())*Scale <= springWidth+springPadding && y_off+(a.position().y())*Scale >=springPaddingTop && y_off+(a.position().y())*Scale <= springWidth+springPaddingTop) || (x_off+(b.position().x())*Scale <= springWidth+springPadding && x_off+(b.position().x())*Scale >= springPadding && y_off+(b.position().y())*Scale >=springPaddingTop && y_off+(b.position().y())*Scale <= springWidth+springPaddingTop)) { line( x_off+(a.position().x())*Scale, y_off+(a.position().y())*Scale,x_off+(b.position().x())*Scale, y_off+(b.position().y())*Scale); } } } //RENDER THE PARTICLES///////////////////////////////////////////////// noStroke(); if(show) { for ( int i = 0; i < forces.size(); ++i ){ ArrayList tAttract= (ArrayList) forces.get(i); Attract fAttract = (Attract) tAttract.get(0); if (fAttract.isOn()) { Particle p = fAttract.getOneEnd(); //transparent zone big color currColor = color(0); if (fAttract.strength < 0) { currColor = repulseColor; } else { currColor = attractColor; } fill(currColor,30); ellipse( x_off+p.position().x()*Scale, y_off+p.position().y()*Scale, ZONE_SIZE*2, ZONE_SIZE*2 ); //transparent zone big fill(currColor,15); ellipse( x_off+p.position().x()*Scale, y_off+p.position().y()*Scale, ZONE_SIZE*3, ZONE_SIZE*3 ); fill(currColor,90); ellipse( x_off+p.position().x()*Scale, y_off+p.position().y()*Scale, ZONE_SIZE, ZONE_SIZE ); //center fill(currColor); ellipse( x_off+p.position().x()*Scale, y_off+p.position().y()*Scale, NODE_SIZE, NODE_SIZE ); } } } ////////////////////////////// } Spring addSpringGrid(int x1, int y1, int x2, int y2,float xpos1,float ypos1,float xpos2,float ypos2) { if(springMatrix[x1*max_y+y1][x2*max_y+y2] == -1) { if(grid[x1][y1] == -1) { Particle p = pSys.makeParticle(1,xpos1,ypos1,0); if(x1==0 || y1==0 || x1==max_x-1 || y1 == max_y-1) { p.makeFixed(); } grid[x1][y1] = particles.size(); particles.add(p); } if(grid[x2][y2] == -1) { Particle p = pSys.makeParticle(1,xpos2,ypos2,0); if(x2 ==0 || y2==0 || x2==max_x-1 || y2==max_y-1) { p.makeFixed(); } grid[x2][y2] = particles.size(); particles.add(p); } Particle p1 = (Particle) particles.get(grid[x1][y1]); Particle p2 = (Particle) particles.get(grid[x2][y2]); springMatrix[x1*max_y+y1][x2*max_y+y2] = springs.size(); springMatrix[x2*max_y+y2][x1*max_y+y1] = springs.size(); Spring s = pSys.makeSpring(p1,p2,K,D,rest); springs.add(s); return s; } else { return (Spring) springs.get(springMatrix[x1*max_y+y1][x2*max_y+y2]); } } void addHex(int x, int y) { //check edges if(x < 0 || y < 1 || x > max_x-4 || y > max_y-2) { return; } float basey = y*sqrt(3.0)/2.0; float basex = int(x/2)*1.5; if(x%2 == 1) basex +=.5; ArrayList newHex = new ArrayList(); newHex.add(addSpringGrid(x,y,x+1,y+1, basex, basey, basex+.5, basey+sqrt(3.0)/2.0)); newHex.add(addSpringGrid(x+1,y+1,x+2,y+1, basex+.5, basey+sqrt(3.0)/2.0, basex+1.5, basey+sqrt(3.0)/2.0)); newHex.add(addSpringGrid(x+2,y+1,x+3,y, basex+1.5, basey+sqrt(3.0)/2.0, basex+2, basey)); newHex.add(addSpringGrid(x+3,y,x+2,y-1, basex+2, basey, basex+1.5, basey-sqrt(3.0)/2.0)); newHex.add(addSpringGrid(x+2,y-1,x+1,y-1, basex+1.5, basey-sqrt(3.0)/2.0, basex+.5, basey-sqrt(3.0)/2.0)); newHex.add(addSpringGrid(x+1,y-1,x,y, basex+.5, basey-sqrt(3.0)/2.0, basex, basey)); hexes.add(newHex); } void drawInterface() { fill(columnColor); noStroke(); rect(0,springWidth+2*springPadding,springWidth+2*springPadding, columnWidth); stroke(borderColor); noFill(); strokeWeight(springPadding); rect(springPadding/2.0,springPaddingTop/2.0+springPadding/2.0,springWidth+springPadding, springWidth+springPaddingTop/2.0+springPaddingBottom/2.0); strokeWeight(strokeWidth); noStroke(); fill(bottomButtonColor); rect(springPadding, 12,springWidth, 24); fill(0); textFont(arialBold,14); text("Tools:",300,springWidth+buttonPadding+springPaddingTop+springPaddingBottom+10); attButton.render(); repButton.render(); delButton.render(); cutButton.render(); healButton.render(); playButton.render(); clearButton.render(); showButton.render(); saveButton.render(); } void keyPressed() { if(key == 'p') { paused = !paused; } else if (key == 'r') { record = true; } else if (key == 'z') { // Scale = Scale * 0.9; } else if (key == 'x') { //Scale = Scale * 1.1; } } void mouseClicked(){ if (mouseButton == LEFT && mouseY < springWidth+springPaddingTop && mouseY > springPaddingTop && mouseX > springPadding && mouseX < springWidth+springPadding) { if (currTool == 0) { addForce((mouseX-x_off)/Scale,(mouseY-y_off)/Scale,1); } else if (currTool == 1) { addForce((mouseX-x_off)/Scale,(mouseY-y_off)/Scale,-1); } else if (currTool == 2) { deleteForce((mouseX-x_off)/Scale,(mouseY-y_off)/Scale); } else if (currTool == 3) { deleteSpring((mouseX-x_off)/Scale,(mouseY-y_off)/Scale); } else if (currTool == 4) { healSpring((mouseX-x_off)/Scale,(mouseY-y_off)/Scale); } } } void addForce(float x, float y, float mag) { Particle newParticle = pSys.makeParticle(1,x,y,0); newParticle.makeFixed(); repel(newParticle,magnitude*mag); } void deleteForce(float x, float y) { float disty = 1000; ArrayList attractions = null; float newDisty; for (int i = 0; i x && mouseX < x+w && mouseY > y && mouseY < y+h) { currTool = toolId; } } } void render() { tint(fillColor); stroke(strokeColor); if(currTool == toolId) { fill(lerpColor(fillColor, color(0), .5)); stroke(lerpColor(strokeColor, color(255), .5)); } if (fillColor == -1) noTint(); if (strokeColor == -1) noStroke(); rect(x,y,w,h); if(imageURL != "") { if(toolImage == null) { toolImage = loadImage(imageURL); } image(toolImage,x,y,w,h); } else if(name != "") { textFont(bFont,fontSize); fill(0); //textAlign(CENTER); text(name,x,y+h/2-5,w,h); } if(currTool == toolId) { noStroke(); fill(0,100); rect(x,y,w,h); } if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { noFill(); stroke(50); rect(x,y,w,h); } } } public class PlayButton { float x,y,w,h; String name = ""; color fillColor, strokeColor; String imageURL = ""; PImage toolImage = null; PlayButton(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; registerMouseEvent(this); } void mouseEvent(MouseEvent e) { switch(e.getID()) { case MouseEvent.MOUSE_CLICKED: if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { paused = !paused; } } } void render() { fill(fillColor); stroke(strokeColor); if (fillColor == -1) noFill(); if (strokeColor == -1) noStroke(); rect(x,y,w,h); if(imageURL != "") { if(toolImage == null) { toolImage = loadImage(imageURL); } image(toolImage,x,y,w,h); } else if(name != "") { textFont(bFont,fontSize); fill(255); // textAlign(CENTER); text(name.toLowerCase(),x,y+h/2-5,w,h); } if(paused) { noStroke(); fill(0,50); rect(x,y,w,h); } if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { noFill(); stroke(50); rect(x,y,w,h); } } } public class ClearButton { float x,y,w,h; String name = ""; color fillColor, strokeColor; String imageURL = ""; PImage toolImage = null; int timer =0; ClearButton(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; registerMouseEvent(this); } void mouseEvent(MouseEvent e) { switch(e.getID()) { case MouseEvent.MOUSE_CLICKED: if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { for(int j=0;j0) --timer; if(mousePressed && mouseX >= x && mouseX <= x+w && mouseY >=y && mouseY <= y+h|| timer > 0) { noStroke(); fill(0,50); rect(x,y,w,h); } if(mouseX >= x && mouseX <= x+w && mouseY >=y && mouseY <= y+h) { noFill(); stroke(50); rect(x,y,w,h); } } } public class ShowButton { float x,y,w,h; String name = ""; color fillColor, strokeColor; String imageURL = ""; PImage toolImage = null; ShowButton(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; registerMouseEvent(this); } void mouseEvent(MouseEvent e) { switch(e.getID()) { case MouseEvent.MOUSE_CLICKED: if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { show = !show; showCut = !showCut; } } } void render() { fill(fillColor); stroke(strokeColor); if (fillColor == -1) noFill(); if (strokeColor == -1) noStroke(); rect(x,y,w,h); if(imageURL != "") { if(toolImage == null) { toolImage = loadImage(imageURL); } image(toolImage,x,y,w,h); } else if(name != "") { textFont(bFont,fontSize); fill(255); // textAlign(CENTER); text(name.toLowerCase(),x,y+h/2-5,w,h); } if(!show) { noStroke(); fill(0,50); rect(x,y,w,h); } if(mouseX >= x && mouseX <= x+w && mouseY >=y && mouseY <= y+h) { noFill(); stroke(50); rect(x,y,w,h); } } } public class SaveButton { float x,y,w,h; String name = ""; color fillColor, strokeColor; String imageURL = ""; PImage toolImage = null; int timer = 0; SaveButton(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; registerMouseEvent(this); } void mouseEvent(MouseEvent e) { switch(e.getID()) { case MouseEvent.MOUSE_CLICKED: if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { saveRadiolaria(); timer =3; } } } void render() { fill(fillColor); stroke(strokeColor); if (fillColor == -1) noFill(); if (strokeColor == -1) noStroke(); rect(x,y,w,h); if(imageURL != "") { if(toolImage == null) { toolImage = loadImage(imageURL); } image(toolImage,x,y,w,h); } else if(name != "") { textFont(bFont,fontSize); fill(255); // textAlign(CENTER); text(name.toLowerCase(),x,y+h/2-5,w,h); } if(timer >0) --timer; if(mousePressed && mouseX >= x && mouseX <= x+w && mouseY >=y && mouseY <= y+h|| timer > 0) { noStroke(); fill(0,50); rect(x,y,w,h); } if(mouseX >= x && mouseX <= x+w && mouseY >=y && mouseY <= y+h) { noFill(); stroke(50); rect(x,y,w,h); } } } public class Attract { float strength; Attraction a; Attract(Particle p1, Particle p2, float s, float minDist) { a = pSys.makeAttraction(p1,p2,s,minDist); strength = s; } void turnOff() { a.turnOff(); } boolean isOn() { return a.isOn(); } boolean isOff() { return a.isOff(); } Particle getOneEnd() { return a.getOneEnd(); } Particle getTheOtherEnd() { return a.getTheOtherEnd(); } } public class POSTRequest extends Thread { String content; String urlString; String output = ""; POSTRequest(String _c, String _u) { super(); content = _c; urlString = _u; } void start() { super.start(); } void run() { try{ URL url; HttpURLConnection urlConn; DataOutputStream printout; url = new URL (urlString); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput (true); urlConn.setDoInput(true); urlConn.setRequestMethod("POST"); printout = new DataOutputStream (urlConn.getOutputStream ()); printout.writeBytes (content); printout.flush (); printout.close (); //println(message); BufferedReader in = new BufferedReader( new InputStreamReader( urlConn.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { output += inputLine; println(inputLine); } in.close(); } catch (Exception e) { exit(); } } }