This commit is contained in:
Casey Reas
2011-09-06 05:22:47 +00:00
parent 3b84c790cd
commit 9775dedaad
17 changed files with 89 additions and 311 deletions
@@ -1,6 +1,6 @@
/**
* Penrose Snowflake L-System
* by Geraldine Sarmiento (NYU ITP).
* by Geraldine Sarmiento.
*
* This code was based on Patrick Dwyer's L-System class.
*/
@@ -1,6 +1,6 @@
/**
* Penrose Tile L-System
* by Geraldine Sarmiento (NYU ITP).
* by Geraldine Sarmiento.
*
* This code was based on Patrick Dwyer's L-System class.
*/
@@ -1,10 +1,10 @@
/**
* Pentigree L-System
* by Geraldine Sarmiento (NYU ITP).
* by Geraldine Sarmiento.
*
* This code was based on Patrick Dwyer's L-System class.
*/
PentigreeLSystem ps;
@@ -20,4 +20,3 @@ void draw() {
ps.render();
}
@@ -1,220 +0,0 @@
/**
* Buttons.
*
* Click on one of the shapes to change
* the background color. This example
* demonstates a class for buttons.
*/
color currentcolor;
CircleButton circle1, circle2, circle3;
RectButton rect1, rect2;
boolean locked = false;
void setup()
{
size(640, 360);
smooth();
color baseColor = color(102);
currentcolor = baseColor;
// Define and create circle button
color buttoncolor = color(204);
color highlight = color(153);
ellipseMode(CENTER);
circle1 = new CircleButton(30, 100, 100, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(204);
highlight = color(153);
circle2 = new CircleButton(130, 110, 24, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(153);
highlight = color(102);
circle3 = new CircleButton(130, 140, 24, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(102);
highlight = color(51);
rect1 = new RectButton(150, 20, 100, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(51);
highlight = color(0);
rect2 = new RectButton(90, 20, 50, buttoncolor, highlight);
}
void draw()
{
background(currentcolor);
stroke(255);
update(mouseX, mouseY);
circle1.display();
circle2.display();
circle3.display();
rect1.display();
rect2.display();
}
void update(int x, int y)
{
if(locked == false) {
circle1.update();
circle2.update();
circle3.update();
rect1.update();
rect2.update();
}
else {
locked = false;
}
if(mousePressed) {
if(circle1.pressed()) {
currentcolor = circle1.basecolor;
}
else if(circle2.pressed()) {
currentcolor = circle2.basecolor;
}
else if(circle3.pressed()) {
currentcolor = circle3.basecolor;
}
else if(rect1.pressed()) {
currentcolor = rect1.basecolor;
}
else if(rect2.pressed()) {
currentcolor = rect2.basecolor;
}
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(over) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
boolean overCircle(int x, int y, int diameter)
{
float disX = x - mouseX;
float disY = y - mouseY;
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
}
else {
return false;
}
}
}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overCircle(x, y, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
ellipse(x, y, size, size);
}
}
class RectButton extends Button
{
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
+6 -6
View File
@@ -31,7 +31,7 @@ void draw() {
void mouseReleased() {
for(int i=0; i<num; i++) {
handles[i].release();
handles[i].releaseEvent();
}
}
@@ -71,8 +71,8 @@ class Handle {
}
if(otherslocked == false) {
over();
press();
overEvent();
pressEvent();
}
if(press) {
@@ -80,7 +80,7 @@ class Handle {
}
}
void over() {
void overEvent() {
if(overRect(boxx, boxy, size, size)) {
over = true;
} else {
@@ -88,7 +88,7 @@ class Handle {
}
}
void press() {
void pressEvent() {
if(over && mousePressed || locked) {
press = true;
locked = true;
@@ -97,7 +97,7 @@ class Handle {
}
}
void release() {
void releaseEvent() {
locked = false;
}
@@ -13,8 +13,8 @@ int topWidth, bottomWidth; // The width of the top and bottom images
void setup() {
size(640, 360);
noStroke();
hs1 = new HScrollbar(0, 20, width, 20, 3*5+1);
hs2 = new HScrollbar(0, height-20, width, 20, 3*5+1);
hs1 = new HScrollbar(0, height*0.33, width, 20, 3*5+1);
hs2 = new HScrollbar(0, height*0.66, width, 20, 3*5+1);
top = loadImage("seedTop.jpg");
topWidth = top.width;
bottom = loadImage("seedBottom.jpg");
@@ -45,15 +45,15 @@ void draw() {
class HScrollbar {
int swidth, sheight; // width and height of bar
int xpos, ypos; // x and y position of bar
float xpos, ypos; // x and y position of bar
float spos, newspos; // x position of slider
int sposMin, sposMax; // max and min values of slider
float sposMin, sposMax; // max and min values of slider
int loose; // how loose/heavy
boolean over; // is the mouse over the slider?
boolean locked;
float ratio;
HScrollbar (int xp, int yp, int sw, int sh, int l) {
HScrollbar (float xp, float yp, int sw, int sh, int l) {
swidth = sw;
sheight = sh;
int widthtoheight = sw - sh;
@@ -68,7 +68,7 @@ class HScrollbar {
}
void update() {
if(over()) {
if(overEvent()) {
over = true;
} else {
over = false;
@@ -87,11 +87,11 @@ class HScrollbar {
}
}
int constrain(int val, int minv, int maxv) {
float constrain(float val, float minv, float maxv) {
return min(max(val, minv), maxv);
}
boolean over() {
boolean overEvent() {
if(mouseX > xpos && mouseX < xpos+swidth &&
mouseY > ypos && mouseY < ypos+sheight) {
return true;
@@ -101,10 +101,10 @@ class HScrollbar {
}
void display() {
fill(255);
fill(204);
rect(xpos, ypos, swidth, sheight);
if(over || locked) {
fill(153, 102, 0);
fill(0, 0, 0);
} else {
fill(102, 102, 102);
}
@@ -11,14 +11,14 @@ float angle1 = 0.0;
float segLength = 50;
void setup() {
size(200, 200);
size(640, 360);
smooth();
strokeWeight(20.0);
stroke(0, 100);
stroke(255, 100);
}
void draw() {
background(226);
background(0);
float dx = mouseX - x;
float dy = mouseY - y;
@@ -12,14 +12,14 @@ float[] y = new float[2];
float segLength = 50;
void setup() {
size(200, 200);
size(640, 360);
smooth();
strokeWeight(20.0);
stroke(0, 100);
stroke(255, 100);
}
void draw() {
background(226);
background(0);
dragSegment(0, mouseX, mouseY);
dragSegment(1, x[0], y[0]);
}
@@ -9,17 +9,17 @@
float[] x = new float[20];
float[] y = new float[20];
float segLength = 9;
float segLength = 18;
void setup() {
size(200, 200);
size(640, 360);
smooth();
strokeWeight(5);
stroke(0, 100);
strokeWeight(9);
stroke(255, 100);
}
void draw() {
background(226);
background(0);
dragSegment(0, mouseX, mouseY);
for(int i=0; i<x.length-1; i++) {
dragSegment(i+1, x[i], y[i]);
@@ -7,21 +7,23 @@
*/
float x = 100;
float y = 100;
float x2 = 100;
float y2 = 100;
float segLength = 30;
float segLength = 80;
float x, y, x2, y2;
void setup() {
size(200, 200);
size(640, 360);
smooth();
strokeWeight(20.0);
stroke(0, 100);
stroke(255, 100);
x = width/2;
y = height/2;
x2 = x;
y2 = y;
}
void draw() {
background(226);
background(0);
float dx = mouseX - x;
float dy = mouseY - y;
@@ -10,20 +10,20 @@ int numSegments = 10;
float[] x = new float[numSegments];
float[] y = new float[numSegments];
float[] angle = new float[numSegments];
float segLength = 20;
float segLength = 26;
float targetX, targetY;
void setup() {
size(200, 200);
size(640, 360);
smooth();
strokeWeight(20.0);
stroke(0, 100);
x[x.length-1] = 0; // Set base x-coordinate
stroke(255, 100);
x[x.length-1] = width/2; // Set base x-coordinate
y[x.length-1] = height; // Set base y-coordinate
}
void draw() {
background(226);
background(0);
reachSegment(0, mouseX, mouseY);
for(int i=1; i<numSegments; i++) {
@@ -6,11 +6,11 @@
* calculating the angles with atan2().
*/
int numSegments = 6;
int numSegments = 8;
float[] x = new float[numSegments];
float[] y = new float[numSegments];
float[] angle = new float[numSegments];
float segLength = 15;
float segLength = 26;
float targetX, targetY;
float ballX = 50;
@@ -19,17 +19,17 @@ int ballXDirection = 1;
int ballYDirection = -1;
void setup() {
size(200, 200);
size(640, 360);
smooth();
strokeWeight(20.0);
stroke(0, 100);
stroke(255, 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);
background(0);
strokeWeight(20);
ballX = ballX + 1.0 * ballXDirection;
@@ -9,14 +9,14 @@
Spring2D s1, s2;
float gravity = 6.0;
float gravity = 9.0;
float mass = 2.0;
void setup()
{
size(200, 200);
size(640, 360);
smooth();
fill(0);
fill(255, 126);
// Inputs: x, y, mass, gravity
s1 = new Spring2D(0.0, width/2, mass, gravity);
s2 = new Spring2D(0.0, width/2, mass, gravity);
@@ -24,7 +24,7 @@ void setup()
void draw()
{
background(204);
background(0);
s1.update(mouseX, mouseY);
s1.display(mouseX, mouseY);
s2.update(s1.x, s1.y);
@@ -36,7 +36,7 @@ class Spring2D {
float x, y; // The x- and y-coordinates
float gravity;
float mass;
float radius = 20;
float radius = 30;
float stiffness = 0.2;
float damping = 0.7;
@@ -63,7 +63,7 @@ class Boid {
// Takes a second argument, if true, it slows down as it approaches the target
PVector steer(PVector target, boolean slowdown) {
PVector steer; // The steering vector
PVector desired = target.sub(target,loc); // A vector pointing from the location to the target
PVector desired = PVector.sub(target,loc); // A vector pointing from the location to the target
float d = desired.mag(); // Distance from the target is the magnitude of the vector
// If the distance is greater than 0, calc steering (otherwise return zero vector)
if (d > 0) {
@@ -73,7 +73,7 @@ class Boid {
if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
else desired.mult(maxspeed);
// Steering = Desired minus Velocity
steer = target.sub(desired,vel);
steer = PVector.sub(desired,vel);
steer.limit(maxforce); // Limit to maximum steering force
}
else {
@@ -11,7 +11,7 @@ Random generator;
void setup() {
size(640, 200);
size(640, 360);
colorMode(RGB, 255, 255, 255, 100);
// Using a Java random number generator for Gaussian random numbers
+24 -27
View File
@@ -5,47 +5,47 @@
*/
// Spring drawing constants for top bar
int s_height = 16; // Height
int left = 50; // Left position
int right = 150; // Right position
int max = 100; // Maximum Y value
int min = 20; // Minimum Y value
boolean over = false; // If mouse over
boolean move = false; // If mouse down and over
int springHeight = 16; // Height
int left; // Left position
int right; // Right position
int max = 200; // Maximum Y value
int min = 100; // Minimum Y value
boolean over = false; // If mouse over
boolean move = false; // If mouse down and over
// Spring simulation constants
float M = 0.8; // Mass
float K = 0.2; // Spring constant
float D = 0.92; // Damping
float R = 60; // Rest position
float R = 150; // Rest position
// Spring simulation variables
float ps = 60.0; // Position
float ps = R; // Position
float vs = 0.0; // Velocity
float as = 0; // Acceleration
float f = 0; // Force
void setup()
{
size(200, 200);
void setup() {
size(640, 360);
rectMode(CORNERS);
noStroke();
left = width/2 - 100;
right = width/2 + 100;
}
void draw()
{
void draw() {
background(102);
updateSpring();
drawSpring();
}
void drawSpring()
{
void drawSpring() {
// Draw base
fill(0.2);
float b_width = 0.5 * ps + -8;
rect(width/2 - b_width, ps + s_height, width/2 + b_width, 150);
float baseWidth = 0.5 * ps + -8;
rect(width/2 - baseWidth, ps + springHeight, width/2 + baseWidth, height);
// Set color and draw top bar
if(over || move) {
@@ -53,12 +53,11 @@ void drawSpring()
} else {
fill(204);
}
rect(left, ps, right, ps + s_height);
rect(left, ps, right, ps + springHeight);
}
void updateSpring()
{
void updateSpring() {
// Update the spring position
if(!move) {
f = -K * (ps - R); // f=-ky
@@ -71,7 +70,7 @@ void updateSpring()
}
// Test if mouse is over the top bar
if(mouseX > left && mouseX < right && mouseY > ps && mouseY < ps + s_height) {
if(mouseX > left && mouseX < right && mouseY > ps && mouseY < ps + springHeight) {
over = true;
} else {
over = false;
@@ -79,9 +78,8 @@ void updateSpring()
// Set and constrain the position of top bar
if(move) {
ps = mouseY - s_height/2;
if (ps < min) { ps = min; }
if (ps > max) { ps = max; }
ps = mouseY - springHeight/2;
ps = constrain(ps, min, max);
}
}
@@ -91,7 +89,6 @@ void mousePressed() {
}
}
void mouseReleased()
{
void mouseReleased() {
move = false;
}
@@ -12,12 +12,12 @@ Spring[] springs = new Spring[num];
void setup()
{
size(200, 200);
size(640, 360);
noStroke();
smooth();
springs[0] = new Spring( 70, 160, 20, 0.98, 8.0, 0.1, springs, 0);
springs[1] = new Spring(150, 110, 60, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring( 40, 70, 120, 0.90, 9.9, 0.1, springs, 2);
springs[0] = new Spring(240, 260, 40, 0.98, 8.0, 0.1, springs, 0);
springs[1] = new Spring(320, 210, 120, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring(180, 170, 200, 0.90, 9.9, 0.1, springs, 2);
}
void draw()
@@ -104,7 +104,7 @@ class Spring
tempxpos = tempxpos + velx; // Updated position
if ((over() || move) && !otherOver() ) {
if ((overEvent() || move) && !otherOver() ) {
over = true;
} else {
over = false;
@@ -112,7 +112,7 @@ class Spring
}
// Test to see if mouse is over this spring
boolean over() {
boolean overEvent() {
float disX = tempxpos - mouseX;
float disY = tempypos - mouseY;
if (sqrt(sq(disX) + sq(disY)) < size/2 ) {