wrapping up the synthesis moves
@@ -1,26 +0,0 @@
|
||||
/**
|
||||
* Synthesis 1: Form and Code,
|
||||
* Collage Engine by Casey Reas (www.processing.org)
|
||||
* p. 150
|
||||
*
|
||||
* Step 1, working with one image.
|
||||
*/
|
||||
|
||||
// Load the image
|
||||
PImage nyt01 = loadImage("nyt_01.jpg");
|
||||
|
||||
float x, y; // Image position
|
||||
float r; // Image rotation
|
||||
|
||||
size(400, 300);
|
||||
background(255);
|
||||
tint(255, 204);
|
||||
|
||||
x = random(width); // Set random x-coordinate
|
||||
y = random(height); // Set random y-coordinate
|
||||
r = random(0, TWO_PI); // Set random rotation
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt01, -nyt01.width/2, -nyt01.height/2);
|
||||
popMatrix();
|
||||
@@ -1,23 +0,0 @@
|
||||
/**
|
||||
* 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();
|
||||
@@ -1,27 +0,0 @@
|
||||
/**
|
||||
* 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();
|
||||
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* 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");
|
||||
@@ -1,34 +0,0 @@
|
||||
/**
|
||||
* 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");
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* 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");
|
||||
@@ -1,20 +0,0 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
/**
|
||||
* 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<numSquares; y=y+1) {
|
||||
for (float x=0; x<numSquares; x=x+1) {
|
||||
int sqArea = int(random(10, 100));
|
||||
int halfArea = sqArea/2;
|
||||
int ydiv = sqArea/12;
|
||||
float offset = sqSize/2+gap;
|
||||
float xp = offset + (x*sqSize);
|
||||
float yp = offset + (y*sqSize);
|
||||
for (int i = 0; i <= sqArea-gap*2; i = i+ydiv) {
|
||||
float yy = i*halfArea+gap+i;
|
||||
line(xp-halfArea+gap, yp-halfArea+gap+i, xp+halfArea-gap, yp-halfArea+gap+i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/**
|
||||
* Synthesis 1: Form and Code
|
||||
* Wilson Grids by Casey Reas (www.processing.org)
|
||||
* p. 152
|
||||
*
|
||||
* Step 4.
|
||||
*/
|
||||
|
||||
size(600, 600);
|
||||
background(0);
|
||||
stroke(255, 204);
|
||||
float numSquares = 6.0;
|
||||
int gap = 8;
|
||||
float xUnit = (width-gap*2)/numSquares;
|
||||
float yUnit = (height-gap*2)/numSquares;
|
||||
float squareWidth = xUnit;
|
||||
int squareHeight = int(yUnit);
|
||||
|
||||
for(float y=0; y<numSquares; y=y+1) {
|
||||
for(float x=0; x<numSquares; x=x+1) {
|
||||
squareWidth = int(random(10, 300));
|
||||
squareHeight = int(squareWidth);
|
||||
//squareHeight = int(random(10, 100));
|
||||
int ydiv = squareHeight/12;
|
||||
float xoff = xUnit/2+gap;
|
||||
float yoff = yUnit/2+gap;
|
||||
float xp = xoff + (x*xUnit);
|
||||
float yp = yoff + (y*yUnit);
|
||||
for(int i=0; i<=squareHeight-gap*2; i=i+ydiv) {
|
||||
float yy = i*squareHeight/2+gap+i;
|
||||
line(xp-squareWidth/2+gap, yp-squareHeight/2+gap+i, xp+squareWidth/2-gap, yp-squareHeight/2+gap+i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/**
|
||||
* Synthesis 1: Form and Code
|
||||
* Wilson Grids by Casey Reas (www.processing.org)
|
||||
* p. 152
|
||||
*
|
||||
* Step 5.
|
||||
*/
|
||||
|
||||
|
||||
size(600, 600);
|
||||
background(0);
|
||||
stroke(255, 204);
|
||||
float numSquares = 12.0;
|
||||
int gap = 4;
|
||||
float xUnit = (width-gap*2)/numSquares;
|
||||
float yUnit = (height-gap*2)/numSquares;
|
||||
float squareWidth = xUnit;
|
||||
float squareHeight = yUnit;
|
||||
|
||||
for (float y = 0; y < numSquares; y = Y + 1) {
|
||||
for (float x = 0; x < numSquares; x = x+1) {
|
||||
float ydiv = squareHeight/12;
|
||||
float xoff = xUnit/2+gap;
|
||||
float yoff = yUnit/2+gap;
|
||||
float xp = xoff + (x*xUnit);
|
||||
float yp = yoff + (y*yUnit);
|
||||
for(float i=0; i<=squareHeight-gap*2; i=i+ydiv) {
|
||||
float yy = i*squareHeight/2+gap+i;
|
||||
line(xp-squareWidth/2+gap, yp-squareHeight/2+gap+i, xp+squareWidth/2-gap, yp-squareHeight/2+gap+i);
|
||||
}
|
||||
|
||||
ydiv = random(1.0, squareHeight/4);
|
||||
|
||||
for(float i=0; i<=squareHeight-gap*2; i=i+ydiv) {
|
||||
float yy = i*squareHeight/2+gap+i;
|
||||
line(xp-squareWidth/2+gap, yp-squareHeight/2+gap+i, xp+squareWidth/2-gap, yp-squareHeight/2+gap+i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// save("Synthesis-3--3-" + int(random(0, 100)) + ".tif");
|
||||
@@ -1,47 +0,0 @@
|
||||
/**
|
||||
* Synthesis 1: Form and Code
|
||||
* Mandelbrot Set by William Ngan (http:/www.metaphorical.net)
|
||||
* p. 153
|
||||
*/
|
||||
|
||||
|
||||
size(600, 600);
|
||||
|
||||
float co; // color
|
||||
int accuracy = 200; // Lower numbers are less accurate (lower resolution)
|
||||
float zoom = 200.0; // Try these: 200 250 350 500 700 950 1250 1600 2000 2450 2950 3500
|
||||
float dx = width/2 - mouseX;
|
||||
float dy = height/2 - mouseY;
|
||||
float xscale = width / zoom;
|
||||
float yscale = height / zoom;
|
||||
float xOff = dx/width * xscale / 2;
|
||||
float yOff = -1 + (dy/height * yscale / 2);
|
||||
|
||||
for ( int i=0; i<width; i++ ) {
|
||||
for (int j=0; j<height; j++) {
|
||||
// For every point on complex plane
|
||||
float re = i/zoom - xOff - width/2.0/zoom;
|
||||
float im = j/zoom - yOff - height/2.0/zoom;
|
||||
// Complex number z, c
|
||||
float z1 = 0.0;
|
||||
float z2 = 0.0;
|
||||
co = 0;
|
||||
|
||||
for (int k=0; k<accuracy; k++) {
|
||||
float zz1 = sq(z1) - sq(z2) + re;
|
||||
float zz2 = 2*z1*z2 + im;
|
||||
z1 = zz1;
|
||||
z2 = zz2;
|
||||
// Check if the modulus of complex num is within limit
|
||||
if ( sqrt((sq(z1) + sq(z2)) ) > 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");
|
||||
@@ -1,103 +0,0 @@
|
||||
/**
|
||||
* 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<height; i=i+netSegment) {
|
||||
line(width/2, i, width/2, i+netSegment/3);
|
||||
}
|
||||
noStroke();
|
||||
|
||||
|
||||
ballX += ballDir * 2;
|
||||
ballY += ballAngle;
|
||||
|
||||
if(ballX > 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");
|
||||
}
|
||||
@@ -1,304 +0,0 @@
|
||||
/**
|
||||
* 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<nummodes; i++) {
|
||||
x = bgx + 15+100*i;
|
||||
y = bgy + 55;
|
||||
noFill();
|
||||
stroke(1,1,1);//.6, .6, .6);
|
||||
if (i+1 != mode) {
|
||||
if (isInside(mx, my, x-4, y-32, w, h) ||
|
||||
isInside(mx, my, x-4 - gx, y-32, w, h) ||
|
||||
isInside(mx, my, x-4, y-32 - gy, w, h) ||
|
||||
isInside(mx, my, x-4 - gx, y-32 - gy, w, h)) {
|
||||
fill(.7, .1, 0);
|
||||
if (mousePressed) {
|
||||
mode = i+1;
|
||||
println("chose "+mode);
|
||||
mousePressed = false;
|
||||
}
|
||||
}
|
||||
rect(x-4, y-32, w, h);
|
||||
rect(x-4 - gx, y-32, w, h);
|
||||
rect(x-4, y-32 - gy, w, h);
|
||||
rect(x-4 - gx, y-32 - gy, w, h);
|
||||
}
|
||||
fill(.6, .6, .6);
|
||||
fill(1,1,1);
|
||||
y += 34;
|
||||
x += 2;
|
||||
text(""+(i+1), x, y);
|
||||
text(""+(i+1), x - gx, y);
|
||||
text(""+(i+1), x, y - gy);
|
||||
text(""+(i+1), x - gx, y - gy);
|
||||
}
|
||||
|
||||
if (mode == 1) {
|
||||
fill(1,1,1);
|
||||
stroke(.2,.2,.2);
|
||||
|
||||
drawCursor(mx, my, 1, 0);
|
||||
|
||||
} else if (mode == 2) {
|
||||
// scaling/rotating cursor
|
||||
fill(1,1,1);
|
||||
stroke(.2,.2,.2);
|
||||
|
||||
float rot = atan2(my-lastmy, mx-lastmx);
|
||||
float sc = 1 + .06*abs(lastmx-mx) + .06*abs(lastmy-my);
|
||||
drawCursor(mx, my, sc, rot*.5 + lastrot*.5);
|
||||
lastrot = rot*.5 + lastrot*.5;
|
||||
lastsc = sc;
|
||||
|
||||
} else if (mode == 3) {
|
||||
// slow poke / blur
|
||||
fill(1,1,1, .07);
|
||||
|
||||
float sc = 1 - .04*abs(lastmx-mx) - .03*abs(lastmy-my);
|
||||
if (sc < 0.01) sc = .01;
|
||||
|
||||
noStroke();
|
||||
|
||||
drawBlurCursor(mx, my, sc*.15 + lastsc*.85, sc);
|
||||
|
||||
noFill();
|
||||
stroke(.2,.2,.2, .35);
|
||||
drawCursor(mx, my, sc*.15 + lastsc*.85, 0);
|
||||
|
||||
lastsc = sc;
|
||||
|
||||
} else if (mode == 4) {
|
||||
// grid
|
||||
fill(1,1,1);
|
||||
stroke(.2,.2,.2);
|
||||
|
||||
float rot;
|
||||
for (int i=16; i<gx+2; i+=62) {
|
||||
for (int j=11; j<gy; j+=60) {
|
||||
rot = -PI/4. + atan2(j-my, i-mx);
|
||||
drawCursor(i, j, 1, rot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (forapplet) {
|
||||
fill(0, 0, 0);
|
||||
rect(0, gy/2, gx, gy/2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
/**
|
||||
* Synthesis 2: Input and Response
|
||||
* Typing by Casey Reas (www.processing.org)
|
||||
* p. 258
|
||||
*
|
||||
* Click and drag the mouse to set the size and
|
||||
* and angle of the current line of text. Type to change the
|
||||
* text. Hit "delete" or "backspace" to remove letters from the
|
||||
* end of the line. Hit "return" to switch to the next line of text.
|
||||
*/
|
||||
|
||||
|
||||
String text1 = "Flatland by Edwin A. Abbott";
|
||||
String text2 = "1884";
|
||||
String text3 = "I call our world Flatland, not because we call it so,";
|
||||
String text4 = "but to make its nature clearer to you, my happy readers,";
|
||||
String text5 = "who are privileged to live in Space.";
|
||||
String s = "";
|
||||
int x, y;
|
||||
int mx, my;
|
||||
|
||||
int mode = 1;
|
||||
|
||||
float s1 = 1.0;
|
||||
float r1 = 0;
|
||||
float x1 = 0;
|
||||
float y1 = 0;
|
||||
|
||||
float s2 = 1.0;
|
||||
float r2 = 0;
|
||||
float x2 = 0;
|
||||
float y2 = 0;
|
||||
|
||||
float s3 = 1.0;
|
||||
float r3 = 0;
|
||||
float x3 = 0;
|
||||
float y3 = 0;
|
||||
|
||||
float s4 = 1.0;
|
||||
float r4 = 0;
|
||||
float x4 = 0;
|
||||
float y4 = 0;
|
||||
|
||||
float s5 = 1.0;
|
||||
float r5 = 0;
|
||||
float x5 = 0;
|
||||
float y5 = 0;
|
||||
|
||||
|
||||
PFont font;
|
||||
int whichString = 0;
|
||||
|
||||
void setup() {
|
||||
size(600, 300);
|
||||
smooth();
|
||||
|
||||
//String[] fontList = PFont.list();
|
||||
//println(fontList);
|
||||
|
||||
font = createFont("Serif", 128);
|
||||
textFont(font, 18);
|
||||
stroke(0);
|
||||
fill(0, 200);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
|
||||
stroke(204);
|
||||
line(mouseX, 0, mouseX, height);
|
||||
line(0, mouseY, width, mouseY);
|
||||
stroke(0);
|
||||
line(mx, 0, mx, height);
|
||||
line(0, my, width, my);
|
||||
|
||||
pushMatrix();
|
||||
translate(x1, y1);
|
||||
scale(s1);
|
||||
rotate(r1);
|
||||
text(text1, 0, 0);
|
||||
popMatrix();
|
||||
|
||||
pushMatrix();
|
||||
translate(x2, y2);
|
||||
scale(s2);
|
||||
rotate(r2);
|
||||
text(text2, 0, 0);
|
||||
popMatrix();
|
||||
|
||||
pushMatrix();
|
||||
translate(x3, y3);
|
||||
scale(s3);
|
||||
rotate(r3);
|
||||
text(text3, 0, 0);
|
||||
popMatrix();
|
||||
|
||||
pushMatrix();
|
||||
translate(x4, y4);
|
||||
scale(s4);
|
||||
rotate(r4);
|
||||
text(text4, 0, 0);
|
||||
popMatrix();
|
||||
|
||||
pushMatrix();
|
||||
translate(x5, y5);
|
||||
scale(s5);
|
||||
rotate(r5);
|
||||
text(text5, 0, 0);
|
||||
popMatrix();
|
||||
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if(key == ' ') {
|
||||
//saveFrame("type-####.tif");
|
||||
}
|
||||
if(key == ENTER || key == RETURN) {
|
||||
mode++;
|
||||
if(mode > 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;
|
||||
}
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
/**
|
||||
* 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<NCOLORS; i++){
|
||||
colorArray[i] = color(i,i,i);
|
||||
}
|
||||
|
||||
ys0 = 0;
|
||||
ys1 = canvasHeight/3;
|
||||
ym0 = ys1+1;
|
||||
ym1 = canvasHeight*2/3;
|
||||
yh0 = ym1+1;
|
||||
yh1 = canvasHeight;
|
||||
}
|
||||
|
||||
|
||||
void draw(){
|
||||
updateClock();
|
||||
drawClock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
void updateClock(){
|
||||
|
||||
findMillisError();
|
||||
|
||||
mil = (millis()-milError)%1000;
|
||||
sec = second();
|
||||
minut = minute();
|
||||
hou = hour()%12;
|
||||
|
||||
S = (float)(sec *1000.0 + mil)/1000.0;
|
||||
M = (minut*60.0 + S)/60.0;
|
||||
H = (hou *60.0 + M)/60.0;
|
||||
|
||||
Soffset += Svel;
|
||||
Svel *= damp;
|
||||
Moffset += Mvel;
|
||||
Mvel *= damp;
|
||||
Hoffset += Hvel;
|
||||
Hvel *= damp;
|
||||
|
||||
float p;
|
||||
float ps, pm, ph;
|
||||
for (int i=0; i<canvasWidth; i++){
|
||||
p = (float)i/(float)canvasWidth;
|
||||
ps = p*S + Soffset;
|
||||
pm = p*M + Moffset;
|
||||
ph = p*H + Hoffset;
|
||||
Scolors[i] = wave(GMOD(ps, 1.0f));
|
||||
Mcolors[i] = wave(GMOD(pm, 1.0f));
|
||||
Hcolors[i] = wave(GMOD(ph, 1.0f));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
void drawClock(){
|
||||
for (int x=0; x<canvasWidth; x++){
|
||||
stroke(colorArray[Scolors[x]]);
|
||||
line (x, ys0, x, ys1);
|
||||
stroke(colorArray[Mcolors[x]]);
|
||||
line (x, ym0, x, ym1);
|
||||
stroke(colorArray[Hcolors[x]]);
|
||||
line (x, yh0, x, yh1);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Utilities
|
||||
|
||||
void findMillisError(){
|
||||
int sec2 = second();
|
||||
if (sec2 > 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");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
/**
|
||||
* 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<n_nodes; i++) {
|
||||
float r = sqrt(r1 * r1 + 2.0 * dr * D);
|
||||
float d = (r - r1) / dr;
|
||||
|
||||
nodes_x[i] = x - sin(d) * r;
|
||||
nodes_y[i] = y + cos(d) * r;
|
||||
|
||||
D += node_length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(204);
|
||||
|
||||
// Set the position of the head
|
||||
setTarget(mouseX, mouseY);
|
||||
|
||||
// Draw the head
|
||||
fill(col_head);
|
||||
ellipse(nodes_x[0], nodes_y[0], node_size, node_size);
|
||||
|
||||
// Draw the body
|
||||
fill(col_body);
|
||||
for (int i=1; i < n_nodes; i++) {
|
||||
ellipse(nodes_x[i], nodes_y[i], node_size, node_size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setTarget(float tx, float ty)
|
||||
{
|
||||
// Motion interpolation for the head
|
||||
x += (tx - x) / delay;
|
||||
y += (ty - y) / delay;
|
||||
nodes_x[0] = x;
|
||||
nodes_y[0] = y;
|
||||
|
||||
// Constrained motion for the other nodes
|
||||
for (int i=1; i < n_nodes; i++)
|
||||
{
|
||||
float dx = nodes_x[i - 1] - nodes_x[i];
|
||||
float dy = nodes_y[i - 1] - nodes_y[i];
|
||||
float len = sqrt(sq(dx) + sq(dy));
|
||||
nodes_x[i] = nodes_x[i - 1] - (dx/len * node_length);
|
||||
nodes_y[i] = nodes_y[i - 1] - (dy/len * node_length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void keyPressed() {
|
||||
// saveFrame("centipede-####.tif");
|
||||
}
|
||||
|
||||
@@ -1,236 +0,0 @@
|
||||
/**
|
||||
* Synthesis 3: Motion and Arrays
|
||||
* Chronodraw by Andreas Gysin (www.ertdfgcvb.ch)
|
||||
* p. 373
|
||||
*/
|
||||
|
||||
|
||||
int hquads = 23;
|
||||
int vquads = 1;
|
||||
int quadwidth = 38;
|
||||
int quadheight = 250;
|
||||
int buflength = 200;
|
||||
int framestep = 4;
|
||||
int framerest = 2;
|
||||
boolean fat = false;
|
||||
boolean grid = true;
|
||||
int btnresomulti = 2;
|
||||
int btnreso = 23;
|
||||
int btnheight;
|
||||
int framecounter = 0;
|
||||
PImage[] bitlist = new PImage[buflength];
|
||||
PImage blank = new PImage();
|
||||
int currentpic = buflength - 1;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(quadwidth * hquads, quadheight * vquads);
|
||||
blank.width = quadwidth;
|
||||
blank.height = quadheight;
|
||||
blank.pixels = new color[quadwidth*quadheight];
|
||||
for (int j=0; j<blank.pixels.length; j++){
|
||||
blank.pixels[j] = color(0,0,0);
|
||||
}
|
||||
clearall();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// drawpixels
|
||||
if (mousePressed) {
|
||||
if ((mouseX >= 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<vquads; j++){
|
||||
for (int i=0; i<hquads; i++){
|
||||
set(i*quadwidth, j*quadheight, bitlist[p]);
|
||||
p+= framestep;
|
||||
p = p%buflength;
|
||||
if (p<0) {
|
||||
p = buflength + p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// increment
|
||||
framecounter++;
|
||||
if (framecounter >= 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<bitlist.length; i++){
|
||||
bitlist[i] = blank.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void keyPressed(){
|
||||
if (key == 'x' || key == 'X') {
|
||||
clearall();
|
||||
}
|
||||
else if (key == 'f' || key == 'F') {
|
||||
fat = !fat;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void drawgrid() {
|
||||
stroke(255, 255, 255, 40);
|
||||
|
||||
for (int i=0; i<vquads; i++){
|
||||
int l = i*quadheight;
|
||||
line(0, l, width, l);
|
||||
}
|
||||
line(0, height-1, width, height-1); // last line
|
||||
|
||||
for (int i=0; i<hquads; i++){
|
||||
int l = i*quadwidth;
|
||||
line(l, 0, l, height);
|
||||
}
|
||||
line(width-1, 0, width-1, height); // last line
|
||||
}
|
||||
|
||||
|
||||
void drawdot(int qx, int qy){
|
||||
int offsx = qx%quadwidth;
|
||||
int offsy = qy%quadheight;
|
||||
int mx = qx / quadwidth;
|
||||
int my = qy / quadheight;
|
||||
int offsp = mx + my * hquads;
|
||||
int cp = (currentpic + offsp * framestep) % buflength;
|
||||
if (cp < 0) cp = buflength + cp;
|
||||
bitlist[cp].set(offsx, offsy, color(255,255,255));
|
||||
}
|
||||
|
||||
|
||||
//Bresenham's algorithm
|
||||
void drawline(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
int sizex, sizey, incx, incy;
|
||||
int countx, county, x, y;
|
||||
sizex=x2-x1;
|
||||
sizey=y2-y1;
|
||||
|
||||
if(sizex<0) {
|
||||
sizex=-sizex;
|
||||
incx=-1;
|
||||
} else {
|
||||
incx=1;
|
||||
}
|
||||
|
||||
if(sizey<0) {
|
||||
sizey=-sizey;
|
||||
incy=-1;
|
||||
}else {
|
||||
incy = 1;
|
||||
}
|
||||
countx=x1;
|
||||
county=y1;
|
||||
|
||||
drawdot(x1, y1);
|
||||
if (sizex>=sizey) {
|
||||
y=sizex>>1;
|
||||
for(int i=0;i<sizex;i++) {
|
||||
y+=sizey;
|
||||
if (y>=sizex) {
|
||||
y-=sizex;
|
||||
county+=incy;
|
||||
}
|
||||
countx+=incx;
|
||||
drawdot(countx, county);
|
||||
}
|
||||
} else {
|
||||
x=sizey>>1;
|
||||
for(int i=0;i<sizey;i++) {
|
||||
x+=sizex;
|
||||
if (x>=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]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
/**
|
||||
* Synthesis 3: Motion and Arrays
|
||||
* AmoebaAbstract_03 by Marius Watz <http://www.unlekker.net>
|
||||
* 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; i<num; i++) {
|
||||
x[i]=0;
|
||||
y[i]=h*i+2;
|
||||
speed[i]=random(maxSpeed/2-1)+1;
|
||||
if(random(100)>50) 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<num; i++) {
|
||||
x[i] += speed[i];
|
||||
if(x[i] < 0) {
|
||||
x[i] += width;
|
||||
} else if(x[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; i<num; i++) {
|
||||
colR[colNum] = r1+r2*(float)i;
|
||||
colG[colNum] = g1+g2*(float)i;
|
||||
colB[colNum] = b1+b2*(float)i;
|
||||
colNum++;
|
||||
}
|
||||
}
|
||||
|
||||
void updateSpeed() {
|
||||
int which = mouseY/(height/num);
|
||||
speed[which] = sq(((mouseX-width/2.0)/(width/2.0)))*maxSpeed;
|
||||
if(speed[which] < 0.2) {
|
||||
speed[which] = 0.2;
|
||||
}
|
||||
if(mouseX < width/2) {
|
||||
speed[which] *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
void mouseDragged() {
|
||||
updateSpeed();
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
background(255);
|
||||
initColors();
|
||||
updateSpeed();
|
||||
}
|
||||
|
||||
void mouseMoved() {
|
||||
updateSpeed();
|
||||
}
|
||||
|
||||
|
||||
void keyPressed() {
|
||||
// saveFrame("amoebaAbstract-####.tif");
|
||||
}
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* Synthesis 3: Motion and Arrays
|
||||
* Mr. Roboto by Leon Hong
|
||||
* p. 375
|
||||
*
|
||||
* The program loads a series images. They are assembled with the code
|
||||
* to create the robot and its motion. The position of the robot is controlled
|
||||
* with the mouse. Move the robot over the battery to turn on its lights.
|
||||
*/
|
||||
|
||||
|
||||
PImage batt, battalpha, batteat, batteatalpha, body, body2, head, head2, headalpha;
|
||||
float mx = -100;
|
||||
float my = -10;
|
||||
float delay = 100.0;
|
||||
float sinval;
|
||||
float angle;
|
||||
float speed = -.9;
|
||||
|
||||
void setup() {
|
||||
size(600, 600);
|
||||
batt = loadImage("batt.png");
|
||||
battalpha = loadImage("battalpha.gif");
|
||||
batt.mask(battalpha);
|
||||
batteat = loadImage("batteat.gif");
|
||||
batteatalpha = loadImage("batteatalpha.gif");
|
||||
batteat.mask(batteatalpha);
|
||||
body = loadImage("body.png");
|
||||
body2 = loadImage("body2.jpg");
|
||||
head = loadImage("head.png");
|
||||
head2 = loadImage("head2.jpg");
|
||||
headalpha = loadImage("headalpha.gif");
|
||||
head.mask(headalpha);
|
||||
head2.mask(headalpha);
|
||||
frameRate(30);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
noCursor();
|
||||
background (172);
|
||||
pushMatrix();
|
||||
interpolate();
|
||||
translate (mx-100,my-150);
|
||||
angle = angle + speed;
|
||||
sinval = sin(angle);
|
||||
if (abs(my-mouseY)>20 && 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");
|
||||
}
|
||||
|
Before Width: | Height: | Size: 516 B |
|
Before Width: | Height: | Size: 163 B |
|
Before Width: | Height: | Size: 754 B |
|
Before Width: | Height: | Size: 1.6 KiB |
@@ -1,18 +0,0 @@
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/**
|
||||
* 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<num; i++) {
|
||||
int qq;
|
||||
if(random(1) > 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<num; i++){
|
||||
modsA[i].updateMe();
|
||||
for (int j=0; j<num; j++){
|
||||
modsB[j].myAngle = modsA[j].myAngle;
|
||||
modsB[j].myRadius = modsA[j].myRadius+i;
|
||||
}
|
||||
modsB[i].updateMe();
|
||||
if ( (modsA[i].x < 0) || (modsA[i].x > 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");
|
||||
}
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 63 B |
|
Before Width: | Height: | Size: 63 B |
|
Before Width: | Height: | Size: 63 B |
|
Before Width: | Height: | Size: 62 B |
|
Before Width: | Height: | Size: 62 B |
|
Before Width: | Height: | Size: 62 B |
@@ -1,671 +0,0 @@
|
||||
/**
|
||||
* 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; i<NUM; i++) {
|
||||
flock[i] = new Fish( random(width), random(height), 2f, random(PI), random(8f,12f) );
|
||||
flock[i].setSpeedLimit( random(1f,3f), 0.5f );
|
||||
flock[i].setColor( random(13), random(30,70), 100 );
|
||||
}
|
||||
|
||||
// Ripple
|
||||
for (int i=0; i<ripple.length; i++) {
|
||||
ripple[i] = 999;
|
||||
}
|
||||
|
||||
// Big fish 1
|
||||
bigfish1 = new Fish( random(width), random(height), 1f, random(PI), 18f );
|
||||
bigfish1.setSpeedLimit( 2f, 1f );
|
||||
bigfish1.setColor( 84,45,100 );
|
||||
|
||||
// Big fish 2
|
||||
bigfish2 = new Fish( random(width), random(height), 1f, random(PI), 18f );
|
||||
bigfish2.setSpeedLimit( 1f, 0.5f );
|
||||
bigfish2.setColor( 90,60,70 );
|
||||
|
||||
|
||||
// Big fish 3
|
||||
bigfish3 = new Fish( random(width), random(height), 1f, random(PI), 22f );
|
||||
bigfish3.setSpeedLimit( 1f, 0.5f );
|
||||
bigfish3.setColor( 100,45,50 );
|
||||
|
||||
smooth();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void draw() {
|
||||
|
||||
background( 85,46,83);
|
||||
stroke(84,45,100);
|
||||
noFill();
|
||||
|
||||
// Draw bigfish
|
||||
if (hasPredator>0) {
|
||||
|
||||
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; i<flock.length; i++) {
|
||||
|
||||
fill(flock[i].colour[0], flock[i].colour[1]+flock[i].tone, flock[i].colour[2]);
|
||||
|
||||
if (hasRipple) {
|
||||
flock[i].swarm( rippleX, rippleY, PI/20 );
|
||||
}
|
||||
|
||||
flock[i].scanFlock( flock, 200, 50 );
|
||||
|
||||
if (hasPredator>0) {
|
||||
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; k<ripple.length; k++) {
|
||||
if (ripple[k]<width) {
|
||||
ripple[k]+=3f*(k+4);
|
||||
ellipse( rippleX, rippleY, ripple[k], ripple[k]);
|
||||
countRipple = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hasRipple = (countRipple>60) ? false : true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void mouseDragged() {
|
||||
rippleX = mouseX;
|
||||
rippleY = mouseY;
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
rippleX = mouseX;
|
||||
rippleY = mouseY;
|
||||
}
|
||||
|
||||
void mouseReleased() {
|
||||
if (!hasRipple) {
|
||||
for (int k=0; k<ripple.length; k++) {
|
||||
ripple[k]=0;
|
||||
}
|
||||
hasRipple = true;
|
||||
countRipple = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if(key == '1') {
|
||||
hasPredator = 1;
|
||||
}
|
||||
else if (key == '2') {
|
||||
hasPredator = 2;
|
||||
}
|
||||
else if (key == '3') {
|
||||
hasPredator = 3;
|
||||
}
|
||||
else if (key == '0') {
|
||||
hasPredator = 0;
|
||||
}
|
||||
|
||||
// saveFrame("pond-####.tif");
|
||||
}
|
||||
|
||||
|
||||
|
||||
// FISH CLASS
|
||||
|
||||
class Fish {
|
||||
|
||||
float fsize;
|
||||
float[] tailP = { 0,0 };
|
||||
float[] tailPC = { 0,0 };
|
||||
float tailLength = 3.0f;
|
||||
float x, y, angle, speed;
|
||||
float maxSpeed, minSpeed;
|
||||
|
||||
float energy = 1f; // Energy to wriggle
|
||||
float wave = 0; // Tail wave
|
||||
int wcount = 0;
|
||||
int uturn = 0;
|
||||
int boundTime = 0;
|
||||
|
||||
float[] colour = { 255,255,255 };
|
||||
float tone = 0;
|
||||
boolean isBound = false;
|
||||
|
||||
Fish( float px, float py, float s, float a, float size ) {
|
||||
tailP[1] = tailLength;
|
||||
tailPC[1] = tailLength;
|
||||
|
||||
x = px;
|
||||
y = py;
|
||||
angle = a;
|
||||
speed = s;
|
||||
fsize = size;
|
||||
}
|
||||
|
||||
|
||||
// Draw fish's curves
|
||||
void getFish(){
|
||||
float[] pos1, pos2, pos3;
|
||||
beginShape();
|
||||
|
||||
pos1 = calc( 0f, -1f, fsize );
|
||||
vertex( pos1[0], pos1[1]);
|
||||
|
||||
pos1 = calc( 0.5f, -1f, fsize );
|
||||
pos2 = calc( 1f, -0.5f, fsize );
|
||||
pos3 = calc( 1f, 0f, fsize );
|
||||
bezierVertex(pos1[0], pos1[1], pos2[0], pos2[1], pos3[0], pos3[1]);
|
||||
|
||||
pos1 = calc( 1f, 1f, fsize );
|
||||
pos2 = calc( tailPC[0], tailPC[1], fsize );
|
||||
pos3 = calc( tailP[0], tailP[1], fsize );
|
||||
bezierVertex(pos1[0], pos1[1], pos2[0], pos2[1], pos3[0], pos3[1]);
|
||||
|
||||
pos1 = calc( tailPC[0], tailPC[1], fsize );
|
||||
pos2 = calc( -1f, 1f, fsize );
|
||||
pos3 = calc( -1f, 0f, fsize );
|
||||
bezierVertex(pos1[0], pos1[1], pos2[0], pos2[1], pos3[0], pos3[1]);
|
||||
|
||||
pos1 = calc( -1f, -0.5f, fsize );
|
||||
pos2 = calc( -0.5f, -1f, fsize );
|
||||
pos3 = calc( 0f, -1f, fsize );
|
||||
bezierVertex(pos1[0], pos1[1], pos2[0], pos2[1], pos3[0], pos3[1]);
|
||||
|
||||
endShape();
|
||||
}
|
||||
|
||||
|
||||
// Set tail's position
|
||||
void setTail( float strength, float wave ) {
|
||||
tailP[0] = strength*wave;
|
||||
tailP[1] = tailLength+tailLength/2 - abs( tailLength/4*strength*wave );
|
||||
tailPC[0] = strength*wave*-1;
|
||||
}
|
||||
|
||||
// Translate a bezier ctrl point according to fish's angle and pos.
|
||||
float[] calc( float px, float py, float s ) {
|
||||
float a = atan2( py, px) + angle+ PI/2;
|
||||
float r = sqrt( (px*px + py*py) );
|
||||
float[] pos ={
|
||||
x+r*s*cos(a), y+r*s*sin(a) };
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Wriggle
|
||||
protected void wriggle() {
|
||||
|
||||
// Calc energy
|
||||
if (energy > 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 (speed<minSpeed) { speed=minSpeed; }
|
||||
if (speed>maxSpeed) { 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 && temp<PI/2 ) {
|
||||
uturn = 1;
|
||||
}
|
||||
else if ( temp >PI/2 && temp<PI ) {
|
||||
uturn = -1;
|
||||
}
|
||||
else if ( temp>PI && temp<PI*3/2 ) {
|
||||
uturn = 1;
|
||||
}
|
||||
else if ( temp>PI*3/2 && temp<PI*2 ) {
|
||||
uturn = -1;
|
||||
}
|
||||
else {
|
||||
uturn = 1;
|
||||
}
|
||||
|
||||
if (y-gap < 0 || y+gap > 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; i<flocks.length; i++) {
|
||||
nx += flocks[i].x;
|
||||
ny += flocks[i].y;
|
||||
}
|
||||
|
||||
nx /= flocks.length;
|
||||
ny /= flocks.length;
|
||||
|
||||
turnTo( angle(nx, ny), angleSpeed+random(angleSpeed*2) ); //0.001
|
||||
speedChange( moveSpeed ); //-0.1
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Seperation -- moves away from the flock when it's too crowded
|
||||
void seperate( Fish[] flocks, float angleSpeed, float moveSpeed ) {
|
||||
|
||||
// find normalised away angle
|
||||
float nA = 0;
|
||||
|
||||
for (int i=0; i<flocks.length; i++) {
|
||||
nA += (flocks[i].angle+PI);
|
||||
}
|
||||
|
||||
nA /= flocks.length;
|
||||
turnTo( nA, angleSpeed+random(angleSpeed*2) ); //0.001
|
||||
speedChange( moveSpeed ); //0.05
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Collision aviodance -- moves away quickly when it's too close
|
||||
void avoid( Fish[] flocks, float angleSpeed, float moveSpeed ) {
|
||||
|
||||
for (int i=0; i<flocks.length; i++) {
|
||||
float dA = angle( flocks[i] ) + PI;
|
||||
|
||||
x = x + cos(dA)*moveSpeed/2;
|
||||
y = y + sin(dA)*moveSpeed/2;
|
||||
|
||||
turnTo( dA, angleSpeed+random(angleSpeed) ); //0.005
|
||||
}
|
||||
speedChange( moveSpeed ); //0.1
|
||||
}
|
||||
|
||||
// Flee from predator
|
||||
void predator( float px, float py, float alertDistance, float angleSpeed, float moveSpeed ) {
|
||||
|
||||
float d = dist( px, py );
|
||||
if ( d < alertDistance) {
|
||||
float dA = angle(px, py) + PI;
|
||||
x = x + cos(dA)*moveSpeed; //0.01
|
||||
y = y + sin(dA)*moveSpeed;
|
||||
turnTo( dA, angleSpeed+ random(angleSpeed) );
|
||||
if (tone <50) tone+=5;
|
||||
}
|
||||
else {
|
||||
if (tone>0) 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<flocks.length; i++) {
|
||||
|
||||
Fish fp = flocks[i];
|
||||
|
||||
// check nearby fishes
|
||||
if (fp != this) {
|
||||
float d = dist( fp );
|
||||
if (d < cohereR ) {
|
||||
near[nCount++] = fp;
|
||||
if (dist > 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<nCount; j++) {
|
||||
near2[j] = near[j];
|
||||
}
|
||||
for (j=0; j<tnCount; j++) {
|
||||
tooNear2[j] = tooNear[j];
|
||||
}
|
||||
for (j=0; j<cCount; j++) {
|
||||
collide2[j] = collide[j];
|
||||
}
|
||||
|
||||
if (!inbound && !hasRipple) {
|
||||
if (nearest!=null) {
|
||||
align( nearest, 0.1f*PI/180, 0.2f );
|
||||
}
|
||||
cohere( near2, 0.1f*PI/180, -0.1f );
|
||||
}
|
||||
seperate( tooNear2, (random(0.1f)+0.1f)*PI/180, 0.05f );
|
||||
avoid( collide2, (random(0.2f)+0.2f)*PI/180, 0.1f );
|
||||
}
|
||||
|
||||
float diffA = (angle - tempA)*5;
|
||||
float c = diffA*180/(float)Math.PI;
|
||||
|
||||
// Wriggle tail
|
||||
energy += abs( c/100 );
|
||||
wriggle();
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Scan for food
|
||||
void scanPrey( Fish[] flocks, float range ) {
|
||||
|
||||
|
||||
Fish nearest = null;
|
||||
float dist = 99999;
|
||||
|
||||
float tempA = angle;
|
||||
|
||||
// Look for nearby food
|
||||
for (int i=0; i<flocks.length; i++) {
|
||||
float d = dist( flocks[i] );
|
||||
if (dist > 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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
|
||||
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<b.length;i++) {
|
||||
if(i!=id) {
|
||||
if(b[i].checkParent(this)) parent = b[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printChildren() {
|
||||
print("this->"+this.id+" *** child(s) ->");
|
||||
for(int i=0;i<children.length;i++) {
|
||||
if(children[i]!=null) {
|
||||
print(" ,"+children[i].id);
|
||||
}
|
||||
}
|
||||
if(parent!=null) {
|
||||
print(" *** parent->"+parent.id);
|
||||
} else {
|
||||
print(" *** ROOT");
|
||||
}
|
||||
println("");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/**
|
||||
* 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<s.length; i++) {
|
||||
s[i].setFutureToOrigin();
|
||||
s[i].setLength(0);
|
||||
s[i].scaleFutureLength(1.4);
|
||||
}
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
background(255);
|
||||
s[rootId].render();
|
||||
pX += (mouseX-pX)/10;
|
||||
pY += (mouseY-pY)/10;
|
||||
|
||||
float tmpSpeed = 8*(pX-width/2)/width;
|
||||
s[rootId].setAngle(90 + sin(millis()*0.0006)*10*tmpSpeed);
|
||||
s[redNum].setAngle(sin(millis()*0.0001)*10*tmpSpeed);
|
||||
s[240].setAngle(sin(millis()*0.0001)*(30*tmpSpeed));
|
||||
s[148].setAngle(sin(millis()*0.0001)*(20*tmpSpeed));
|
||||
s[113].setAngle(sin(millis()*0.0004)*(20*tmpSpeed));
|
||||
|
||||
float tmpHeight = (pY-height/2)/height;
|
||||
for(int i=0;i<s.length;i++) {
|
||||
s[i].scaleLengthFromOrigin(1-tmpHeight);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
redNum++;
|
||||
println(redNum%391);
|
||||
|
||||
// saveFrame("tree-####.tif");
|
||||
}
|
||||
|
||||
void parseTree() {
|
||||
String[] lines;
|
||||
String[] params;
|
||||
lines = loadStrings("treeData.txt");
|
||||
println("There are " + lines.length + " lines.");
|
||||
b = new Branch[lines.length];
|
||||
s = new Segment[lines.length];
|
||||
|
||||
for (int i=0; i < lines.length; i++) {
|
||||
params = split(lines[i],'|');
|
||||
b[i] = new Branch(params[0], params[1], params[2]);
|
||||
}
|
||||
|
||||
for(int i=0; i<b.length; i++) {
|
||||
b[i].checkForParents();
|
||||
}
|
||||
for(int i=0; i<b.length; i++) {
|
||||
s[i] = new Segment(i);
|
||||
}
|
||||
for(int i=0; i<b.length; i++) {
|
||||
s[i].setBranch(b[i]);
|
||||
}
|
||||
|
||||
// get the ID of the root Segment
|
||||
rootId = -1;
|
||||
for(int i=0; i<b.length; i++) {
|
||||
if(s[i].parent == null) {
|
||||
rootId = i;
|
||||
}
|
||||
}
|
||||
println("ROOT -> " + rootId);
|
||||
|
||||
for(int i=0; i<b.length; i++) {
|
||||
s[i].setParamsFromBranch();
|
||||
}
|
||||
|
||||
s[rootId].adjustAngle(0);
|
||||
s[rootId].adjustDepth(1);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,391 +0,0 @@
|
||||
0|7 75 6|7 80 6
|
||||
1|8 89 -1|8 94 -1
|
||||
2|17 105 -2|17 110 -2
|
||||
3|18 127 0|18 132 0
|
||||
4|15 150 -8|15 155 -8
|
||||
5|5 57 9|7 75 6
|
||||
6|7 75 6|8 89 -1
|
||||
7|8 89 -1|17 105 -2
|
||||
8|17 105 -2|18 127 0
|
||||
9|18 127 0|15 150 -8
|
||||
10|15 150 -8|21 171 -23
|
||||
11|13 90 9|13 95 9
|
||||
12|12 105 10|12 110 10
|
||||
13|12 123 6|12 128 6
|
||||
14|18 143 8|18 148 8
|
||||
15|16 166 16|16 171 16
|
||||
16|13 71 9|13 90 9
|
||||
17|13 90 9|12 105 10
|
||||
18|12 105 10|12 123 6
|
||||
19|12 123 6|18 143 8
|
||||
20|18 143 8|16 166 16
|
||||
21|16 166 16|8 190 15
|
||||
22|18 107 16|18 112 16
|
||||
23|23 120 23|23 125 23
|
||||
24|22 139 25|22 144 25
|
||||
25|28 159 21|28 164 21
|
||||
26|14 89 14|18 107 16
|
||||
27|18 107 16|23 120 23
|
||||
28|23 120 23|22 139 25
|
||||
29|22 139 25|28 159 21
|
||||
30|28 159 21|44 178 25
|
||||
31|16 127 9|16 132 9
|
||||
32|26 140 6|26 145 6
|
||||
33|32 157 9|32 162 9
|
||||
34|33 178 8|33 183 8
|
||||
35|11 110 10|16 127 9
|
||||
36|16 127 9|26 140 6
|
||||
37|26 140 6|32 157 9
|
||||
38|32 157 9|33 178 8
|
||||
39|33 178 8|41 199 -3
|
||||
40|19 150 0|19 155 0
|
||||
41|19 164 -7|19 169 -7
|
||||
42|27 180 -10|27 185 -10
|
||||
43|30 201 -7|30 206 -7
|
||||
44|20 131 2|19 150 0
|
||||
45|19 150 0|19 164 -7
|
||||
46|19 164 -7|27 180 -10
|
||||
47|27 180 -10|30 201 -7
|
||||
48|30 201 -7|23 225 -10
|
||||
49|34 173 7|34 178 7
|
||||
50|33 188 11|33 193 11
|
||||
51|32 206 6|32 211 6
|
||||
52|33 154 6|34 173 7
|
||||
53|34 173 7|33 188 11
|
||||
54|33 188 11|32 206 6
|
||||
55|32 206 6|39 226 7
|
||||
56|38 201 13|38 206 13
|
||||
57|45 214 18|45 219 18
|
||||
58|46 232 22|46 237 22
|
||||
59|33 183 12|38 201 13
|
||||
60|38 201 13|45 214 18
|
||||
61|45 214 18|46 232 22
|
||||
62|46 232 22|50 252 18
|
||||
63|37 232 4|37 237 4
|
||||
64|45 244 -1|45 249 -1
|
||||
65|54 261 1|54 266 1
|
||||
66|33 214 6|37 232 4
|
||||
67|37 232 4|45 244 -1
|
||||
68|45 244 -1|54 261 1
|
||||
69|54 261 1|54 282 0
|
||||
70|44 266 0|44 271 0
|
||||
71|44 280 -4|44 285 -4
|
||||
72|45 247 1|44 266 0
|
||||
73|44 266 0|44 280 -4
|
||||
74|44 280 -4|48 297 -10
|
||||
75|0 28 9|5 57 9
|
||||
76|5 57 9|13 71 9
|
||||
77|13 71 9|14 89 14
|
||||
78|14 89 14|11 110 10
|
||||
79|11 110 10|20 131 2
|
||||
80|20 131 2|33 154 6
|
||||
81|33 154 6|33 183 12
|
||||
82|33 183 12|33 214 6
|
||||
83|33 214 6|45 247 1
|
||||
84|45 247 1|55 283 4
|
||||
85|-3 86 10|-3 91 10
|
||||
86|-12 100 9|-12 105 9
|
||||
87|-13 116 0|-13 121 0
|
||||
88|-13 138 0|-13 143 0
|
||||
89|0 68 12|-3 86 10
|
||||
90|-3 86 10|-12 100 9
|
||||
91|-12 100 9|-13 116 0
|
||||
92|-13 116 0|-13 138 0
|
||||
93|-13 138 0|-19 161 3
|
||||
94|0 101 5|0 106 5
|
||||
95|0 116 5|0 121 5
|
||||
96|-3 134 6|-3 139 6
|
||||
97|-2 154 0|-2 159 0
|
||||
98|0 82 5|0 101 5
|
||||
99|0 101 5|0 116 5
|
||||
100|0 116 5|-3 134 6
|
||||
101|-3 134 6|-2 154 0
|
||||
102|-2 154 0|6 177 0
|
||||
103|4 118 -2|4 123 -2
|
||||
104|10 131 -7|10 136 -7
|
||||
105|12 150 -7|12 155 -7
|
||||
106|3 100 1|4 118 -2
|
||||
107|4 118 -2|10 131 -7
|
||||
108|10 131 -7|12 150 -7
|
||||
109|12 150 -7|7 170 -13
|
||||
110|-1 138 1|-1 143 1
|
||||
111|-6 151 -8|-6 156 -8
|
||||
112|-3 168 -14|-3 173 -14
|
||||
113|0 121 5|-1 138 1
|
||||
114|-1 138 1|-6 151 -8
|
||||
115|-6 151 -8|-3 168 -14
|
||||
116|-3 168 -14|-4 189 -15
|
||||
117|-12 161 -1|-12 166 -1
|
||||
118|-19 175 -1|-19 180 -1
|
||||
119|-23 191 -8|-23 196 -8
|
||||
120|-9 142 -1|-12 161 -1
|
||||
121|-12 161 -1|-19 175 -1
|
||||
122|-19 175 -1|-23 191 -8
|
||||
123|-23 191 -8|-20 212 -11
|
||||
124|-6 184 -16|-6 189 -16
|
||||
125|-2 199 -15|-2 204 -15
|
||||
126|-7 165 -15|-6 184 -16
|
||||
127|-6 184 -16|-2 199 -15
|
||||
128|-2 199 -15|-7 217 -14
|
||||
129|0 212 -20|0 217 -20
|
||||
130|4 225 -29|4 230 -29
|
||||
131|0 194 -15|0 212 -20
|
||||
132|0 212 -20|4 225 -29
|
||||
133|4 225 -29|8 243 -30
|
||||
134|-9 243 -19|-9 248 -19
|
||||
135|-16 255 -27|-16 260 -27
|
||||
136|-7 225 -15|-9 243 -19
|
||||
137|-9 243 -19|-16 255 -27
|
||||
138|-16 255 -27|-14 272 -35
|
||||
139|0 39 16|0 68 12
|
||||
140|0 68 12|0 82 5
|
||||
141|0 82 5|3 100 1
|
||||
142|3 100 1|0 121 6
|
||||
143|0 121 6|-9 142 -1
|
||||
144|-9 142 -1|-7 165 -15
|
||||
145|-7 165 -15|0 194 -15
|
||||
146|0 194 -15|-7 225 -15
|
||||
147|-7 225 -15|-13 258 -26
|
||||
148|-9 102 25|-9 107 25
|
||||
149|-10 116 34|-10 121 34
|
||||
150|-18 132 37|-18 137 37
|
||||
151|-20 154 35|-20 159 35
|
||||
152|-8 84 22|-9 102 25
|
||||
153|-9 102 25|-10 116 34
|
||||
154|-10 116 34|-18 132 37
|
||||
155|-18 132 37|-20 154 35
|
||||
156|-20 154 35|-15 177 42
|
||||
157|-15 117 24|-15 122 24
|
||||
158|-14 132 23|-14 137 23
|
||||
159|-13 150 27|-13 155 27
|
||||
160|-20 170 26|-20 175 26
|
||||
161|-15 98 24|-15 117 24
|
||||
162|-15 117 24|-14 132 23
|
||||
163|-14 132 23|-13 150 27
|
||||
164|-13 150 27|-20 170 26
|
||||
165|-20 170 26|-20 193 18
|
||||
166|-22 134 18|-22 139 18
|
||||
167|-28 147 12|-28 152 12
|
||||
168|-28 166 10|-28 171 10
|
||||
169|-18 116 19|-22 134 18
|
||||
170|-22 134 18|-28 147 12
|
||||
171|-28 147 12|-28 166 10
|
||||
172|-28 166 10|-34 186 15
|
||||
173|-19 154 23|-19 159 23
|
||||
174|-28 167 28|-28 172 28
|
||||
175|-35 184 26|-35 189 26
|
||||
176|-14 137 21|-19 154 23
|
||||
177|-19 154 23|-28 167 28
|
||||
178|-28 167 28|-35 184 26
|
||||
179|-35 184 26|-35 205 28
|
||||
180|-21 177 35|-21 182 35
|
||||
181|-20 191 42|-20 196 42
|
||||
182|-26 207 47|-26 212 47
|
||||
183|-21 158 32|-21 177 35
|
||||
184|-21 177 35|-20 191 42
|
||||
185|-20 191 42|-26 207 47
|
||||
186|-26 207 47|-30 228 44
|
||||
187|-36 200 30|-36 205 30
|
||||
188|-35 215 26|-35 220 26
|
||||
189|-35 181 31|-36 200 30
|
||||
190|-36 200 30|-35 215 26
|
||||
191|-35 215 26|-34 233 31
|
||||
192|-41 228 24|-41 233 24
|
||||
193|-49 241 21|-49 246 21
|
||||
194|-36 210 24|-41 228 24
|
||||
195|-41 228 24|-49 241 21
|
||||
196|-49 241 21|-51 259 17
|
||||
197|-39 259 34|-39 264 34
|
||||
198|-46 271 41|-46 276 41
|
||||
199|-35 241 31|-39 259 34
|
||||
200|-39 259 34|-46 271 41
|
||||
201|-46 271 41|-54 288 40
|
||||
202|-3 55 21|-8 84 22
|
||||
203|-8 84 22|-15 98 24
|
||||
204|-15 98 24|-18 116 19
|
||||
205|-18 116 19|-14 137 21
|
||||
206|-14 137 21|-21 158 32
|
||||
207|-21 158 32|-35 181 31
|
||||
208|-35 181 31|-36 210 24
|
||||
209|-36 210 24|-35 241 31
|
||||
210|-35 241 31|-45 274 39
|
||||
211|4 120 28|4 125 28
|
||||
212|13 134 28|13 139 28
|
||||
213|17 150 36|17 155 36
|
||||
214|1 102 27|4 120 28
|
||||
215|4 120 28|13 134 28
|
||||
216|13 134 28|17 150 36
|
||||
217|17 150 36|15 172 37
|
||||
218|3 135 34|3 140 34
|
||||
219|2 150 33|2 155 33
|
||||
220|6 168 32|6 173 32
|
||||
221|3 116 34|3 135 34
|
||||
222|3 135 34|2 150 33
|
||||
223|2 150 33|6 168 32
|
||||
224|6 168 32|6 188 39
|
||||
225|-1 152 42|-1 157 42
|
||||
226|-5 165 49|-5 170 49
|
||||
227|0 134 38|-1 152 42
|
||||
228|-1 152 42|-5 165 49
|
||||
229|-5 165 49|-8 184 49
|
||||
230|3 172 38|3 177 38
|
||||
231|9 185 46|9 190 46
|
||||
232|0 155 33|3 172 38
|
||||
233|3 172 38|9 185 46
|
||||
234|9 185 46|8 202 53
|
||||
235|15 195 39|15 200 39
|
||||
236|21 209 37|21 214 37
|
||||
237|12 176 39|15 195 39
|
||||
238|15 195 39|21 209 37
|
||||
239|21 209 37|27 225 42
|
||||
240|11 218 54|11 223 54
|
||||
241|12 199 53|11 218 54
|
||||
242|11 218 54|8 233 54
|
||||
243|6 246 59|6 251 59
|
||||
244|6 228 54|6 246 59
|
||||
245|6 246 59|4 259 68
|
||||
246|0 73 23|1 102 27
|
||||
247|1 102 27|3 116 34
|
||||
248|3 116 34|0 134 38
|
||||
249|0 134 38|0 155 33
|
||||
250|0 155 33|12 176 39
|
||||
251|12 176 39|12 199 53
|
||||
252|12 199 53|6 228 54
|
||||
253|6 228 54|12 259 53
|
||||
254|9 139 25|9 144 25
|
||||
255|8 153 17|8 158 17
|
||||
256|15 169 12|15 174 12
|
||||
257|8 121 29|9 139 25
|
||||
258|9 139 25|8 153 17
|
||||
259|8 153 17|15 169 12
|
||||
260|15 169 12|17 191 14
|
||||
261|14 154 27|14 159 27
|
||||
262|13 169 28|13 174 28
|
||||
263|12 187 25|12 192 25
|
||||
264|14 135 27|14 154 27
|
||||
265|14 154 27|13 169 28
|
||||
266|13 169 28|12 187 25
|
||||
267|12 187 25|18 207 24
|
||||
268|22 171 32|22 176 32
|
||||
269|29 184 36|29 189 36
|
||||
270|18 153 32|22 171 32
|
||||
271|22 171 32|29 184 36
|
||||
272|29 184 36|30 203 38
|
||||
273|18 191 27|18 196 27
|
||||
274|26 204 20|26 209 20
|
||||
275|14 174 30|18 191 27
|
||||
276|18 191 27|26 204 20
|
||||
277|26 204 20|33 221 20
|
||||
278|18 214 15|18 219 15
|
||||
279|15 228 9|15 233 9
|
||||
280|18 195 18|18 214 15
|
||||
281|18 214 15|15 228 9
|
||||
282|15 228 9|20 244 2
|
||||
283|33 237 16|33 242 16
|
||||
284|32 218 15|33 237 16
|
||||
285|33 237 16|33 252 19
|
||||
286|40 265 22|40 270 22
|
||||
287|35 247 22|40 265 22
|
||||
288|40 265 22|49 278 22
|
||||
289|3 92 32|8 121 29
|
||||
290|8 121 29|14 135 27
|
||||
291|14 135 27|18 153 32
|
||||
292|18 153 32|14 174 30
|
||||
293|14 174 30|18 195 18
|
||||
294|18 195 18|32 218 15
|
||||
295|32 218 15|35 247 22
|
||||
296|35 247 22|32 278 15
|
||||
297|-6 161 40|-6 166 40
|
||||
298|-14 175 41|-14 180 41
|
||||
299|-2 143 40|-6 161 40
|
||||
300|-6 161 40|-14 175 41
|
||||
301|-14 175 41|-19 191 35
|
||||
302|-5 176 33|-5 181 33
|
||||
303|-4 191 34|-4 196 34
|
||||
304|-5 157 33|-5 176 33
|
||||
305|-5 176 33|-4 191 34
|
||||
306|-4 191 34|-7 209 36
|
||||
307|-1 193 25|-1 198 25
|
||||
308|-1 175 29|-1 193 25
|
||||
309|-1 193 25|1 206 16
|
||||
310|-7 213 30|-7 218 30
|
||||
311|-3 196 34|-7 213 30
|
||||
312|-7 213 30|-14 226 23
|
||||
313|-18 236 30|-18 241 30
|
||||
314|-15 217 30|-18 236 30
|
||||
315|-18 236 30|-24 250 34
|
||||
316|-18 240 17|-17 259 16
|
||||
317|0 114 43|-2 143 40
|
||||
318|-2 143 40|-5 157 33
|
||||
319|-5 157 33|-1 175 29
|
||||
320|-1 175 29|-3 196 34
|
||||
321|-3 196 34|-15 217 30
|
||||
322|-15 217 30|-18 240 17
|
||||
323|-18 240 17|-12 269 14
|
||||
324|-5 190 61|-5 195 61
|
||||
325|-3 204 69|-3 209 69
|
||||
326|-5 172 57|-5 190 61
|
||||
327|-5 190 61|-3 204 69
|
||||
328|-3 204 69|-8 220 75
|
||||
329|-11 205 60|-11 210 60
|
||||
330|-11 220 58|-11 225 58
|
||||
331|-11 186 60|-11 205 60
|
||||
332|-11 205 60|-11 220 58
|
||||
333|-11 220 58|-8 238 62
|
||||
334|-20 222 57|-20 227 57
|
||||
335|-16 204 57|-20 222 57
|
||||
336|-20 222 57|-28 235 55
|
||||
337|-14 242 62|-14 247 62
|
||||
338|-11 225 58|-14 242 62
|
||||
339|-14 242 62|-20 255 70
|
||||
340|-12 265 72|-12 270 72
|
||||
341|-13 246 70|-12 265 72
|
||||
342|-12 265 72|-8 279 78
|
||||
343|-26 269 75|-28 288 75
|
||||
344|-1 143 53|-5 172 57
|
||||
345|-5 172 57|-11 186 60
|
||||
346|-11 186 60|-16 204 57
|
||||
347|-16 204 57|-11 225 59
|
||||
348|-11 225 59|-13 246 70
|
||||
349|-13 246 70|-26 269 75
|
||||
350|-26 269 75|-30 298 69
|
||||
351|8 223 67|8 228 67
|
||||
352|4 205 67|8 223 67
|
||||
353|8 223 67|15 237 63
|
||||
354|8 238 72|8 243 72
|
||||
355|8 219 72|8 238 72
|
||||
356|8 238 72|6 253 72
|
||||
357|5 237 77|5 255 81
|
||||
358|6 258 72|11 275 75
|
||||
359|18 279 73|20 298 72
|
||||
360|0 176 63|4 205 67
|
||||
361|4 205 67|8 219 72
|
||||
362|8 219 72|5 237 77
|
||||
363|5 237 77|6 258 72
|
||||
364|6 258 72|18 279 73
|
||||
365|18 279 73|25 302 85
|
||||
366|3 262 69|3 267 69
|
||||
367|3 244 73|3 262 69
|
||||
368|3 262 69|-1 276 63
|
||||
369|8 277 68|8 282 68
|
||||
370|8 258 68|8 277 68
|
||||
371|8 277 68|8 292 70
|
||||
372|13 276 70|17 294 69
|
||||
373|8 297 70|10 314 65
|
||||
374|8 318 58|7 337 56
|
||||
375|0 215 77|3 244 73
|
||||
376|3 244 73|8 258 68
|
||||
377|8 258 68|13 276 70
|
||||
378|13 276 70|8 297 70
|
||||
379|8 297 70|8 318 58
|
||||
380|8 318 58|19 341 50
|
||||
381|0 0 0|0 28 9
|
||||
382|0 28 9|0 39 16
|
||||
383|0 39 16|-3 55 21
|
||||
384|-3 55 21|0 73 23
|
||||
385|0 73 23|3 92 32
|
||||
386|3 92 32|0 114 43
|
||||
387|0 114 43|-1 143 53
|
||||
388|-1 143 53|0 176 63
|
||||
389|0 176 63|0 215 77
|
||||
390|0 215 77|0 259 93
|
||||
@@ -1,35 +0,0 @@
|
||||
|
||||
class Button extends Control {
|
||||
boolean selected = false;
|
||||
boolean down = false;
|
||||
Button(int x, int y, int w, int h, String label) {
|
||||
super(x,y,w,h,label);
|
||||
}
|
||||
|
||||
boolean mousePressed() {
|
||||
if (super.mousePressed() && !selected)
|
||||
down = true;
|
||||
return down;
|
||||
}
|
||||
|
||||
boolean mouseReleased() {
|
||||
down = false;
|
||||
if (super.mouseIn()) {
|
||||
selected = !selected;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void drawContents() {
|
||||
if (selected||down) {
|
||||
if (!selected&&(over^down))
|
||||
fill(0xDD,0xDD,0xDD);
|
||||
else
|
||||
fill(0x00,0x99,0xFF);
|
||||
noStroke();
|
||||
rect(x+1,y+1,w-1,h-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
|
||||
class Control {
|
||||
int x,y,w,h;
|
||||
boolean over = false;
|
||||
String label;
|
||||
|
||||
Control(int x, int y, int w, int h, String label) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.w = w;
|
||||
this.h = h;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
boolean mouseIn() {
|
||||
return over = mouseX>x&&mouseX<x+w&&mouseY>y&&mouseY<y+h;
|
||||
}
|
||||
|
||||
boolean mousePressed() {
|
||||
return mouseIn();
|
||||
}
|
||||
|
||||
void mouseDragged() {
|
||||
mouseIn();
|
||||
}
|
||||
|
||||
boolean mouseReleased() {
|
||||
return mouseIn();
|
||||
}
|
||||
|
||||
void display() {
|
||||
stroke(0x00,0x99,0xFF);
|
||||
if (over)
|
||||
fill(0xDD,0xDD,0xDD);
|
||||
else
|
||||
fill(0xFF,0xFF,0xFF);
|
||||
rect(x,y,w,h);
|
||||
drawContents();
|
||||
stroke(0);
|
||||
fill(0);
|
||||
text(label, x+2, (y+h)-3);
|
||||
}
|
||||
|
||||
void drawContents() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
|
||||
class Mass {
|
||||
static final float diamter = 5;
|
||||
static final float radius = 1+diamter/2;
|
||||
|
||||
float x,y,xv,yv;
|
||||
|
||||
Mass(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
void update() {
|
||||
yv += g.value;
|
||||
|
||||
double speed = sqrt(xv*xv+yv*yv);
|
||||
double fs = 1-f.value;
|
||||
if (speed>speedFrictionThreshold)
|
||||
fs *= speedFrictionThreshold/speed;
|
||||
xv *= fs;
|
||||
yv *= fs;
|
||||
|
||||
x += xv;
|
||||
y += yv;
|
||||
|
||||
if (x<radius) {
|
||||
x -= x-radius;
|
||||
xv = -xv;
|
||||
} else if (x>width-radius) {
|
||||
x -= x-(width-radius);
|
||||
xv = -xv;
|
||||
}
|
||||
if (y<sliderHeight+radius) {
|
||||
y -= y-(sliderHeight+radius);
|
||||
yv = -yv;
|
||||
} else if (y>height-radius) {
|
||||
y -= y-(height-radius);
|
||||
yv = -yv;
|
||||
}
|
||||
}
|
||||
|
||||
void clamp() {
|
||||
if (x<radius) {
|
||||
x = radius;
|
||||
} else if (x>width-radius) {
|
||||
x = width-radius;
|
||||
}
|
||||
if (y<sliderHeight+radius) {
|
||||
y = sliderHeight+radius;
|
||||
} else if (y>height-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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,329 +0,0 @@
|
||||
/**
|
||||
* 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<controls.length;i++)
|
||||
if (controls[i] instanceof Button && ((Button)controls[i]).selected)
|
||||
mode = i;
|
||||
if (mode != MAKE)
|
||||
drawMass = null;
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
// saveFrame();
|
||||
}
|
||||
|
||||
void mouseMoved() {
|
||||
for (int i = 0;i<controls.length;i++)
|
||||
controls[i].mouseIn();
|
||||
|
||||
tempDistance = -1;
|
||||
Mass m = null;
|
||||
if (mode == MOVE || mode == MAKE || mode == DELETE)
|
||||
m = mouseMass();
|
||||
float md = tempDistance;
|
||||
tempDistance = -1;
|
||||
Spring s = null;
|
||||
if (mode == DELETE)
|
||||
s = mouseSpring();
|
||||
float sd = tempDistance;
|
||||
if (m != null && md != -1 && (md <= sd || sd == -1) && md < mouseTolerance) {
|
||||
overMass = m;
|
||||
overSpring = null;
|
||||
} else if (s != null && sd != -1 && (sd<md || md == -1) && sd<mouseTolerance) {
|
||||
overSpring = s;
|
||||
overMass = null;
|
||||
} else {
|
||||
overMass = null;
|
||||
overSpring = null;
|
||||
}
|
||||
}
|
||||
|
||||
void mouseDragged() {
|
||||
if (activeControl != null)
|
||||
activeControl.mouseDragged();
|
||||
else
|
||||
if (dragMass != null) {
|
||||
dragMass.x = mouseX+dragDx;
|
||||
dragMass.y = mouseY+dragDy;
|
||||
dragMass.xv = mouseX-pmouseX;
|
||||
dragMass.yv = mouseY-pmouseY;
|
||||
dragMass.clamp();
|
||||
}
|
||||
}
|
||||
|
||||
void mouseReleased() {
|
||||
if (activeControl != null) {
|
||||
if (activeControl.mouseReleased() && activeControl instanceof Button) {
|
||||
for (int i = 0;i<controls.length;i++)
|
||||
if (controls[i] != activeControl && controls[i] instanceof Button)
|
||||
((Button)controls[i]).selected = false;
|
||||
checkMode();
|
||||
}
|
||||
activeControl = null;
|
||||
}
|
||||
if (dragMass != null) {
|
||||
if (overMass == dragMass)
|
||||
overMass = null;
|
||||
dragMass = null;
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
activeControl = null;
|
||||
for (int i = 0;i<controls.length && activeControl == null;i++)
|
||||
if ( controls[i].mousePressed() && !(controls[i] instanceof Button && ((Button)controls[i]).selected))
|
||||
activeControl = controls[i];
|
||||
if (activeControl == null) {
|
||||
switch(mode) {
|
||||
case MAKE:
|
||||
Mass m = mouseMass();
|
||||
if (m != null && tempDistance<mouseTolerance) {
|
||||
if (drawMass != null) {
|
||||
if (drawMass != m) {
|
||||
boolean springExists = false;
|
||||
for (int i = 0;i<springCount&&!springExists;i++)
|
||||
springExists = ((springs[i].a == drawMass && springs[i].b == m)||(springs[i].b == drawMass && springs[i].a == m));
|
||||
if (!springExists)
|
||||
addSpring(new Spring(drawMass,m));
|
||||
}
|
||||
drawMass = null;
|
||||
} else {
|
||||
drawMass = m;
|
||||
}
|
||||
} else {
|
||||
Mass newMass;
|
||||
addMass(newMass = new Mass(mouseX,mouseY));
|
||||
if (drawMass != null)
|
||||
addSpring(new Spring(drawMass,newMass));
|
||||
drawMass = newMass;
|
||||
}
|
||||
break;
|
||||
case MOVE:
|
||||
m = mouseMass();
|
||||
if (m != null && tempDistance<mouseTolerance) {
|
||||
overMass = dragMass = m;
|
||||
dragDx = m.x-mouseX;
|
||||
dragDy = m.y-mouseY;
|
||||
} else
|
||||
overMass = dragMass = null;
|
||||
break;
|
||||
case DELETE:
|
||||
if (overMass != null) {
|
||||
for (int i = 0;i<springCount;i++)
|
||||
if (springs[i].a == overMass || springs[i].b == overMass)
|
||||
deleteSpring(springs[i--]);
|
||||
deleteMass(overMass);
|
||||
if (overMass == dragMass)
|
||||
dragMass = null;
|
||||
overMass = null;
|
||||
} else if (overSpring != null) {
|
||||
deleteSpring(overSpring);
|
||||
overSpring = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Mass mouseMass() {
|
||||
tempDistance = -1;
|
||||
Mass m = null;
|
||||
for (int i = 0;i<massCount;i++) {
|
||||
float d = masses[i].distanceTo(mouseX,mouseY);
|
||||
if (d != -1 && (d<tempDistance || tempDistance == -1)) {
|
||||
tempDistance = d;
|
||||
m = masses[i];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
Spring mouseSpring() {
|
||||
tempDistance = -1;
|
||||
Spring s = null;
|
||||
for (int i = 0;i<springCount;i++) {
|
||||
float d = springs[i].distanceTo(mouseX,mouseY);
|
||||
if (d != -1 && (d<tempDistance || tempDistance == -1)) {
|
||||
tempDistance = d;
|
||||
s = springs[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void doUpdate() {
|
||||
for (int i = 0;i<springCount;i++)
|
||||
springs[i].applyForces();
|
||||
for (int i = 0;i<massCount;i++)
|
||||
if (masses[i] != dragMass)
|
||||
masses[i].update();
|
||||
}
|
||||
|
||||
void display() {
|
||||
stroke(0x00,0x99,0xFF);
|
||||
fill(0xFF,0xFF,0xFF);
|
||||
rect(0,0,width-1,height-1);
|
||||
|
||||
for (int i = 0;i<springCount;i++)
|
||||
springs[i].display();
|
||||
|
||||
if (drawMass != null) {
|
||||
stroke(0x00,0x99,0xFF);
|
||||
line(drawMass.x,drawMass.y,mouseX,mouseY);
|
||||
}
|
||||
|
||||
for (int i = 0;i<massCount;i++)
|
||||
masses[i].display();
|
||||
|
||||
for (int i = 0;i<controls.length;i++) {
|
||||
controls[i].display();
|
||||
}
|
||||
fill(0x00,0x99,0xFF);
|
||||
for (int i = 0;i<controls.length;i++)
|
||||
if (controls[i].over)
|
||||
text(controls[i].label+toolTips[i], 2, sliderHeight*3-3);
|
||||
}
|
||||
|
||||
// list handling for masses
|
||||
|
||||
void addMass(Mass mass) {
|
||||
if (massCount == masses.length) {
|
||||
Mass temp[] = new Mass[massCount*2];
|
||||
System.arraycopy(masses, 0, temp, 0, massCount);
|
||||
masses = temp;
|
||||
}
|
||||
masses[massCount++] = mass;
|
||||
}
|
||||
|
||||
void deleteMass(Mass mass) {
|
||||
int index = massIndex(mass);
|
||||
if (index >= 0)
|
||||
deleteMassIndex(index);
|
||||
}
|
||||
|
||||
void deleteMassIndex(int index) {
|
||||
if (index<massCount)
|
||||
System.arraycopy(masses, index+1, masses, index, massCount-index);
|
||||
massCount--;
|
||||
}
|
||||
|
||||
int massIndex(Mass mass) {
|
||||
for (int i = 0;i<massCount;i++)
|
||||
if (masses[i] == mass)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// list handling for springs
|
||||
|
||||
void addSpring(Spring spring) {
|
||||
if (springCount == springs.length) {
|
||||
Spring temp[] = new Spring[springCount*2];
|
||||
System.arraycopy(springs, 0, temp, 0, springCount);
|
||||
springs = temp;
|
||||
}
|
||||
springs[springCount++] = spring;
|
||||
}
|
||||
|
||||
void deleteSpring(Spring spring) {
|
||||
int index = springIndex(spring);
|
||||
if (index >= 0)
|
||||
deleteSpringIndex(index);
|
||||
}
|
||||
|
||||
void deleteSpringIndex(int index) {
|
||||
if (index<springCount)
|
||||
System.arraycopy(springs, index+1, springs, index, springCount-index);
|
||||
springCount--;
|
||||
}
|
||||
|
||||
int springIndex(Spring spring) {
|
||||
for (int i = 0;i<springCount;i++)
|
||||
if (springs[i] == spring)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// end of list handling
|
||||
|
||||