Initial Commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Composite Objects
|
||||
*
|
||||
* An object can include several other objects. Creating such composite objects
|
||||
* is a good way to use the principles of modularity and build higher levels of
|
||||
* abstraction within a program.
|
||||
*/
|
||||
|
||||
EggRing er1, er2;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
|
||||
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
er1.transmit();
|
||||
er2.transmit();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
class Egg {
|
||||
float x, y; // X-coordinate, y-coordinate
|
||||
float tilt; // Left and right angle offset
|
||||
float angle; // Used to define the tilt
|
||||
float scalar; // Height of the egg
|
||||
|
||||
// Constructor
|
||||
Egg(float xpos, float ypos, float t, float s) {
|
||||
x = xpos;
|
||||
y = ypos;
|
||||
tilt = t;
|
||||
scalar = s / 100.0;
|
||||
}
|
||||
|
||||
void wobble() {
|
||||
tilt = cos(angle) / 8;
|
||||
angle += 0.1;
|
||||
}
|
||||
|
||||
void display() {
|
||||
noStroke();
|
||||
fill(255);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(tilt);
|
||||
scale(scalar);
|
||||
beginShape();
|
||||
vertex(0, -100);
|
||||
bezierVertex(25, -100, 40, -65, 40, -40);
|
||||
bezierVertex(40, -15, 25, 0, 0, 0);
|
||||
bezierVertex(-25, 0, -40, -15, -40, -40);
|
||||
bezierVertex(-40, -65, -25, -100, 0, -100);
|
||||
endShape();
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class EggRing {
|
||||
Egg ovoid;
|
||||
Ring circle = new Ring();
|
||||
|
||||
EggRing(float x, float y, float t, float sp) {
|
||||
ovoid = new Egg(x, y, t, sp);
|
||||
circle.start(x, y - sp/2);
|
||||
}
|
||||
|
||||
void transmit() {
|
||||
ovoid.wobble();
|
||||
ovoid.display();
|
||||
circle.grow();
|
||||
circle.display();
|
||||
if (circle.on == false) {
|
||||
circle.on = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
class Ring {
|
||||
|
||||
float x, y; // X-coordinate, y-coordinate
|
||||
float diameter; // Diameter of the ring
|
||||
boolean on = false; // Turns the display on and off
|
||||
|
||||
void start(float xpos, float ypos) {
|
||||
x = xpos;
|
||||
y = ypos;
|
||||
on = true;
|
||||
diameter = 1;
|
||||
}
|
||||
|
||||
void grow() {
|
||||
if (on == true) {
|
||||
diameter += 0.5;
|
||||
if (diameter > width*2) {
|
||||
diameter = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
if (on == true) {
|
||||
noFill();
|
||||
strokeWeight(4);
|
||||
stroke(155, 153);
|
||||
ellipse(x, y, diameter, diameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Inheritance
|
||||
*
|
||||
* A class can be defined using another class as a foundation. In object-oriented
|
||||
* programming terminology, one class can inherit fi elds and methods from another.
|
||||
* An object that inherits from another is called a subclass, and the object it
|
||||
* inherits from is called a superclass. A subclass extends the superclass.
|
||||
*/
|
||||
|
||||
SpinSpots spots;
|
||||
SpinArm arm;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
arm = new SpinArm(width/2, height/2, 0.01);
|
||||
spots = new SpinSpots(width/2, height/2, -0.02, 90.0);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(204);
|
||||
arm.update();
|
||||
arm.display();
|
||||
spots.update();
|
||||
spots.display();
|
||||
}
|
||||
|
||||
class Spin {
|
||||
float x, y, speed;
|
||||
float angle = 0.0;
|
||||
Spin(float xpos, float ypos, float s) {
|
||||
x = xpos;
|
||||
y = ypos;
|
||||
speed = s;
|
||||
}
|
||||
void update() {
|
||||
angle += speed;
|
||||
}
|
||||
}
|
||||
|
||||
class SpinArm extends Spin {
|
||||
SpinArm(float x, float y, float s) {
|
||||
super(x, y, s);
|
||||
}
|
||||
void display() {
|
||||
strokeWeight(1);
|
||||
stroke(0);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
angle += speed;
|
||||
rotate(angle);
|
||||
line(0, 0, 165, 0);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
class SpinSpots extends Spin {
|
||||
float dim;
|
||||
SpinSpots(float x, float y, float s, float d) {
|
||||
super(x, y, s);
|
||||
dim = d;
|
||||
}
|
||||
void display() {
|
||||
noStroke();
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
angle += speed;
|
||||
rotate(angle);
|
||||
ellipse(-dim/2, 0, dim, dim);
|
||||
ellipse(dim/2, 0, dim, dim);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Multiple constructors
|
||||
*
|
||||
* A class can have multiple constructors that assign the fields in different ways.
|
||||
* Sometimes it's beneficial to specify every aspect of an object's data by assigning
|
||||
* parameters to the fields, but other times it might be appropriate to define only
|
||||
* one or a few.
|
||||
*/
|
||||
|
||||
Spot sp1, sp2;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
background(204);
|
||||
noLoop();
|
||||
// Run the constructor without parameters
|
||||
sp1 = new Spot();
|
||||
// Run the constructor with three parameters
|
||||
sp2 = new Spot(width*0.5, height*0.5, 120);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
sp1.display();
|
||||
sp2.display();
|
||||
}
|
||||
|
||||
class Spot {
|
||||
float x, y, radius;
|
||||
|
||||
// First version of the Spot constructor;
|
||||
// the fields are assigned default values
|
||||
Spot() {
|
||||
radius = 40;
|
||||
x = width*0.25;
|
||||
y = height*0.5;
|
||||
}
|
||||
|
||||
// Second version of the Spot constructor;
|
||||
// the fields are assigned with parameters
|
||||
Spot(float xpos, float ypos, float r) {
|
||||
x = xpos;
|
||||
y = ypos;
|
||||
radius = r;
|
||||
}
|
||||
void display() {
|
||||
ellipse(x, y, radius*2, radius*2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Objects
|
||||
* by hbarragan.
|
||||
*
|
||||
* Move the cursor across the image to change the speed and positions
|
||||
* of the geometry. The class MRect defines a group of lines.
|
||||
*/
|
||||
|
||||
MRect r1, r2, r3, r4;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 360);
|
||||
fill(255, 204);
|
||||
noStroke();
|
||||
r1 = new MRect(1, 134.0, 0.532, 0.1*height, 10.0, 60.0);
|
||||
r2 = new MRect(2, 44.0, 0.166, 0.3*height, 5.0, 50.0);
|
||||
r3 = new MRect(2, 58.0, 0.332, 0.4*height, 10.0, 35.0);
|
||||
r4 = new MRect(1, 120.0, 0.0498, 0.9*height, 15.0, 60.0);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0);
|
||||
|
||||
r1.display();
|
||||
r2.display();
|
||||
r3.display();
|
||||
r4.display();
|
||||
|
||||
r1.move(mouseX-(width/2), mouseY+(height*0.1), 30);
|
||||
r2.move((mouseX+(width*0.05))%width, mouseY+(height*0.025), 20);
|
||||
r3.move(mouseX/4, mouseY-(height*0.025), 40);
|
||||
r4.move(mouseX-(width/2), (height-mouseY), 50);
|
||||
}
|
||||
|
||||
class MRect
|
||||
{
|
||||
int w; // single bar width
|
||||
float xpos; // rect xposition
|
||||
float h; // rect height
|
||||
float ypos ; // rect yposition
|
||||
float d; // single bar distance
|
||||
float t; // number of bars
|
||||
|
||||
MRect(int iw, float ixp, float ih, float iyp, float id, float it) {
|
||||
w = iw;
|
||||
xpos = ixp;
|
||||
h = ih;
|
||||
ypos = iyp;
|
||||
d = id;
|
||||
t = it;
|
||||
}
|
||||
|
||||
void move (float posX, float posY, float damping) {
|
||||
float dif = ypos - posY;
|
||||
if (abs(dif) > 1) {
|
||||
ypos -= dif/damping;
|
||||
}
|
||||
dif = xpos - posX;
|
||||
if (abs(dif) > 1) {
|
||||
xpos -= dif/damping;
|
||||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
for (int i=0; i<t; i++) {
|
||||
rect(xpos+(i*(d+w)), ypos, w, height*h);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user