Skip to content

Commit 529fc06

Browse files
authored
Merge pull request #429 from 17bcs029/example
add composite Objects example
2 parents 90d4660 + 686b5ae commit 529fc06

File tree

3 files changed

+297
-0
lines changed

3 files changed

+297
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* @name Composite Objects
2+
* @description An object can include several other objects.
3+
* Creating such composite objects is a good way to use the principles
4+
* of modularity and build higher levels of abstraction within a program.
5+
*/
6+
let er1, er2;
7+
8+
function setup() {
9+
createCanvas(640, 360);
10+
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
11+
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
er1.transmit();
17+
er2.transmit();
18+
}
19+
20+
class Egg {
21+
constructor(xpos, ypos, t, s) {
22+
this.x = xpos;
23+
this.y = ypos;
24+
this.tilt = t;
25+
this.scalar = s / 100.0;
26+
this.angle = 0.0;
27+
}
28+
29+
wobble() {
30+
this.tilt = cos(this.angle) / 8;
31+
this.angle += 0.1;
32+
}
33+
34+
display() {
35+
noStroke();
36+
fill(255);
37+
push();
38+
translate(this.x, this.y);
39+
rotate(this.tilt);
40+
scale(this.scalar);
41+
beginShape();
42+
vertex(0, -100);
43+
bezierVertex(25, -100, 40, -65, 40, -40);
44+
bezierVertex(40, -15, 25, 0, 0, 0);
45+
bezierVertex(-25, 0, -40, -15, -40, -40);
46+
bezierVertex(-40, -65, -25, -100, 0, -100);
47+
endShape();
48+
pop();
49+
}
50+
}
51+
52+
class Ring {
53+
start(xpos, ypos) {
54+
this.x = xpos;
55+
this.y = ypos;
56+
this.on = true;
57+
this.diameter = 1;
58+
}
59+
60+
grow() {
61+
if (this.on == true) {
62+
this.diameter += 0.5;
63+
if (this.diameter > width*2) {
64+
this.diameter = 0.0;
65+
}
66+
}
67+
}
68+
69+
display() {
70+
if (this.on == true) {
71+
noFill();
72+
strokeWeight(4);
73+
stroke(155, 153);
74+
ellipse(this.x, this.y, this.diameter, this.diameter);
75+
}
76+
}
77+
}
78+
79+
class EggRing {
80+
constructor(x, y, t, sp) {
81+
this.x = x;
82+
this.y = y;
83+
this.t = t;
84+
this.sp = sp;
85+
this.circle = new Ring();
86+
this.ovoid = new Egg(this.x, this.y, this.t, this.sp);
87+
this.circle.start(this.x, this.y - this.sp/2);
88+
}
89+
90+
transmit() {
91+
this.ovoid.wobble();
92+
this.ovoid.display();
93+
this.circle.grow();
94+
this.circle.display();
95+
if (circle.on == false) {
96+
circle.on = true;
97+
}
98+
}
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* @name Composite Objects
2+
* @description An object can include several other objects.
3+
* Creating such composite objects is a good way to use the principles
4+
* of modularity and build higher levels of abstraction within a program.
5+
*/
6+
let er1, er2;
7+
8+
function setup() {
9+
createCanvas(640, 360);
10+
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
11+
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
er1.transmit();
17+
er2.transmit();
18+
}
19+
20+
class Egg {
21+
constructor(xpos, ypos, t, s) {
22+
this.x = xpos;
23+
this.y = ypos;
24+
this.tilt = t;
25+
this.scalar = s / 100.0;
26+
this.angle = 0.0;
27+
}
28+
29+
wobble() {
30+
this.tilt = cos(this.angle) / 8;
31+
this.angle += 0.1;
32+
}
33+
34+
display() {
35+
noStroke();
36+
fill(255);
37+
push();
38+
translate(this.x, this.y);
39+
rotate(this.tilt);
40+
scale(this.scalar);
41+
beginShape();
42+
vertex(0, -100);
43+
bezierVertex(25, -100, 40, -65, 40, -40);
44+
bezierVertex(40, -15, 25, 0, 0, 0);
45+
bezierVertex(-25, 0, -40, -15, -40, -40);
46+
bezierVertex(-40, -65, -25, -100, 0, -100);
47+
endShape();
48+
pop();
49+
}
50+
}
51+
52+
class Ring {
53+
start(xpos, ypos) {
54+
this.x = xpos;
55+
this.y = ypos;
56+
this.on = true;
57+
this.diameter = 1;
58+
}
59+
60+
grow() {
61+
if (this.on == true) {
62+
this.diameter += 0.5;
63+
if (this.diameter > width*2) {
64+
this.diameter = 0.0;
65+
}
66+
}
67+
}
68+
69+
display() {
70+
if (this.on == true) {
71+
noFill();
72+
strokeWeight(4);
73+
stroke(155, 153);
74+
ellipse(this.x, this.y, this.diameter, this.diameter);
75+
}
76+
}
77+
}
78+
79+
class EggRing {
80+
constructor(x, y, t, sp) {
81+
this.x = x;
82+
this.y = y;
83+
this.t = t;
84+
this.sp = sp;
85+
this.circle = new Ring();
86+
this.ovoid = new Egg(this.x, this.y, this.t, this.sp);
87+
this.circle.start(this.x, this.y - this.sp/2);
88+
}
89+
90+
transmit() {
91+
this.ovoid.wobble();
92+
this.ovoid.display();
93+
this.circle.grow();
94+
this.circle.display();
95+
if (this.circle.on == false) {
96+
this.circle.on = true;
97+
}
98+
}
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* @name Composite Objects
2+
* @description An object can include several other objects.
3+
* Creating such composite objects is a good way to use the principles
4+
* of modularity and build higher levels of abstraction within a program.
5+
*/
6+
let er1, er2;
7+
8+
function setup() {
9+
createCanvas(640, 360);
10+
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
11+
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
er1.transmit();
17+
er2.transmit();
18+
}
19+
20+
class Egg {
21+
constructor(xpos, ypos, t, s) {
22+
this.x = xpos;
23+
this.y = ypos;
24+
this.tilt = t;
25+
this.scalar = s / 100.0;
26+
this.angle = 0.0;
27+
}
28+
29+
wobble() {
30+
this.tilt = cos(this.angle) / 8;
31+
this.angle += 0.1;
32+
}
33+
34+
display() {
35+
noStroke();
36+
fill(255);
37+
push();
38+
translate(this.x, this.y);
39+
rotate(this.tilt);
40+
scale(this.scalar);
41+
beginShape();
42+
vertex(0, -100);
43+
bezierVertex(25, -100, 40, -65, 40, -40);
44+
bezierVertex(40, -15, 25, 0, 0, 0);
45+
bezierVertex(-25, 0, -40, -15, -40, -40);
46+
bezierVertex(-40, -65, -25, -100, 0, -100);
47+
endShape();
48+
pop();
49+
}
50+
}
51+
52+
class Ring {
53+
start(xpos, ypos) {
54+
this.x = xpos;
55+
this.y = ypos;
56+
this.on = true;
57+
this.diameter = 1;
58+
}
59+
60+
grow() {
61+
if (this.on == true) {
62+
this.diameter += 0.5;
63+
if (this.diameter > width*2) {
64+
this.diameter = 0.0;
65+
}
66+
}
67+
}
68+
69+
display() {
70+
if (this.on == true) {
71+
noFill();
72+
strokeWeight(4);
73+
stroke(155, 153);
74+
ellipse(this.x, this.y, this.diameter, this.diameter);
75+
}
76+
}
77+
}
78+
79+
class EggRing {
80+
constructor(x, y, t, sp) {
81+
this.x = x;
82+
this.y = y;
83+
this.t = t;
84+
this.sp = sp;
85+
this.circle = new Ring();
86+
this.ovoid = new Egg(this.x, this.y, this.t, this.sp);
87+
this.circle.start(this.x, this.y - this.sp/2);
88+
}
89+
90+
transmit() {
91+
this.ovoid.wobble();
92+
this.ovoid.display();
93+
this.circle.grow();
94+
this.circle.display();
95+
if (this.circle.on == false) {
96+
this.circle.on = true;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)