diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_01/Synthesis_02_waves_01.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_01/Synthesis_02_waves_01.pde new file mode 100644 index 000000000..31d2dd90f --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_01/Synthesis_02_waves_01.pde @@ -0,0 +1,23 @@ +/** + * Synthesis 1: Form and Code + * Riley Waves by Casey Reas (www.processing.org) + * p. 151 + * + * Step 1, creating the basic form. +*/ + + +size(400, 400); +background(255); +float angle = 0.0; +float magnitude = 24.0; + +beginShape(TRIANGLE_STRIP); +for(int x=0; x<=width; x=x+8) { + float y = 50 + (sin(angle)* magnitude); + angle += PI/48.0; + float y2 = 70 + (sin(angle+PI/6)* magnitude); + vertex(x, y); + vertex(x, y2); +} +endShape(); diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_02/Synthesis_02_waves_02.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_02/Synthesis_02_waves_02.pde new file mode 100644 index 000000000..b923a3b55 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_02/Synthesis_02_waves_02.pde @@ -0,0 +1,27 @@ +/** + * Synthesis 1: Form and Code + * Riley Waves by Casey Reas (www.processing.org) + * p. 151 + * + * Step 2, the basic form is drawn many times inside a for structure. +*/ + + +size(400, 400); +background(255); +float angle = 0.0; +int magnitude = 18; + +for(int i =- magnitude; i < height+magnitude; i = i+24) { + + beginShape(TRIANGLE_STRIP); + for(int x = 0; x <= width; x = x+8) { + float y = i + (sin(angle)* magnitude); + angle += PI/24.0; + float y2 = i+10 + (sin(angle+PI/12) * magnitude); + vertex(x, y); + vertex(x, y2); + } + endShape(); + +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_03/Synthesis_02_waves_03.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_03/Synthesis_02_waves_03.pde new file mode 100644 index 000000000..5932fa877 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_03/Synthesis_02_waves_03.pde @@ -0,0 +1,70 @@ +/** + * Synthesis 1: Form and Code + * Riley Waves by Casey Reas (www.processing.org) + * p. 151 + * + * Step 3, values are modified to create a new pattern. +*/ + + +size(1200, 280); +background(255); +smooth(); +noStroke(); +float angle = 0.0; +float angle2 = 0.0; +int magnitude = 8; + +for(int i = -magnitude; i < height+magnitude; i = i+18) { + + angle2 = angle; + + fill(0); + beginShape(TRIANGLE_STRIP); + for(int x=0; x<=width; x=x+8) { + float y = i + (sin(angle)* magnitude); + angle += PI/24.0; + float y2 = i+4 + (sin(angle+PI/12)* magnitude); + vertex(x, y); + vertex(x, y2); + } + endShape(); + + fill(204); + beginShape(TRIANGLE_STRIP); + for(int x=0; x<=width; x=x+8) { + float y = i+4 + (sin(angle2+PI/12)* magnitude); + angle2 += PI/24.0; + float y2 = i+8 + (sin(angle2+PI/12)* magnitude); + vertex(x, y); + vertex(x, y2); + } + endShape(); + + /* + fill(0); + beginShape(TRIANGLE_STRIP); + for(int x=0; x<=width; x=x+8) { + float y = i + (sin(angle)* magnitude); + angle += PI/16.0; + float y2 = i+4 + (sin(angle+PI/24)* magnitude); + vertex(x, y); + vertex(x, y2); + } + endShape(); + + fill(204); + beginShape(TRIANGLE_STRIP); + for(int x=0; x<=width; x=x+8) { + float y = i+4 + (sin(angle2+PI/24)* magnitude); + angle2 += PI/16.0; + float y2 = i+8 + (sin(angle2+PI/24)* magnitude); + vertex(x, y); + vertex(x, y2); + } + endShape(); + */ +} + + +// save("Synthesis-2--1.tif"); diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_04/Synthesis_02_waves_04.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_04/Synthesis_02_waves_04.pde new file mode 100644 index 000000000..fb80ac2bd --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_04/Synthesis_02_waves_04.pde @@ -0,0 +1,34 @@ +/** + * Synthesis 1: Form and Code + * Riley Waves by Casey Reas (www.processing.org) + * p. 151 + * + * Step 3, values are modified to create a new pattern. +*/ + + +size(1200, 280); +background(255); +smooth(); +noStroke(); +fill(0); +float angle = 0.0; +int magnitude = 16; + +for(int i = -magnitude; i < width+magnitude; i = i+16) { + + fill(float(i)/width * 255); + + beginShape(TRIANGLE_STRIP); + for(int y = 0; y <= height; y = y+6) { + float x = i + (sin(angle)* magnitude); + angle += PI/22.0; + float x2 = i+8 + (sin(angle+PI/22)* magnitude); + vertex(x, y); + vertex(x2, y); + } + endShape(); + +} + +// saveFrame("Synthesis-2--2.tif"); diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_05/Synthesis_02_waves_05.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_05/Synthesis_02_waves_05.pde new file mode 100644 index 000000000..423453ad9 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_02_waves_05/Synthesis_02_waves_05.pde @@ -0,0 +1,37 @@ +/** + * Synthesis 1: Form and Code + * Riley Waves by Casey Reas (www.processing.org) + * p. 151 + * + * Step 3, values are modified to create a new pattern. +*/ + + +size(1200, 280); +background(255); +smooth(); +noStroke(); +//fill(0); +float angle = PI; +float angle2 = PI; +int magnitude = 3; + +for(int i = -magnitude; i < height+magnitude; i = i+12) { + + angle2 = angle; + + fill(0); + beginShape(TRIANGLE_STRIP); + for(int x = 0; x <= width; x = x+8) { + float y = i + (sin(angle)* magnitude); + angle += PI/24.0; + float y2 = i+4 + (sin(angle+PI/12)* magnitude); + vertex(x, y); + vertex(x, y2); + } + endShape(); + +} + + +// save("Synthesis-2--3.tif"); diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_01/Synthesis_03_grids_01.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_01/Synthesis_03_grids_01.pde new file mode 100644 index 000000000..fe719e4f3 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_01/Synthesis_03_grids_01.pde @@ -0,0 +1,20 @@ +/** + * Synthesis 1: Form and Code + * Wilson Grids by Casey Reas (www.processing.org) + * p. 152 + * + * Step 1, basic embedded for structure. +*/ + + +size(600, 600); +background(255); +int sqNum = 12; +int sqSize = width/sqNum; +int halfSize = sqSize/2; + +for (int y = halfSize; y < width; y = y+sqSize) { + for (int x = halfSize; x < height; x = x+sqSize) { + rect(x-halfSize+2, y-halfSize+2, sqSize-4, sqSize-4); + } +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_02/Synthesis_03_grids_02.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_02/Synthesis_03_grids_02.pde new file mode 100644 index 000000000..8f5b4a361 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_02/Synthesis_03_grids_02.pde @@ -0,0 +1,30 @@ +/** + * Synthesis 1: Form and Code + * Wilson Grids by Casey Reas (www.processing.org) + * p. 152 + * + * Step 2, textures inside the grid by adding a third for structure. +*/ + + +size(600, 600); +background(255); +int numSquares = 12; +int gap = 4; +int sqSize = (width-gap*2)/numSquares; +int halfSize = sqSize/2; +int offset = halfSize+gap; +int ydiv = sqSize/12; + +for(int y = 0; y < numSquares; y++) { + for(int x = 0; x < numSquares; x++) { + float xp = offset + (x*sqSize); + float yp = offset + (y*sqSize); + for(int i=0; i<=sqSize-gap*2; i=i+ydiv) { + float yy = i*halfSize+gap+i; + line(xp-halfSize+gap, yp-halfSize+gap+i, xp+halfSize-gap, yp-halfSize+gap+i); + } + } + +} + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_03/Synthesis_03_grids_03.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_03/Synthesis_03_grids_03.pde new file mode 100644 index 000000000..9d4d5f670 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_03_grids_03/Synthesis_03_grids_03.pde @@ -0,0 +1,30 @@ +/** + * Synthesis 1: Form and Code + * Wilson Grids by Casey Reas (www.processing.org) + * p. 152 + * + * Step 3, changing the size of each grid element. +*/ + + +size(600, 600); +background(0); +stroke(255, 204); +int numSquares = 6; +int gap = 8; +int sqSize = (width-gap*2)/numSquares; + +for (float y=0; y 2 ) { + co = 1 - k/float(accuracy); + break; // NOTE: This is new syntax!!! + } + } + stroke(color(255*co, 255*co, 255*co)); + point(i, j); + } +} + + +// save("Synthesis-04--" + zoom + ".tif"); diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_05_tennis/Synthesis_05_tennis.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_05_tennis/Synthesis_05_tennis.pde new file mode 100644 index 000000000..4dc267f43 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_05_tennis/Synthesis_05_tennis.pde @@ -0,0 +1,103 @@ +/** + * Synthesis 2: Input and Response + * Tennis by Casey Reas (www.processing.org) + * p. 256 + * + * Only the right paddle works. As a challenge, try to + * add code to activate the left paddle. You can make decide to make + * it a one-player or two-player game. As an additional challenge, + * have the program keep score. + */ + + +int ballX; +int ballY; +int ballDir = 1; +int ballSize = 10; // Radius +float ballAngle = 0.0; // Direction + +// Global variables for the paddle +int paddleWidth = 20; +int paddleHeight = 40; + +int wallGap = 50; + +int netSegment; + +void setup() { + size(640, 480); + noStroke(); + ballY = height/2; + ballX = 0; + noCursor(); + netSegment = height/32; +} + +void draw() { + background(0); + + stroke(255); + // Draw Net + for(int i=0; i width+ballSize*2) { + ballX = -ballSize; + ballY = int(random(0, height-ballSize)); + ballAngle = 0; + println(ballX + ":" + ballY + ":" + ballAngle); + } + + if(ballX < -ballSize*2) { + ballX = width; + ballY = int(random(0, height-ballSize)); + ballAngle = 0; + println(ballX + ":" + ballY + ":" + ballAngle); + } + + // Constrain paddle to screen + float paddleY = constrain(mouseY, 0, height-paddleHeight); + + // Test to see if the ball is touching the paddle + float py = width-wallGap-ballSize; + if(ballX >= py && ( ballY+ballSize >= paddleY && ballY <= paddleY + paddleHeight)) { + + ballDir *= -1; + if(mouseY != pmouseY) { + ballAngle = (mouseY-pmouseY)/2.0; + ballAngle = constrain(ballAngle, -5, 5); + } + } + + // If ball hits back wall, reverse direction + if(ballX < 0) { + ballDir *= -1; + } + + // If the ball is touching top or bottom edge, reverse direction + if((ballY > height-ballSize) || (ballY < 0)) { + ballAngle = ballAngle * -1; + } + + // Draw ball + fill(255); + rect(ballX, ballY, ballSize, ballSize); + + // Draw the paddle + fill(153); + rect(width-wallGap, paddleY, paddleWidth, paddleHeight); + + // Draw the paddle + rect(wallGap, height-paddleY-paddleHeight, paddleWidth, paddleHeight); +} + + +void keyPressed() { + //saveFrame("pong-####.tif"); +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_06_cursor/Synthesis_06_cursor.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_06_cursor/Synthesis_06_cursor.pde new file mode 100644 index 000000000..ef52995ee --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_06_cursor/Synthesis_06_cursor.pde @@ -0,0 +1,304 @@ +/** + * Synthesis 2: Input and Response + * Cursor by Peter Cho (www.typotopo.com) + * p. 257 + * + * There are four modes, each is a different + * way to use the input from the mouse to control + * the cursor(s). Click on a number to select that + * mode. + */ + + +PFont fontA; +int gx, gy; +int mode, nextmode; +int nummodes; +boolean forapplet = false; + +float mx, my, lastmx, lastmy; +float lastrot, lastsc; + +float bgx, bgy; + + +float p_x, p_y; +float p_fx, p_fy; +float p_v2, p_vx, p_vy; +float p_a2, p_ax, p_ay; +float p_mass, p_drag; + + +void setup() +{ + size(600, 600); + gx = width; + gy = height; + size(gx, gy); + colorMode(RGB, 1.0); + strokeWeight(1.3); + loop(); + smooth(); + strokeJoin(ROUND); + + init_particle(0.6, 0.9, width/2, height/2); + + fontA = loadFont("NewsGothicBT-Bold-48.vlw"); + nummodes = 4; + mode = 1; + + bgx = 0; + bgy = 0; + mx = gx/2; + my = gy/2; + + noCursor(); + + if (forapplet) { + cursor(CROSS); + } +} + + +void init_particle(float _mass, float _drag, float ox, float oy) +{ + p_x = ox; + p_y = oy; + p_v2 = 0.0f; + p_vx = 0.0f; + p_vy = 0.0f; + p_a2 = 0.0f; + p_ax = 0.0f; + p_ay = 0.0f; + p_mass = _mass; + p_drag = _drag; +} + +void iterate_particle(float fkx, float fky) +{ + // iterate for a single force acting on the particle + p_fx = fkx; + p_fy = fky; + p_a2 = p_fx*p_fx + p_fy*p_fy; + if (p_a2 < 0.0000001) return; + p_ax = p_fx/p_mass; + p_ay = p_fy/p_mass; + p_vx += p_ax; + p_vy += p_ay; + p_v2 = p_vx*p_vx + p_vy*p_vy; + if (p_v2 < 0.0000001) return; + p_vx *= (1.0 - p_drag); + p_vy *= (1.0 - p_drag); + p_x += p_vx; + p_y += p_vy; +} + +void drawCursor(float x, float y, float myscale, float myrot) { + // draw generic arrow cursor + if (forapplet) y -= gy/2; + pushMatrix(); + translate(x, y); + rotate(myrot); + scale(myscale, myscale); + beginShape(POLYGON); + vertex(7, 21); + vertex(4, 13); + vertex(1, 16); + vertex(0, 16); + vertex(0, 0); // tip of cursor shape + vertex(1, 0); + vertex(12, 11); + vertex(12, 12); + vertex(7, 12); + vertex(10, 20); + vertex(9, 21); + vertex(7, 21); + endShape(); + popMatrix(); +} + +void blurdot(float x, float y, float sc) { + ellipse(x, y, sc*5, sc*5); + ellipse(x, y, sc*23, sc*23); + ellipse(x, y, sc*57, sc*57); + ellipse(x, y, sc*93, sc*93); +} + +void drawBlurCursor(float x, float y, float myscale, float dotval) { + // draw generic arrow cursor + if (forapplet) y -= gy/2; + pushMatrix(); + translate(x, y); + scale(myscale, myscale); + float dotval2 = .5 + (1-dotval)*5; + dotval = .5 + (1-dotval)*9; + blurdot(7, 21, dotval2); + blurdot(1, 16, dotval); + blurdot(0, 8, dotval2); // midpt + blurdot(0, 0, dotval); // tip of cursor shape + blurdot(1, 0, dotval2); + blurdot(6, 6, dotval); // midpt + blurdot(12, 12, dotval2); + blurdot(10, 20, dotval); + popMatrix(); +} + + +int n = 1; +void keyPressed() { + if (key == '1') { + mode = 1; + } else if (key == '2') { + mode = 2; + } else if (key == '3') { + mode = 3; + } else if (key == '4') { + mode = 4; + } + + // saveFrame("cursor--" + mode + "-" + n + ".tif"); + n++; +} + +boolean isInside(float x, float y, float rx, float ry, float rw, float rh) { + return (x >= rx && x <= rx+rw && y >= ry && y <= ry+rh); +} + +void scrollbg(float x, float y) { + // scroll the bg + float amt = 30; + if (x < gx*.5) { + bgx += amt*(gx*.5 - x)*(gx*.5 - x)/(gx*gx); + } else if (x > gx - gx*.5) { + bgx -= amt*(x - (gx-gx*.5))*(x - (gx-gx*.5))/(gx*gx); + } + bgx = bgx % gx; + if (bgx < 0) bgx += gx; + + if (y < gy*.5) { + bgy += amt*(gy*.5 - y)*(gy*.5 - y)/(gy*gy); + } else if (y > gy - gy*.5) { + bgy -= amt*(y - (gy-gy*.5))*(y - (gy-gy*.5))/(gy*gy); + } + bgy = bgy % gy; + if (bgy < 0) bgy += gy; +} + +void draw() +{ + lastmx = mx; + lastmy = my; + + mx = mouseX; + my = mouseY; + + if (mode == 3) { + mx = mx*.01 + lastmx*.99; + my = my*.01 + lastmy*.99; + + } else if (mode == 4) { + mx = mx*.25 + lastmx*.75; + my = my*.25 + lastmy*.75; + + } else { + mx = mx*.5 + lastmx*.5; + my = my*.5 + lastmy*.5; + } + + iterate_particle(.15*(-p_x+mx), .15*(-p_y+my)); + scrollbg(p_x, p_y); + + background(.8,.8,.8); + // Set the font and its size (in units of pixels) + textFont(fontA, 24); + + float x, y; + int w=95, h=75; + for (int i=0; i 5) { + mode = 1; + } + } + else if (key == BACKSPACE) { + if(mode == 1) { + if(text1.length() > 0) { + text1 = text1.substring(0, text1.length() - 1); + } + } + else if (mode ==2 ) { + if(text2.length() > 0) { + text2 = text2.substring(0, text2.length() - 1); + } + } + else if (mode == 3) { + if(text3.length() > 0) { + text3 = text3.substring(0, text3.length() - 1); + } + } + else if (mode == 4) { + if(text4.length() > 0) { + text4 = text4.substring(0, text4.length() - 1); + } + } + else if (mode == 5) { + if(text5.length() > 0) { + text5 = text5.substring(0, text5.length() - 1); + } + } + } + else { + if(mode == 1) { + text1 += key; + } + else if (mode ==2 ) { + text2 += key; + } + else if (mode == 3) { + text3 += key; + } + else if (mode == 4) { + text4 += key; + } + else if (mode == 5) { + text5 += key; + } + } +} + +void mousePressed() { + mx = mouseX; + my = mouseY; +} + +void mouseMoved() { + x = mouseX; + y = mouseY; + mx = mouseX; + my = mouseY; + + if(mode == 1) { + x1 = x; + y1 = y; + } + else if (mode ==2 ) { + x2 = x; + y2 = y; + } + else if (mode == 3) { + x3 = x; + y3 = y; + } + else if (mode == 4) { + x4 = x; + y4 = y; + } + else if (mode == 5) { + x5 = x; + y5 = y; + } +} + +void mouseDragged() { + //float mr = float(mouseX - mx) / width * TWO_PI; + + + float mr = atan2(mouseY-my, mouseX-mx); + //float ms = abs(float(mouseY-my) / height * 5.0) + 0.25; + float ms = dist(mouseX, mouseY, mx, my) / 100.0; + if(mode == 1) { + r1 = mr; + s1 = ms; + } + else if (mode ==2 ) { + r2 = mr; + s2 = ms; + } + else if (mode == 3) { + r3 = mr; + s3 = ms; + } + else if (mode == 4) { + r4 = mr; + s4 = ms; + } + else if (mode == 5) { + r5 = mr; + s5 = ms; + } +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_07_typing/data/TheSerif-Plain-128.vlw b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_07_typing/data/TheSerif-Plain-128.vlw new file mode 100644 index 000000000..d03189b65 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_07_typing/data/TheSerif-Plain-128.vlw differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_07_typing/data/TheSerif-Plain-96.vlw b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_07_typing/data/TheSerif-Plain-96.vlw new file mode 100644 index 000000000..246476063 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_07_typing/data/TheSerif-Plain-96.vlw differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_08_clock/Synthesis_08_clock.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_08_clock/Synthesis_08_clock.pde new file mode 100644 index 000000000..c11ce559d --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_08_clock/Synthesis_08_clock.pde @@ -0,0 +1,190 @@ +/** + * Synthesis 2: Input and Response + * Banded Clock by Golan Levin (www.flong.com) + * p. 259 + * + * + * + */ + + +//================================================ +int prevX = 0; +int prevY = 0; +int clickX = 0; +int clickY = 0; + +final int NCOLORS = 256; +color colorArray[]; +float S, M, H; +int Scolors[]; +int Mcolors[]; +int Hcolors[]; +int ys0, ys1; +int ym0, ym1; +int yh0, yh1; + +float Soffset = 0; +float Hoffset = 0; +float Moffset = 0; +float Svel = 0; +float Hvel = 0; +float Mvel = 0; +float damp = 0.94f; + +int mil, sec, minut, hou; +int milError = 0; +int canvasWidth; +int canvasHeight; + +//================================================ +void setup(){ + + size(600, 600); + canvasWidth = width; + canvasHeight = height; + + Scolors = new int[canvasWidth]; + Mcolors = new int[canvasWidth]; + Hcolors = new int[canvasWidth]; + colorArray = new color[NCOLORS]; + for (int i=0; i sec){ + milError = millis()%1000; + } +} + +//------------------ +float GMOD (float A, float B){ + return (float)(A - (floor(A/B)*B)); +} + +//------------------ +int wave(float a){ + // inexpensive ramp function, + // but not as nice as true sine wave (below) + int val = 0; + float cscale = 2.0f*255.0f; + if (a < 0.5){ + val = (int) round (a *cscale); + } + else { + val = (int) round ((1.0f-a)*cscale); + } + return val; +} + +//------------------ +int sinWave (float a){ + // expensive trigonometric function, but nicer looking + float sina = (1.0+sin(TWO_PI*a))*255.0/2.0; + int val = (int)round(sina); + return val; +} + +//-------------------------------------------------------------------------- +// interaction methods + +void mousePressed (){ + prevX = mouseX; + prevY = mouseY; + clickX = mouseX; + clickY = mouseY; +} + +void mouseDragged() { + // allow bands to be shifted around, for "fun" + float accel = (prevX - mouseX)*0.004f; + if ((clickY >= ys0) && (clickY < ys1)){ + Svel += accel; + } + else if ((clickY >= ym0) && (clickY < ym1)){ + Mvel += accel; + } + else if ((clickY >= yh0) && (clickY < yh1)){ + Hvel += accel; + } + prevX = mouseX; + prevY = mouseY; +} + + + + +void keyPressed() { + saveFrame("clock--" + hour() + "-" + minute() + "-" + second() + ".tif"); +} + + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_09_centipede/Synthesis_09_centipede.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_09_centipede/Synthesis_09_centipede.pde new file mode 100644 index 000000000..76959237a --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_09_centipede/Synthesis_09_centipede.pde @@ -0,0 +1,90 @@ +/** + * Synthesis 3: Motion and Arrays + * Centipede by Ariel Malka (www.chronotext.org) + * p. 372 + */ + + +float x, y; +float node_length = 30; +float node_size = node_length-1; +int n_nodes = 70; +float[] nodes_x; +float[] nodes_y; +float delay = 20; +color col_head = color(255, 0, 0); +color col_body = color(0); + + +void setup() +{ + size(600, 600); + smooth(); + noStroke(); + + x = width/2; + y = height/2; + + int r1 = 10; + int r2 = 100; + int dr = r2-r1; + float D = 0; + + nodes_x = new float[n_nodes]; + nodes_y = new float[n_nodes]; + + for (int i=0; i= 0) && (mouseX < width) && (mouseY >= 0) && (mouseY < height)){ + drawline(mouseX, mouseY, pmouseX, pmouseY); + if (fat) { + drawthickline(mouseX, mouseY, pmouseX, pmouseY, 2); + drawdot(mouseX+1, mouseY); + drawdot(mouseX, mouseY+1); + drawdot(mouseX, mouseY-1); + drawdot(mouseX-1, mouseY); + drawdot(mouseX+1, mouseY-1); + drawdot(mouseX+1, mouseY+1); + drawdot(mouseX-1, mouseY-1); + drawdot(mouseX-1, mouseY+1); + } + } + } + + // draw frames + int p = currentpic; + for (int j=0; j= framerest) { + framecounter = 0; + currentpic--; + if (currentpic < 0) { + currentpic = buflength - 1; + } + } + + // btncheck + if (keyPressed && key==' ') { + btnheight = height / 2; + if (mouseY < btnheight && mouseY >=0){ + framestep = - int(btnreso / 2) + int(mouseX/(width/btnreso)); + //println(framestep); + } else if (mouseY > btnheight && mouseY < height){ + framerest = int(mouseX/(width/btnreso))*btnresomulti; + } + } else { + btnheight = 8; + } + + // drawgrid + if (grid) { + drawgrid(); + } + + // drawbtn1 + noStroke(); + fill(0,0,255,60); + int btnxoffset = width/btnreso * (btnreso / 2 + framestep); + rect(btnxoffset, 0, width/btnreso, btnheight); + + // drawbtn2 + fill(255,0,0,60); + btnxoffset = width/btnreso * framerest / btnresomulti; + rect(btnxoffset, height - btnheight, width/btnreso, btnheight); +} + +void clearall(){ + for (int i=0; i=sizey) { + y=sizex>>1; + for(int i=0;i=sizex) { + y-=sizex; + county+=incy; + } + countx+=incx; + drawdot(countx, county); + } + } else { + x=sizey>>1; + for(int i=0;i=sizey) { + x-=sizey; + countx+=incx; + } + county+=incy; + drawdot(countx, county); + } + } +} + + +//idem +void drawthickline(int x1, int y1, int x2, int y2, int thickness) { + + int dX = x2 - x1; + int dY = y2 - y1; + + double lineLength = Math.sqrt(dX * dX + dY * dY); + double scale = (double)(thickness) / (2 * lineLength); + double ddx = -scale * (double)dY; + double ddy = scale * (double)dX; + ddx += (ddx > 0) ? 0.5 : -0.5; + ddy += (ddy > 0) ? 0.5 : -0.5; + int dx = (int)ddx; + int dy = (int)ddy; + + int xPoints[] = new int[4]; + int yPoints[] = new int[4]; + + xPoints[0] = x1 + dx; yPoints[0] = y1 + dy; + xPoints[1] = x1 - dx; yPoints[1] = y1 - dy; + xPoints[2] = x2 - dx; yPoints[2] = y2 - dy; + xPoints[3] = x2 + dx; yPoints[3] = y2 + dy; + + drawline(xPoints[0], yPoints[0], xPoints[1], yPoints[1]); + drawline(xPoints[1], yPoints[1], xPoints[2], yPoints[2]); + drawline(xPoints[2], yPoints[2], xPoints[3], yPoints[3]); + drawline(xPoints[3], yPoints[3], xPoints[0], yPoints[0]); +} + + + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_11_amoebaAbstract/Synthesis_11_amoebaAbstract.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_11_amoebaAbstract/Synthesis_11_amoebaAbstract.pde new file mode 100644 index 000000000..607b23578 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_11_amoebaAbstract/Synthesis_11_amoebaAbstract.pde @@ -0,0 +1,175 @@ +/** + * Synthesis 3: Motion and Arrays + * AmoebaAbstract_03 by Marius Watz + * p. 374 + * + * Move the mouse to control the speed of the elements. Click to + * restart with different colors. + * + * Notes from Marius: + * Abstract computational animation for the exhibition "Abstraction Now", + * Kunstlerhaus Vienna, 29.08-28.09 2003. + * You are allowed to play with this code as much as you like, but + * you may not publish pieces based directly on it. +*/ + + +int num,cnt,colNum,colScheme; +float h,maxSpeed; +float[] x,y,speed; +boolean initialised; +float[] colR,colG,colB; + +void setup() { + size(700, 400); + background(255); + smooth(); + frameRate(30); + + cnt=0; + maxSpeed=8; + num=20; + x=new float[num]; + y=new float[num]; + speed=new float[num]; + h=height/num; + for(int i=0; i50) speed[i]=-speed[i]; + } + noStroke(); + + colR=new float[1000]; + colG=new float[1000]; + colB=new float[1000]; + + colScheme=-1; + initColors(); +} + +void draw() { + int c; + + cnt++; + for(int i=0; i width) { + x[i] -= width; + } + + c = ((i*11+cnt*3+int(speed[i]*10))/20)%colNum; + if(c<0) { + c=0; + } + fill(colR[c],colG[c],colB[c],150); + + if(i%2==0) { + rect(x[i]%width, y[i], 6, h); + } + else { + ellipse(x[i]%width, y[i]+h/2, h-6, h-6); + } + } + + stroke(255,255,255,10); + noFill(); + strokeWeight(2); + for(int i=0; i < num-3; i++) { + line(x[i], y[i]+h/2, x[(i+3)%num], y[(i+3)%num]+h/2); + line(x[num-i-1], y[num-i-1]+h/2, x[(num-i-2)%num], y[(num-i-2)%num]+h/2); + } + noStroke(); + fill(255); + rect(0, 0, 2, height); + rect(width-2, 0, 2, height); +} + +void initColors() { + colNum=0; + colScheme=(colScheme+1)%3; + if(colScheme==0) { + addColor(12, 100,200,255, 50,100,128); + addColor(8, 26,41,58, 50,100,128); + addColor(2, 255,255,255, 255,255,255); + addColor(16, 0,0,50, 40,40,80); + addColor(12, 100,200,255, 50,100,128); + addColor(8, 26,41,58, 50,100,128); + addColor(6, 0,200,20, 0,255,100); + addColor(2, 255,255,255, 255,255,255); + addColor(16, 0,0,50, 40,40,80); + addColor(5, 255,200,0, 255,170,0); + } + else if(colScheme==1) { + addColor(20, 255,0,100, 128,0,0); + addColor(6, 255,100,0, 255,150,0); + addColor(6, 128,0,0, 50,20,20); + addColor(12, 255,255,255, 255,100,100); + addColor(4, 255,100,0, 102,0,0); + } + else if(colScheme==2) { + addColor(14, 128,163,120, 27,53,38); + addColor(8, 64,95,0, 225,227,0); + addColor(8, 0,150,150, 215,255,255); + addColor(4, 168,106,170, 235,183,237); + addColor(14, 128,163,120, 27,53,38); + addColor(8, 64,95,0, 225,227,0); + addColor(8, 0,150,150, 215,255,255); + addColor(12, 92,18,96, 217,111,226); + } +} + +void addColor(float r,float g,float b) { + colR[colNum]=r; + colG[colNum]=g; + colB[colNum]=b; + colNum++; +} + +void addColor(int num, float r1,float g1,float b1, float r2,float g2,float b2) { + + r2=(r2-r1)/float(num-1); + g2=(g2-g1)/float(num-1); + b2=(b2-b1)/float(num-1); + + for(int i=0; i20 && abs(mx-mouseX)>5){ + image (body,0,135); + image (head,0,sinval*20-40); + } + else{ + image (body2,0,135); + image (head2,0,sinval*20-40); + } + interpolate(); + popMatrix(); + pushMatrix(); + if (abs(my-mouseY)>20 && abs(mx-mouseX)>5){ + battery(true); + } + else{ + battery (false); + } + popMatrix(); +} + +void battery(boolean test){ + if (test==true){ + image (batt, mouseX-50, mouseY-20); + } + else{ + image (batteat, mouseX-50+random(-20,20), mouseY-20+random(-20,20)); + } +} + +void interpolate(){ + float diffx = mouseX-mx; + if(abs(diffx) > 1) { + mx = mx + diffx/delay; + } + float diffy = mouseY-my; + if(abs(diffy) > 1) { + my = my + diffy/delay; + } +} + +void keyPressed() { + // saveFrame("robot-####.tif"); +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/battalpha.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/battalpha.gif new file mode 100644 index 000000000..8c2ed3ea0 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/battalpha.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/batteat.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/batteat.gif new file mode 100644 index 000000000..36a12ae34 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/batteat.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/batteatalpha.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/batteatalpha.gif new file mode 100644 index 000000000..19ce7a9be Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/batteatalpha.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/headalpha.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/headalpha.gif new file mode 100644 index 000000000..0527d026f Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_12_robot/data/headalpha.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/Module.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/Module.pde new file mode 100644 index 000000000..83b628975 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/Module.pde @@ -0,0 +1,18 @@ + +class Module { + int i; + float x, y, myAngle, myRadius, dir; + float mx = width/2; + float my = height/2; + float delay = 40.0; + + Module(int spriteNum, float xx, float yy, float deg, float rad, float pp) { + i = spriteNum; + x = xx; + y = yy; + myAngle = deg; + myRadius = rad; + dir = pp; + } + +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/ModuleA.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/ModuleA.pde new file mode 100644 index 000000000..4743b5b66 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/ModuleA.pde @@ -0,0 +1,33 @@ + +class ModuleA extends Module { + + ModuleA(int spriteNum, float xx, float yy, float deg, float rad, float pp) { + super(spriteNum, xx, yy, deg, rad, pp); + } + + void updateMe() { + float mh = x - mouseX; + float mv = y - mouseY; + float mdif = sqrt(mh*mh+mv*mv); + float dh = width/2 - mouseX; + float dv = height/2 - mouseY; + float ddif = sqrt(dh*dh+dv*dv); + if(dir == 1){ + myAngle += abs(ddif - mdif)/50.0; + } + else{ + myAngle -= abs(ddif - mdif)/50.0; + } + myRadius += mdif/100.00; + if(myRadius > width){ + myRadius = random(10,40); + } + mx += (mouseX - mx)/delay; + my += (mouseY - my)/delay; + x = mx + (myRadius * cos(radians(myAngle))); + y = my + (myRadius * sin(radians(myAngle))); + stroke(num/(i+1), num/(i+1), num/(i+1)); + point(x,y); + } + +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/ModuleB.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/ModuleB.pde new file mode 100644 index 000000000..50bb85667 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/ModuleB.pde @@ -0,0 +1,27 @@ + +class ModuleB extends Module { + + ModuleB(int spriteNum, float xx, float yy, float deg, float rad, float pp) { + super(spriteNum, xx, yy, deg, rad, pp); + } + + void updateMe(){ + mx += (mouseX - mx)/delay; + my += (mouseY - my)/delay; + x = mx + (myRadius * cos(radians(myAngle))); + y = my + (myRadius * sin(radians(myAngle))); + stroke(num/2, num/2, num/2); + point(x,y); + + // from connectMe2 + noStroke(); + fill(0, num/7.0, num/(i+1)+num/4.0, 20); + beginShape(QUADS); + vertex(modsA[i].x, modsA[i].y); + vertex(modsA[i].x+1, modsA[i].y+1); + vertex(x, y); + vertex(x+1, y+1); + endShape(); + } + +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/Synthesis_13_withoutTitle.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/Synthesis_13_withoutTitle.pde new file mode 100644 index 000000000..1e5ccb570 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/Synthesis_13_withoutTitle.pde @@ -0,0 +1,78 @@ +/** + * Synthesis 4: Structure and Interface + * WithoutTitle by Lia (http://lia.sil.at) + * p. 496 + * + * Move the mouse to create the drawing. Click to refresh the window. + */ + + +int num = 50; +ModuleA[] modsA; +ModuleB[] modsB; +boolean initialized = false; + +void setup() { + size(600, 600); + colorMode(RGB, num); + background(num); + smooth(); + + modsA = new ModuleA[num]; + modsB = new ModuleB[num]; + + for (int i=0; i 0.5) { + qq = 1; + } else { + qq = -1; + } + float x = random(width); + float y = random(height); + float angle = random(360); + float direction = random(10, 40); + modsA[i] = new ModuleA(i, x, y, angle, direction, qq); + modsB[i] = new ModuleB(i, x, y, angle, direction, qq); + } + +} + +void draw() { + if(initialized == true){ + for (int i=0; i width) || (modsA[i].y < 0) || (modsA[i].y > height) ){ + modsA[i].x = mouseX; + modsA[i].y = mouseY; + modsB[i].x = mouseX; + modsB[i].y = mouseY; + float a = random(360); + modsA[i].myAngle = a; + modsB[i].myAngle = a; + float r = random(10, 40); + modsA[i].myRadius = r; + modsB[i].myRadius = r+i*i; + } + } + } +} + +void mousePressed() { + background(num); +} + +void mouseMoved() { + initialized = true; +} + +void keyPressed() { + // saveFrame("withouttitle-####.tif"); +} + + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/Thumbs.db b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/Thumbs.db new file mode 100644 index 000000000..4d6134850 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/Thumbs.db differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_down.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_down.gif new file mode 100644 index 000000000..2b300b2c2 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_down.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_hi.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_hi.gif new file mode 100644 index 000000000..6f971e9e0 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_hi.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_lo.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_lo.gif new file mode 100644 index 000000000..5db0ee614 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/down_lo.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_down.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_down.gif new file mode 100644 index 000000000..0a2367d69 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_down.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_hi.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_hi.gif new file mode 100644 index 000000000..10bf1e299 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_hi.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_lo.gif b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_lo.gif new file mode 100644 index 000000000..e919d4bb6 Binary files /dev/null and b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_13_withoutTitle/data/up_lo.gif differ diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_14_pond/Synthesis_14_pond.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_14_pond/Synthesis_14_pond.pde new file mode 100644 index 000000000..e663426f3 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_14_pond/Synthesis_14_pond.pde @@ -0,0 +1,671 @@ +/** + * Synthesis 4: Structure and Interface + * Pond by William Ngan (www.metaphorical.net) + * p. 197 + * + * Click to generate ripples and attract the fish. + * Press keys 0-3 to turn that number of big fish ON or OFF. + */ + + +int NUM = 75; // number of fish + +Fish[] flock = new Fish[NUM]; +Fish bigfish1; +Fish bigfish2; +Fish bigfish3; + +//ripple +float rippleX, rippleY; +float[] ripple = new float[20]; +boolean hasRipple; +int countRipple; + +int hasPredator = 1; // Number of predator (big fish) + + +void setup() { + + size(600, 600); + + colorMode(HSB, 360, 100, 100); + background(85,46,83); + + frameRate(30); + + // Small fish + for (int i=0; i0) { + + bigfish1.scanPrey( flock, 150f ); + bigfish1.predator( bigfish2.x, bigfish2.y, 100f, 6*PI/180f, 2f); + bigfish1.predator( bigfish3.x, bigfish3.y, 100f, 6*PI/180f, 2f); + bigfish1.predator( mouseX, mouseY, 50f, 5*PI/180f, 1f); + bigfish1.move(); + stroke( bigfish1.colour[0], bigfish1.colour[1], bigfish1.colour[2]); + bigfish1.getFish(); + + if (hasPredator>1) { + bigfish2.scanPrey( flock, 120f ); + bigfish2.predator( bigfish1.x, bigfish1.y, 100f, 5*PI/180f, 1.5f); + bigfish2.predator( bigfish3.x, bigfish3.y, 100f, 5*PI/180f, 1.5f); + bigfish2.predator( mouseX, mouseY, 50f, 4*PI/180f, 0.8f); + bigfish2.move(); + stroke( bigfish2.colour[0], bigfish2.colour[1], bigfish2.colour[2]); + bigfish2.getFish(); + + if (hasPredator>2) { + bigfish3.scanPrey( flock, 100f ); + bigfish3.predator( bigfish1.x, bigfish1.y, 100f, 5*PI/180f, 1.5f); + bigfish3.predator( bigfish2.x, bigfish2.y, 100f, 5*PI/180f, 1.5f); + bigfish3.predator( mouseX, mouseY, 50f, 3*PI/180f, 0.5f); + bigfish3.move(); + stroke( bigfish3.colour[0], bigfish3.colour[1], bigfish3.colour[2]); + bigfish3.getFish(); + } + } + } + + + // Draw small fish + noStroke(); + for (int i=0; i0) { + flock[i].predator( bigfish1.x, bigfish1.y, 100f, 8*PI/180f, 1.5f); + if (hasPredator>1) { + flock[i].predator( bigfish2.x, bigfish2.y, 100f, 8*PI/180f, 1.5f); + if (hasPredator>2) flock[i].predator( bigfish3.x, bigfish3.y, 100f, 8*PI/180f, 1.5f); + } + } + if (!hasRipple) flock[i].predator( mouseX, mouseY, 100f, 5*PI/180f, 1f); + flock[i].move(); + flock[i].getFish(); + + } + + // Draw ripple + stroke(84,66,96); + noFill(); + + if (hasRipple) { + if (countRipple>0) { // ripple done, but active for another second + countRipple++; + } + else { // draw ripple + countRipple = 1; + for (int k=0; k60) ? false : true; + } + +} + + +void mouseDragged() { + rippleX = mouseX; + rippleY = mouseY; +} + +void mousePressed() { + rippleX = mouseX; + rippleY = mouseY; +} + +void mouseReleased() { + if (!hasRipple) { + for (int k=0; k 1) { // if it has energy + wcount+=energy*2; // tail sine-wave movement + } + + // Sine-wave oscillation + if (wcount>120) { + wcount = 0; + energy =0; + } + + wave = sin( wcount*3*PI/180 ); //sine wave + float strength = energy/5 * tailLength/2; //tail strength + + // Set tail position + setTail( strength, wave ); + move(); + } + + + ////////////////////////////////// / + + + // Find distance + float dist( float px, float py ) { + px -= x; + py -= y; + return sqrt( px*px + py*py ); + } + + float dist( Fish p ) { + float dx = p.x - x; + float dy = p.y - y; + return sqrt( dx*dx + dy*dy ); + } + + // Find angle + float angle( float px, float py ) { + return atan2( (py-y), (px-x) ); + } + + float angle( Fish p ) { + return atan2( (p.y-y), (p.x-x) ); + } + + // Move one step + void move() { + x = x+( cos(angle)*speed ); + y = y+( sin(angle)*speed ); + } + + // Speed change + void speedChange( float inc ) { + speed += inc; + if (speedmaxSpeed) { speed=maxSpeed; } + } + + // Direction change + void angleChange( float inc ) { + angle += inc; + } + + // Set speed limit + void setSpeedLimit( float max, float min ) { + maxSpeed = max; + minSpeed = min; + } + + // Set angle + void setAngle( float a ) { + angle = a; + } + + // Turn towards an angle + void turnTo( float ta, float inc ) { + + if (angle < ta) { + angleChange( inc ); + } + else { + angleChange( inc*-1 ); + } + } + + + // Set Color + void setColor( float c1, float c2, float c3 ) { + colour[0] = c1; + colour[1] = c2; + colour[2] = c3; + } + + + // Copy another fish's angle and pos + void copyFish( Fish f ) { + x = f.x; + y = f.y; + angle = f.angle; + speed = f.speed; + } + + ////////////////////////////////// + + // Check bounds and U-turn when near bounds + boolean checkBounds( float turn ) { + + boolean inbound = false; + + turn += boundTime/100; + + // Calculate the "buffer area" and turning angle + float gap = speed * PI/2/turn; + if (gap > width/4) { + gap = width/4; + turn = (gap/speed)/PI/2; + } + + // Which direction to u-turn? + if ( x-gap < 0 || x+gap > width || y-gap < 0 || y+gap > height) { + + if (uturn == 0) { + + float temp = angle; + if (temp < 0) temp += PI*2; + + if ( temp >0 && tempPI/2 && tempPI && tempPI*3/2 && temp height) uturn *=-1; + } + + // Turn + angleChange( turn*uturn ); + + inbound = true; + + } + else { // when out, clear uturn + uturn = 0; + inbound = false; + } + + x = (x<0) ? 0 : ( (x>width) ? width : x ); + y = (y<0) ? 0 : ( (y>height) ? height : y ); + + isBound = inbound; + boundTime = (inbound) ? boundTime+1 : 0; + + return inbound; + + } + + + // Alignment -- move towards the same direction as the flock within sight + void align( Fish fp, float angleSpeed, float moveSpeed ) { + + turnTo( fp.angle, angleSpeed+random(angleSpeed*3) ); // 0.001 + + if ( speed > fp.speed ) { + speedChange( moveSpeed*(-1-random(1)) ); //0.2 + } + else { + speedChange( moveSpeed ); + } + + } + + + // Cohesion -- move towards the center of the flock within sight + void cohere( Fish[] flocks, float angleSpeed, float moveSpeed ) { + + // get normalised position + float nx = 0; + float ny = 0; + + for (int i=0; i0) tone-=2; + } + + speedChange( moveSpeed ); + } + + + // Attracts towards a point (ie, ripple) + void swarm( float px, float py, float d ) { + float dA = angle(px, py); + + turnTo( dA, d ); + if (isBound) { + turnTo( dA, d ); + } + } + + //////////////////////////// // + + // Scan for the environment and determines behaviour + void scanFlock( Fish[] flocks, float cohereR, float avoidR ) { + + + Fish[] near = new Fish[NUM]; + int nCount = 0; + Fish[] tooNear = new Fish[NUM]; + int tnCount = 0; + Fish[] collide = new Fish[NUM]; + int cCount = 0; + Fish nearest = null; + float dist = 99999; + + float tempA = angle; + + // Check boundaries + boolean inbound = (hasPredator>0) ? checkBounds(PI/16) : checkBounds( PI/24); + + + for (int i=0; i d ) { + dist = d; + nearest = fp; + } + if ( d <= avoidR ) { + tooNear[tnCount++] = fp; + if ( d <= avoidR/2 ) { + collide[cCount++] = fp; + } + } + } + } + + // Calc and make flocking behaviours + Fish[] near2 = new Fish[nCount]; + Fish[] tooNear2 = new Fish[tnCount]; + Fish[] collide2 = new Fish[cCount]; + + int j=0; + for (j=0; j d ) { + dist = d; + nearest = flocks[i]; + } + } + + // Move towards food + if (dist < range) { + + if (dist > range/2) { + speedChange( 0.5f ); + } + else { + speedChange( -0.5f ); + } + + turnTo( angle( nearest ), 0.05f ); + + float diffA = (angle - tempA)*10; + + float c = diffA*180/PI; + + energy += abs( c/150 ); + } + + // Check boundaries + checkBounds( PI/16 ); + + // Wriggle tail + wriggle(); + + } + + +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Branch.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Branch.pde new file mode 100644 index 000000000..4db33ac72 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Branch.pde @@ -0,0 +1,69 @@ + +class Branch { + int x1, y1, z1, x2, y2, z2, id; + String xyz1, xyz2; // xyz1 = starting point; xyz2 = endingpoint + + Branch parent = null; + Branch[] children; + int childCounter = 0; + float len,angle; + + Branch(String _id, String _xyz1, String _xyz2) { + id = stringToInt(_id); + String[] tmpList = split(_xyz1,' '); + x1 = stringToInt(tmpList[0]); + y1 = stringToInt(tmpList[1]); + z1 = stringToInt(tmpList[2]); + tmpList = split(_xyz2,' '); + x2 = stringToInt(tmpList[0]); + y2 = stringToInt(tmpList[1]); + z2 = stringToInt(tmpList[2]); + xyz1 = _xyz1; + xyz2 = _xyz2; + children = new Branch[4]; + calc2Dcoords(); + } + + void calc2Dcoords() { + len = sqrt(sq(this.x2-this.x1)+sq(this.y2-this.y1)); + angle = atan2(this.y2-this.y1,this.x2-this.x1); + } + + int stringToInt(String s) { + Integer tmp = new Integer(s); + return tmp.intValue(); + } + + boolean checkParent(Branch _b) { + if((x2==_b.x1) && (y2==_b.y1) && (z2==_b.z1)) { + children[this.childCounter++] = _b; + return true; + } else { + return false; + } + } + + void checkForParents() { + for(int i=0;i"+this.id+" *** child(s) ->"); + for(int i=0;i"+parent.id); + } else { + print(" *** ROOT"); + } + println(""); + } + +} diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Segment.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Segment.pde new file mode 100644 index 000000000..bd0f99cef --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Segment.pde @@ -0,0 +1,156 @@ + +class Segment { + + float x, y, z, len, angle; + float xAbsolute, yAbsolute, zAbsolute, angleAbsolute; + float originLength, futureLength; + + Segment parent = null; + Segment[] children = new Segment[4]; + int childCounter = 0; + int id; + int depth; + Branch branch = null; + + Segment(int _id) { id = _id;} + + void addChild(Segment _c) {children[childCounter++] = _c;} // println("adding child ->"+_c.id+" "+childCounter); + void setParent(Segment _p) {parent = _p;} + + void setXY(float _x,float _y) {x = _x; y = _y;} + void setX(float _x) {x = _x;} + void setY(float _y) {y = _y;} + void setZ(float _z) {z = _z;} + + void setAngle(float _a) {angle = _a;} + void setLength(float _len) {len = _len; originLength = _len; } + void setFutureToOrigin() {futureLength = originLength;} + void setFutureLength(float _len) {futureLength = _len;} + + void setXabsolute(float _xAbsolute) {xAbsolute = _xAbsolute;} + void setYabsolute(float _yAbsolute) {yAbsolute = _yAbsolute;} + void setZabsolute(float _zAbsolute) {zAbsolute = _zAbsolute;} + void setAngleAbsolute(float _angleAbsolute) {angleAbsolute = _angleAbsolute;} + + void scaleLength(float _scalar) {len *= _scalar;} + void scaleLengthFromOrigin(float _scalar) {len = originLength*_scalar;} + void scaleFutureLength(float _scalar) {futureLength *= _scalar;} + + float getX() {return x;} + float getY() {return y;} + float getZ() {return z;} + float getAngle() {return angle;} + float getLength() {return len;} + float getXabsolute() {return xAbsolute;} + float getYabsolute() {return yAbsolute;} + float getZabsolute() {return zAbsolute;} + float getAngleAbsolute() {return angleAbsolute;} + + void calcCoords() { + if(parent==null) { + angleAbsolute = angle; + setX(cos(radToPol(angle))*len); + setY(sin(radToPol(angle))*len); + setXabsolute(rootX+x); + setYabsolute(rootY-y); + } else { + angleAbsolute += ((parent.getAngleAbsolute()+angle)-angleAbsolute)/20; + setX(cos(radToPol(angleAbsolute))*len); + setY(sin(radToPol(angleAbsolute))*len); + setXabsolute(parent.getXabsolute()+x); + setYabsolute(parent.getYabsolute()-y); + } + } + + float radToPol(float _deg) { return _deg/57.2958; } + float polToRad(float _pol) { return _pol*57.2958; } + + void render() { + calcCoords(); + activateChildren(); + + originLength += (futureLength-originLength)/100; + len = originLength; + + if(parent==null) { + drawAsLine(rootX,rootY,getXabsolute(), getYabsolute()); + } else { + drawAsLine( parent.getXabsolute(), + parent.getYabsolute(), + getXabsolute(), + getYabsolute() + ); + } + } + + void activateChildren() { + for(int i=0;i<4;i++) { + if(children[i]!=null) { + children[i].render(); + } + } + } + + void setBranch(Branch _branch) { + branch = _branch; + try { + parent = s[branch.parent.id]; + for(int i=0;i<4;i++) { + if(branch.children[i]!=null) { + addChild(s[branch.children[i].id]); + } + } + } catch (NullPointerException npe) { + for(int i=0; i<4; i++) { + if(branch.children[i] != null) { + addChild(s[branch.children[i].id]); + } + } + } + } + + void setParamsFromBranch() { + try { + int tmp = branch.parent.id; + setParent(s[tmp]); + setAngle(polToRad(branch.angle)); + setAngleAbsolute(polToRad(branch.angle)); + } catch (NullPointerException npe){ + setParent(null); + setAngle(polToRad(branch.angle)); + setAngleAbsolute(polToRad(branch.angle)); + println("NULLPOINTER"); + } + setLength(branch.len); + } + + void adjustAngle(float _angle) { + for(int i=0;i<4;i++) { + if(children[i]!=null) { + children[i].adjustAngle(angle); + } + } + angle -= _angle; + } + + void adjustDepth(int _depth) { + depth = _depth; + for(int i=0;i<4;i++) { + if(children[i]!=null) { + children[i].adjustDepth(depth-1); + } + } + } + + void drawAsLine(float _x1, float _y1, float _x2, float _y2) { + strokeWeight(2); + if(id == redNum) { + stroke(255,0,0); + } else { + stroke(0, 80); + } + line(_x1,_y1,_x2,_y2); + } + +} + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Synthesis_15_tree.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Synthesis_15_tree.pde new file mode 100644 index 000000000..63c691c29 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_15_tree/Synthesis_15_tree.pde @@ -0,0 +1,102 @@ +/** + * Synthesis 4: Structure and Interface + * Swingtree by Andreas Schlegel (www.sojamo.de) at ART+COM (www.artcom.de) + * p. 498 + * + * Loads a data file to create the connections of the tree elements. + * The size and motion is affected by the mouse. + */ + + +Branch[] b; +Segment[] s; + +int rootX; +int rootY; +int rootId; +int redNum = 139; + +float pX,pY; +int frameCounter = 0; + +void setup() { + size(400, 900); + rootX = width/2; + rootY = height; + parseTree(); + for(int i=0; i " + rootId); + + for(int i=0; ix&&mouseXy&&mouseYspeedFrictionThreshold) + fs *= speedFrictionThreshold/speed; + xv *= fs; + yv *= fs; + + x += xv; + y += yv; + + if (xwidth-radius) { + x -= x-(width-radius); + xv = -xv; + } + if (yheight-radius) { + y -= y-(height-radius); + yv = -yv; + } + } + + void clamp() { + if (xwidth-radius) { + x = width-radius; + } + if (yheight-radius) { + y = height-radius; + } + } + + void display() { + if (this == overMass) { + stroke(0x00,0x99,0xFF); + line(x,y,mouseX,mouseY); + noStroke(); + fill(0x00,0x99,0xFF); + } + else { + noStroke(); + fill(0); + } + ellipse(x,y,diamter,diamter); + } + + float distanceTo(Mass m) { + return distanceTo(m.x,m.y); + } + + float distanceTo(float x,float y) { + float dx = this.x-x; + float dy = this.y-y; + return sqrt(dx*dx+dy*dy); + } +} + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Slider.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Slider.pde new file mode 100644 index 000000000..25a9b1772 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Slider.pde @@ -0,0 +1,48 @@ + +class Slider extends Control { + float min,max,value; + int labelW = 8; + + Slider(int x, int y, int w, int h, float min, float max, float value,String label) { + super(x,y,w,h,label); + this.min = min; + this.max = max; + this.value = value; + } + + void mouseDragged() { + setValueToMouse(); + } + + boolean mousePressed() { + boolean down; + if (down = super.mousePressed()) + setValueToMouse(); + return down; + } + + void setValueToMouse() { + int mw = (w-labelW)-1; + float mv = (mouseX-(x+labelW+1.0))/mw; + if (mv>0) + value = min+(mv*mv)*(max-min); + else + value = min; + value = min(value,max); + } + + void drawContents() { + fill(0x00,0x99,0xFF); + noStroke(); + int mw = (w-labelW)-1; + float vw = sqrt((value-min)/(max-min))*mw; + rect(x+labelW+1,y+1,vw,h-1); + } + + void display() { + super.display(); + stroke(0x00,0x99,0xFF); + line(x+labelW,y,x+labelW,y+h); + } +} + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Spring.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Spring.pde new file mode 100644 index 000000000..ba5cb5514 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Spring.pde @@ -0,0 +1,81 @@ + +class Spring { + Mass a,b; + float restLength; + + Spring(Mass a,Mass b) { + this.a = a; + this.b = b; + restLength = a.distanceTo(b); + } + + void display() { + if (this == overSpring) { + stroke(0x00,0x99,0xFF); + float vx = b.x-a.x; + float vy = b.y-a.y; + float dot = (vx*vx + vy*vy); + float rx = mouseX-a.x; + float ry = mouseY-a.y; + float dot2 = (vx*rx + vy*ry); + float value = dot2/dot; + value = min(value,1); + value = max(value,0); + float x = ((b.x*value)+(a.x*(1-value))); + float y = ((b.y*value)+(a.y*(1-value))); + line(x,y,mouseX,mouseY); + } + else { + stroke(0); + } + line(a.x,a.y,b.x,b.y); + } + + void applyForces() { + double d = a.distanceTo(b); + if (d>0) + { + double f = (d-restLength)*k.value; + double fH = (f/d)*(a.x-b.x); + double fV = (f/d)*(a.y-b.y); + a.xv -= fH; + a.yv -= fV; + b.xv += fH; + b.yv += fV; + } + } + + float distanceTo(float x,float y) { + if (x>(min(a.x,b.x)-mouseTolerance) + &&x<(max(a.x,b.x)+mouseTolerance) + &&y>(min(a.y,b.y)-mouseTolerance) + &&y<(max(a.y,b.y)+mouseTolerance)) + { + float vx = b.x-a.x; + float vy = b.y-a.y; + float dot = (vx*vx + vy*vy); + float rx = x-a.x; + float ry = y-a.y; + float dot2 = (vx*rx + vy*ry); + float value = dot2/dot; + + if (value<0) { + float d = a.distanceTo(x,y); + return d <= mouseTolerance?d:-1; + } else if (value>1) { + float d = b.distanceTo(x,y); + return d <= mouseTolerance?d:-1; + } + + float px = ((b.x*value)+(a.x*(1-value)))-x; + float py = ((b.y*value)+(a.y*(1-value)))-y; + + float d = sqrt(px*px+py*py); + + return d <= mouseTolerance?d:-1; + } + else + return -1; + } +} + diff --git a/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Synthesis_16_sodaprocessing.pde b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Synthesis_16_sodaprocessing.pde new file mode 100644 index 000000000..29e93cee5 --- /dev/null +++ b/java/examples/Books/Processing Handbook/Synthesis/Synthesis_16_sodaprocessing/Synthesis_16_sodaprocessing.pde @@ -0,0 +1,329 @@ +/** + * Synthesis 4: Structure and Interface + * SodaProcessing by Ed Burton (www.soda.co.k) + * p. 499 + * + * A simplified version of the Soda Constructor to demonstrate how it works. + * The complete Soda Constructor may be visited at http://www.sodaplay.com. + */ + +Mass masses[]; +int massCount = 0; + +float mouseTolerance = 15; + +Mass dragMass = null; +float dragDx,dragDy; +float tempDistance; + +Mass drawMass = null; +Mass overMass; + +Spring springs[]; +int springCount = 0; +Spring overSpring; + +Control controls[]; +Control activeControl = null; + +Button make,move,delete; +Slider g,f,k; +int sliderHeight = 13; + +float speedFrictionThreshold = 20; + +PFont font; + +int mode; +static final int MAKE = 0; +static final int MOVE = 1; +static final int DELETE = 2; + +String toolTips[]; + +void setup() +{ + size(600, 600); + background(0xFF); + + masses = new Mass[8]; + springs = new Spring[8]; + + font = loadFont("RotisSanSer-Bold.vlw.gz"); + textFont(font, 15); + //hint(SMOOTH_IMAGES); + smooth(); + controls = new Control[6]; + int x = 0; + int i = 0; + int controlWidth = width/(controls.length)+1; + controls[i++] = make = new Button(0,0,controlWidth-1,sliderHeight,"make"); + controls[i++] = move = new Button(controls[i-2].x+controls[i-2].w,0,controlWidth-1,sliderHeight,"move"); + controls[i++] = delete = new Button(controls[i-2].x+controls[i-2].w,0,controlWidth-1,sliderHeight,"delete"); + controls[i++] = g = new Slider(controls[i-2].x+controls[i-2].w,0,controlWidth,sliderHeight,0,4,0.0,"g"); + controls[i++] = f = new Slider(controls[i-2].x+controls[i-2].w,0,controlWidth,sliderHeight,0,1,0.1,"f"); + controls[i++] = k = new Slider(controls[i-2].x+controls[i-2].w,0,controlWidth,sliderHeight,0,0.75,0.5,"k"); + make.selected = true; + checkMode(); + + toolTips = new String [] { + ": click to make masses and springs" + ,": click, drag and throw masses" + ,": click to delete masses and springs" + ," = gravity (hint, set to zero to before choosing to make)" + ," = friction" + ," = spring stiffness"}; +} + +void draw() +{ + doUpdate(); + display(); +} + +void checkMode() { + for (int i = 0;i= 0) + deleteMassIndex(index); +} + +void deleteMassIndex(int index) { + if (index= 0) + deleteSpringIndex(index); +} + +void deleteSpringIndex(int index) { + if (index