Skip to content

Commit fc562ca

Browse files
committed
#154 porting basics objects
1 parent 58ee711 commit fc562ca

7 files changed

Lines changed: 138 additions & 156 deletions

File tree

content/examples_p5/Basics/Objects/CompositeObjects/CompositeObjects.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
* abstraction within a program.
77
*/
88

9-
EggRing er1, er2;
9+
var er1, er2;
1010

11-
12-
void setup() {
13-
size(640, 360);
11+
function setup() {
12+
createCanvas(640, 360);
1413
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
1514
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
1615
}
1716

1817

19-
void draw() {
18+
function draw() {
2019
background(0);
2120
er1.transmit();
2221
er2.transmit();
Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
1-
class Egg {
2-
float x, y; // X-coordinate, y-coordinate
3-
float tilt; // Left and right angle offset
4-
float angle; // Used to define the tilt
5-
float scalar; // Height of the egg
1+
// Constructor
2+
function Egg(xpos, ypos, t, s) {
3+
this.x = xpos;
4+
this.y = ypos;
5+
this.tilt = t;
6+
this.scalar = s / 100.0;
7+
this.angle = 0;
68

7-
// Constructor
8-
Egg(float xpos, float ypos, float t, float s) {
9-
x = xpos;
10-
y = ypos;
11-
tilt = t;
12-
scalar = s / 100.0;
9+
this.wobble = function() {
10+
this.tilt = cos(this.angle) / 8;
11+
this.angle += 0.1;
1312
}
1413

15-
void wobble() {
16-
tilt = cos(angle) / 8;
17-
angle += 0.1;
18-
}
19-
20-
void display() {
14+
this.display = function() {
2115
noStroke();
2216
fill(255);
23-
pushMatrix();
24-
translate(x, y);
25-
rotate(tilt);
26-
scale(scalar);
17+
push();
18+
translate(this.x, this.y);
19+
rotate(this.tilt);
20+
scale(this.scalar);
2721
beginShape();
2822
vertex(0, -100);
2923
bezierVertex(25, -100, 40, -65, 40, -40);
3024
bezierVertex(40, -15, 25, 0, 0, 0);
3125
bezierVertex(-25, 0, -40, -15, -40, -40);
3226
bezierVertex(-40, -65, -25, -100, 0, -100);
3327
endShape();
34-
popMatrix();
28+
pop();
3529
}
3630
}
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
class EggRing {
2-
Egg ovoid;
3-
Ring circle = new Ring();
41

5-
EggRing(float x, float y, float t, float sp) {
6-
ovoid = new Egg(x, y, t, sp);
7-
circle.start(x, y - sp/2);
8-
}
9-
10-
void transmit() {
11-
ovoid.wobble();
12-
ovoid.display();
13-
circle.grow();
14-
circle.display();
15-
if (circle.on == false) {
16-
circle.on = true;
2+
function EggRing(x, y, t, sp) {
3+
this.ovoid = new Egg(x, y, t, sp);
4+
this.circle = new Ring();
5+
this.circle.start(x, y - sp/2);
6+
7+
this.transmit = function() {
8+
this.ovoid.wobble();
9+
this.ovoid.display();
10+
this.circle.grow();
11+
this.circle.display();
12+
if (this.circle.on == false) {
13+
this.circle.on = true;
1714
}
1815
}
1916
}
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
class Ring {
2-
3-
float x, y; // X-coordinate, y-coordinate
4-
float diameter; // Diameter of the ring
5-
boolean on = false; // Turns the display on and off
6-
7-
void start(float xpos, float ypos) {
8-
x = xpos;
9-
y = ypos;
10-
on = true;
11-
diameter = 1;
1+
function Ring() {
2+
3+
this.x = 0;
4+
this.y = 0;
5+
this.on = false;
6+
this.diameter = 0;
7+
8+
this.start = function(xpos, ypos) {
9+
this.x = xpos;
10+
this.y = ypos;
11+
this.on = true;
12+
this.diameter = 1;
1213
}
1314

14-
void grow() {
15-
if (on == true) {
16-
diameter += 0.5;
17-
if (diameter > width*2) {
18-
diameter = 0.0;
15+
this.grow = function() {
16+
if (this.on == true) {
17+
this.diameter += 0.5;
18+
if (this.diameter > width*2) {
19+
this.diameter = 0.0;
1920
}
2021
}
2122
}
2223

23-
void display() {
24-
if (on == true) {
24+
this.display = function() {
25+
if (this.on == true) {
2526
noFill();
2627
strokeWeight(4);
2728
stroke(155, 153);
28-
ellipse(x, y, diameter, diameter);
29+
ellipse(this.x, this.y, this.diameter, this.diameter);
2930
}
3031
}
3132
}

content/examples_p5/Basics/Objects/Inheritance/Inheritance.js

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,74 @@
77
* inherits from is called a superclass. A subclass extends the superclass.
88
*/
99

10-
SpinSpots spots;
11-
SpinArm arm;
10+
var spots;
11+
var arm;
1212

13-
void setup() {
14-
size(640, 360);
13+
function setup() {
14+
createCanvas(640, 360);
1515
arm = new SpinArm(width/2, height/2, 0.01);
1616
spots = new SpinSpots(width/2, height/2, -0.02, 90.0);
1717
}
1818

19-
void draw() {
19+
function draw() {
2020
background(204);
2121
arm.update();
2222
arm.display();
2323
spots.update();
2424
spots.display();
2525
}
2626

27-
class Spin {
28-
float x, y, speed;
29-
float angle = 0.0;
30-
Spin(float xpos, float ypos, float s) {
31-
x = xpos;
32-
y = ypos;
33-
speed = s;
34-
}
35-
void update() {
36-
angle += speed;
27+
28+
function Spin(xpos, ypos, s) {
29+
this.x = xpos;
30+
this.y = ypos;
31+
this.speed = s;
32+
this.angle = 0;
33+
34+
this.update = function() {
35+
this.angle += this.speed;
3736
}
3837
}
3938

40-
class SpinArm extends Spin {
41-
SpinArm(float x, float y, float s) {
42-
super(x, y, s);
43-
}
44-
void display() {
39+
// Child class constructor
40+
function SpinArm(x, y, s) {
41+
Spin.call(this, x, y, s);
42+
43+
// Override the display method
44+
this.display = function(){
4545
strokeWeight(1);
4646
stroke(0);
47-
pushMatrix();
48-
translate(x, y);
49-
angle += speed;
50-
rotate(angle);
47+
push();
48+
translate(this.x, this.y);
49+
this.angle += this.speed;
50+
rotate(this.angle);
5151
line(0, 0, 165, 0);
52-
popMatrix();
53-
}
54-
}
52+
pop();
53+
};
54+
};
5555

56-
class SpinSpots extends Spin {
57-
float dim;
58-
SpinSpots(float x, float y, float s, float d) {
59-
super(x, y, s);
60-
dim = d;
61-
}
62-
void display() {
56+
// Inherit from the parent class
57+
SpinArm.prototype = Object.create(Spin.prototype);
58+
this.constructor = SpinArm;
59+
60+
// Child class constructor
61+
function SpinSpots(x, y, s, d) {
62+
this.dim = d;
63+
Spin.call(this, x, y, s);
64+
65+
// Override the display method
66+
this.display = function(){
6367
noStroke();
64-
pushMatrix();
65-
translate(x, y);
66-
angle += speed;
67-
rotate(angle);
68-
ellipse(-dim/2, 0, dim, dim);
69-
ellipse(dim/2, 0, dim, dim);
70-
popMatrix();
71-
}
72-
}
68+
push();
69+
translate(this.x, this.y);
70+
this.angle += this.speed;
71+
rotate(this.angle);
72+
ellipse(-this.dim/2, 0, this.dim, this.dim);
73+
ellipse(this.dim/2, 0, this.dim, this.dim);
74+
pop();
75+
};
76+
};
77+
78+
// Inherit from the parent class
79+
SpinSpots.prototype = Object.create(Spin.prototype);
80+
this.constructor = SpinSpots;

content/examples_p5/Basics/Objects/MultipleConstructors/MultipleConstructors.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* one or a few.
88
*/
99

10-
Spot sp1, sp2;
10+
var sp1, sp2;
1111

12-
void setup() {
13-
size(640, 360);
12+
function setup() {
13+
createCanvas(640, 360);
1414
background(204);
1515
noLoop();
1616
// Run the constructor without parameters
@@ -19,31 +19,22 @@ void setup() {
1919
sp2 = new Spot(width*0.5, height*0.5, 120);
2020
}
2121

22-
void draw() {
22+
function draw() {
2323
sp1.display();
2424
sp2.display();
25-
}
25+
}
2626

27-
class Spot {
28-
float x, y, radius;
29-
30-
// First version of the Spot constructor;
31-
// the fields are assigned default values
32-
Spot() {
33-
radius = 40;
34-
x = width*0.25;
35-
y = height*0.5;
36-
}
27+
// First version of the Spot constructor;
28+
// the fields are assigned default values
29+
// Second version of the Spot constructor;
30+
// the fields are assigned with parameters
31+
function Spot(xpos, ypos, r) {
32+
this.radius = r || 40;
33+
this.x = xpos || width*0.25;
34+
this.y = ypos || height*0.5;
3735

38-
// Second version of the Spot constructor;
39-
// the fields are assigned with parameters
40-
Spot(float xpos, float ypos, float r) {
41-
x = xpos;
42-
y = ypos;
43-
radius = r;
44-
}
45-
void display() {
46-
ellipse(x, y, radius*2, radius*2);
36+
this.display = function() {
37+
ellipse(this.x, this.y, this.radius*2, this.radius*2);
4738
}
4839

4940
}

0 commit comments

Comments
 (0)