Re-adding Topics to SVN

This commit is contained in:
Casey Reas
2011-09-05 23:58:47 +00:00
parent 4fc6dcca86
commit 391c79c2b6
219 changed files with 31093 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
/**
* Follow 1.
* Based on code from Keith Peters (www.bit-101.com).
*
* A line segment is pushed and pulled by the cursor.
*/
float x = 100;
float y = 100;
float angle1 = 0.0;
float segLength = 50;
void setup() {
size(200, 200);
smooth();
strokeWeight(20.0);
stroke(0, 100);
}
void draw() {
background(226);
float dx = mouseX - x;
float dy = mouseY - y;
angle1 = atan2(dy, dx);
x = mouseX - (cos(angle1) * segLength);
y = mouseY - (sin(angle1) * segLength);
segment(x, y, angle1);
ellipse(x, y, 20, 20);
}
void segment(float x, float y, float a) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}

View File

@@ -0,0 +1,42 @@
/**
* Follow 2.
* Based on code from Keith Peters (www.bit-101.com).
*
* A two-segmented arm follows the cursor position. The relative
* angle between the segments is calculated with atan2() and the
* position calculated with sin() and cos().
*/
float[] x = new float[2];
float[] y = new float[2];
float segLength = 50;
void setup() {
size(200, 200);
smooth();
strokeWeight(20.0);
stroke(0, 100);
}
void draw() {
background(226);
dragSegment(0, mouseX, mouseY);
dragSegment(1, x[0], y[0]);
}
void dragSegment(int i, float xin, float yin) {
float dx = xin - x[i];
float dy = yin - y[i];
float angle = atan2(dy, dx);
x[i] = xin - cos(angle) * segLength;
y[i] = yin - sin(angle) * segLength;
segment(x[i], y[i], angle);
}
void segment(float x, float y, float a) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}

View File

@@ -0,0 +1,44 @@
/**
* Follow 3.
* Based on code from Keith Peters (www.bit-101.com).
*
* A segmented line follows the mouse. The relative angle from
* each segment to the next is calculated with atan2() and the
* position of the next is calculated with sin() and cos().
*/
float[] x = new float[20];
float[] y = new float[20];
float segLength = 9;
void setup() {
size(200, 200);
smooth();
strokeWeight(5);
stroke(0, 100);
}
void draw() {
background(226);
dragSegment(0, mouseX, mouseY);
for(int i=0; i<x.length-1; i++) {
dragSegment(i+1, x[i], y[i]);
}
}
void dragSegment(int i, float xin, float yin) {
float dx = xin - x[i];
float dy = yin - y[i];
float angle = atan2(dy, dx);
x[i] = xin - cos(angle) * segLength;
y[i] = yin - sin(angle) * segLength;
segment(x[i], y[i], angle);
}
void segment(float x, float y, float a) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}

View File

@@ -0,0 +1,49 @@
/**
* Reach 1.
* Based on code from Keith Peters (www.bit-101.com)
*
* The arm follows the position of the mouse by
* calculating the angles with atan2().
*/
float x = 100;
float y = 100;
float x2 = 100;
float y2 = 100;
float segLength = 30;
void setup() {
size(200, 200);
smooth();
strokeWeight(20.0);
stroke(0, 100);
}
void draw() {
background(226);
float dx = mouseX - x;
float dy = mouseY - y;
float angle1 = atan2(dy, dx);
float tx = mouseX - cos(angle1) * segLength;
float ty = mouseY - sin(angle1) * segLength;
dx = tx - x2;
dy = ty - y2;
float angle2 = atan2(dy, dx);
x = x2 + cos(angle2) * segLength;
y = y2 + sin(angle2) * segLength;
segment(x, y, angle1);
segment(x2, y2, angle2);
}
void segment(float x, float y, float a) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}

View File

@@ -0,0 +1,60 @@
/**
* Reach 2.
* Based on code from Keith Peters (www.bit-101.com)
*
* The arm follows the position of the mouse by
* calculating the angles with atan2().
*/
int numSegments = 10;
float[] x = new float[numSegments];
float[] y = new float[numSegments];
float[] angle = new float[numSegments];
float segLength = 20;
float targetX, targetY;
void setup() {
size(200, 200);
smooth();
strokeWeight(20.0);
stroke(0, 100);
x[x.length-1] = 0; // Set base x-coordinate
y[x.length-1] = height; // Set base y-coordinate
}
void draw() {
background(226);
reachSegment(0, mouseX, mouseY);
for(int i=1; i<numSegments; i++) {
reachSegment(i, targetX, targetY);
}
for(int i=x.length-1; i>=1; i--) {
positionSegment(i, i-1);
}
for(int i=0; i<x.length; i++) {
segment(x[i], y[i], angle[i], (i+1)*2);
}
}
void positionSegment(int a, int b) {
x[b] = x[a] + cos(angle[a]) * segLength;
y[b] = y[a] + sin(angle[a]) * segLength;
}
void reachSegment(int i, float xin, float yin) {
float dx = xin - x[i];
float dy = yin - y[i];
angle[i] = atan2(dy, dx);
targetX = xin - cos(angle[i]) * segLength;
targetY = yin - sin(angle[i]) * segLength;
}
void segment(float x, float y, float a, float sw) {
strokeWeight(sw);
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}

View File

@@ -0,0 +1,77 @@
/**
* Reach 3.
* Based on code from Keith Peters (www.bit-101.com)
*
* The arm follows the position of the ball by
* calculating the angles with atan2().
*/
int numSegments = 6;
float[] x = new float[numSegments];
float[] y = new float[numSegments];
float[] angle = new float[numSegments];
float segLength = 15;
float targetX, targetY;
float ballX = 50;
float ballY = 50;
int ballXDirection = 1;
int ballYDirection = -1;
void setup() {
size(200, 200);
smooth();
strokeWeight(20.0);
stroke(0, 100);
noFill();
x[x.length-1] = width/2; // Set base x-coordinate
y[x.length-1] = height; // Set base y-coordinate
}
void draw() {
background(226);
strokeWeight(20);
ballX = ballX + 1.0 * ballXDirection;
ballY = ballY + 0.8 * ballYDirection;
if(ballX > width-25 || ballX < 25) {
ballXDirection *= -1;
}
if(ballY > height-25 || ballY < 25) {
ballYDirection *= -1;
}
ellipse(ballX, ballY, 30, 30);
reachSegment(0, ballX, ballY);
for(int i=1; i<numSegments; i++) {
reachSegment(i, targetX, targetY);
}
for(int i=x.length-1; i>=1; i--) {
positionSegment(i, i-1);
}
for(int i=0; i<x.length; i++) {
segment(x[i], y[i], angle[i], (i+1)*2);
}
}
void positionSegment(int a, int b) {
x[b] = x[a] + cos(angle[a]) * segLength;
y[b] = y[a] + sin(angle[a]) * segLength;
}
void reachSegment(int i, float xin, float yin) {
float dx = xin - x[i];
float dy = yin - y[i];
angle[i] = atan2(dy, dx);
targetX = xin - cos(angle[i]) * segLength;
targetY = yin - sin(angle[i]) * segLength;
}
void segment(float x, float y, float a, float sw) {
strokeWeight(sw);
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}

View File

@@ -0,0 +1,37 @@
/**
* Tickle.
*
* The word "tickle" jitters when the cursor hovers over.
* Sometimes, it can be tickled off the screen.
*/
String message = "tickle";
float x, y; // X and Y coordinates of text
float hr, vr; // horizontal and vertical radius of the text
void setup() {
size(200, 200);
PFont font = loadFont("AmericanTypewriter-24.vlw");
textFont(font);
textAlign(CENTER, CENTER);
hr = textWidth(message) / 2;
vr = (textAscent() + textDescent()) / 2;
noStroke();
x = width / 2;
y = height / 2;
}
void draw() {
// instead of clearing the background, fade it by drawing
// a semi-transparent rectangle on top
fill(204, 120);
rect(0, 0, width, height);
fill(0);
// If the cursor is over the text, change the position
if (abs(mouseX - x) < hr &&
abs(mouseY - y) < vr) {
x += random(-5, 5);
y += random(-5, 5);
}
text("tickle", x, y);
}