Skip to content

Commit c8f645c

Browse files
authored
Merge pull request #452 from 17bcs029/constructor
converted the koch curve example to es6 constructor syntax
2 parents 529fc06 + 2a7dbe3 commit c8f645c

File tree

3 files changed

+84
-73
lines changed

3 files changed

+84
-73
lines changed

src/data/examples/en/09_Simulate/18_Koch.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,34 @@ function draw() {
2727
// A class to describe one line segment in the fractal
2828
// Includes methods to calculate midp5.Vectors along the line according to the Koch algorithm
2929

30-
function KochLine(a,b) {
31-
// Two p5.Vectors,
32-
// start is the "left" p5.Vector and
33-
// end is the "right p5.Vector
34-
this.start = a.copy();
35-
this.end = b.copy();
36-
37-
this.display = function() {
30+
class KochLine {
31+
constructor(a,b) {
32+
// Two p5.Vectors,
33+
// start is the "left" p5.Vector and
34+
// end is the "right p5.Vector
35+
this.start = a.copy();
36+
this.end = b.copy();
37+
}
38+
39+
display() {
3840
stroke(255);
3941
line(this.start.x, this.start.y, this.end.x, this.end.y);
4042
}
4143

42-
this.kochA = function() {
44+
kochA() {
4345
return this.start.copy();
4446
}
4547

4648
// This is easy, just 1/3 of the way
47-
this.kochB = function() {
49+
kochB() {
4850
let v = p5.Vector.sub(this.end, this.start);
4951
v.div(3);
5052
v.add(this.start);
5153
return v;
5254
}
5355

5456
// More complicated, have to use a little trig to figure out where this p5.Vector is!
55-
this.kochC = function() {
57+
kochC() {
5658
let a = this.start.copy(); // Start at the beginning
5759
let v = p5.Vector.sub(this.end, this.start);
5860
v.div(3);
@@ -63,46 +65,48 @@ function KochLine(a,b) {
6365
}
6466

6567
// Easy, just 2/3 of the way
66-
this.kochD = function() {
68+
kochD() {
6769
let v = p5.Vector.sub(this.end, this.start);
6870
v.mult(2/3.0);
6971
v.add(this.start);
7072
return v;
7173
}
7274

73-
this.kochE = function() {
75+
kochE() {
7476
return this.end.copy();
7577
}
7678
}
7779

7880
// A class to manage the list of line segments in the snowflake pattern
7981

80-
function KochFractal() {
81-
this.start = createVector(0,height-20); // A p5.Vector for the start
82-
this.end = createVector(width,height-20); // A p5.Vector for the end
83-
this.lines = []; // An array to keep track of all the lines
84-
this.count = 0;
82+
class KochFractal {
83+
constructor() {
84+
this.start = createVector(0,height-20); // A p5.Vector for the start
85+
this.end = createVector(width,height-20); // A p5.Vector for the end
86+
this.lines = []; // An array to keep track of all the lines
87+
this.count = 0;
88+
this.restart();
89+
}
8590

86-
this.nextLevel = function() {
91+
nextLevel() {
8792
// For every line that is in the arraylist
8893
// create 4 more lines in a new arraylist
8994
this.lines = this.iterate(this.lines);
9095
this.count++;
9196
}
9297

93-
this.restart = function() {
98+
restart() {
9499
this.count = 0; // Reset count
95100
this.lines = []; // Empty the array list
96101
this.lines.push(new KochLine(this.start,this.end)); // Add the initial line (from one end p5.Vector to the other)
97102
}
98-
this.restart();
99103

100-
this.getCount = function() {
104+
getCount() {
101105
return this.count;
102106
}
103107

104108
// This is easy, just draw all the lines
105-
this.render = function() {
109+
render() {
106110
for(let i = 0; i < this.lines.length; i++) {
107111
this.lines[i].display();
108112
}
@@ -116,7 +120,7 @@ function KochFractal() {
116120
// Step 3: Return the new arraylist and it becomes the list of line segments for the structure
117121

118122
// As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . .
119-
this.iterate = function(before) {
123+
iterate(before) {
120124
let now = []; // Create emtpy list
121125
for(let i = 0; i < this.lines.length; i++) {
122126
let l = this.lines[i];

src/data/examples/es/09_Simulate/18_Koch.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,34 @@ function draw() {
2727
// A class to describe one line segment in the fractal
2828
// Includes methods to calculate midp5.Vectors along the line according to the Koch algorithm
2929

30-
function KochLine(a,b) {
31-
// Two p5.Vectors,
32-
// start is the "left" p5.Vector and
33-
// end is the "right p5.Vector
34-
this.start = a.copy();
35-
this.end = b.copy();
36-
37-
this.display = function() {
30+
class KochLine {
31+
constructor(a,b) {
32+
// Two p5.Vectors,
33+
// start is the "left" p5.Vector and
34+
// end is the "right p5.Vector
35+
this.start = a.copy();
36+
this.end = b.copy();
37+
}
38+
39+
display() {
3840
stroke(255);
3941
line(this.start.x, this.start.y, this.end.x, this.end.y);
4042
}
4143

42-
this.kochA = function() {
44+
kochA() {
4345
return this.start.copy();
4446
}
4547

4648
// This is easy, just 1/3 of the way
47-
this.kochB = function() {
49+
kochB() {
4850
let v = p5.Vector.sub(this.end, this.start);
4951
v.div(3);
5052
v.add(this.start);
5153
return v;
5254
}
5355

5456
// More complicated, have to use a little trig to figure out where this p5.Vector is!
55-
this.kochC = function() {
57+
kochC() {
5658
let a = this.start.copy(); // Start at the beginning
5759
let v = p5.Vector.sub(this.end, this.start);
5860
v.div(3);
@@ -63,46 +65,48 @@ function KochLine(a,b) {
6365
}
6466

6567
// Easy, just 2/3 of the way
66-
this.kochD = function() {
68+
kochD() {
6769
let v = p5.Vector.sub(this.end, this.start);
6870
v.mult(2/3.0);
6971
v.add(this.start);
7072
return v;
7173
}
7274

73-
this.kochE = function() {
75+
kochE() {
7476
return this.end.copy();
7577
}
7678
}
7779

7880
// A class to manage the list of line segments in the snowflake pattern
7981

80-
function KochFractal() {
81-
this.start = createVector(0,height-20); // A p5.Vector for the start
82-
this.end = createVector(width,height-20); // A p5.Vector for the end
83-
this.lines = []; // An array to keep track of all the lines
84-
this.count = 0;
82+
class KochFractal {
83+
constructor() {
84+
this.start = createVector(0,height-20); // A p5.Vector for the start
85+
this.end = createVector(width,height-20); // A p5.Vector for the end
86+
this.lines = []; // An array to keep track of all the lines
87+
this.count = 0;
88+
this.restart();
89+
}
8590

86-
this.nextLevel = function() {
91+
nextLevel() {
8792
// For every line that is in the arraylist
8893
// create 4 more lines in a new arraylist
8994
this.lines = this.iterate(this.lines);
9095
this.count++;
9196
}
9297

93-
this.restart = function() {
98+
restart() {
9499
this.count = 0; // Reset count
95100
this.lines = []; // Empty the array list
96101
this.lines.push(new KochLine(this.start,this.end)); // Add the initial line (from one end p5.Vector to the other)
97102
}
98-
this.restart();
99103

100-
this.getCount = function() {
104+
getCount() {
101105
return this.count;
102106
}
103107

104108
// This is easy, just draw all the lines
105-
this.render = function() {
109+
render() {
106110
for(let i = 0; i < this.lines.length; i++) {
107111
this.lines[i].display();
108112
}
@@ -116,7 +120,7 @@ function KochFractal() {
116120
// Step 3: Return the new arraylist and it becomes the list of line segments for the structure
117121

118122
// As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . .
119-
this.iterate = function(before) {
123+
iterate(before) {
120124
let now = []; // Create emtpy list
121125
for(let i = 0; i < this.lines.length; i++) {
122126
let l = this.lines[i];

src/data/examples/zh-Hans/09_Simulate/18_Koch.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,34 @@ function draw() {
2727
// A class to describe one line segment in the fractal
2828
// Includes methods to calculate midp5.Vectors along the line according to the Koch algorithm
2929

30-
function KochLine(a,b) {
31-
// Two p5.Vectors,
32-
// start is the "left" p5.Vector and
33-
// end is the "right p5.Vector
34-
this.start = a.copy();
35-
this.end = b.copy();
36-
37-
this.display = function() {
30+
class KochLine {
31+
constructor(a,b) {
32+
// Two p5.Vectors,
33+
// start is the "left" p5.Vector and
34+
// end is the "right p5.Vector
35+
this.start = a.copy();
36+
this.end = b.copy();
37+
}
38+
39+
display() {
3840
stroke(255);
3941
line(this.start.x, this.start.y, this.end.x, this.end.y);
4042
}
4143

42-
this.kochA = function() {
44+
kochA() {
4345
return this.start.copy();
4446
}
4547

4648
// This is easy, just 1/3 of the way
47-
this.kochB = function() {
49+
kochB() {
4850
let v = p5.Vector.sub(this.end, this.start);
4951
v.div(3);
5052
v.add(this.start);
5153
return v;
5254
}
5355

5456
// More complicated, have to use a little trig to figure out where this p5.Vector is!
55-
this.kochC = function() {
57+
kochC() {
5658
let a = this.start.copy(); // Start at the beginning
5759
let v = p5.Vector.sub(this.end, this.start);
5860
v.div(3);
@@ -63,46 +65,47 @@ function KochLine(a,b) {
6365
}
6466

6567
// Easy, just 2/3 of the way
66-
this.kochD = function() {
68+
kochD() {
6769
let v = p5.Vector.sub(this.end, this.start);
6870
v.mult(2/3.0);
6971
v.add(this.start);
7072
return v;
7173
}
7274

73-
this.kochE = function() {
75+
kochE() {
7476
return this.end.copy();
7577
}
7678
}
7779

7880
// A class to manage the list of line segments in the snowflake pattern
7981

80-
function KochFractal() {
81-
this.start = createVector(0,height-20); // A p5.Vector for the start
82-
this.end = createVector(width,height-20); // A p5.Vector for the end
83-
this.lines = []; // An array to keep track of all the lines
84-
this.count = 0;
85-
86-
this.nextLevel = function() {
82+
class KochFractal {
83+
constructor() {
84+
this.start = createVector(0,height-20); // A p5.Vector for the start
85+
this.end = createVector(width,height-20); // A p5.Vector for the end
86+
this.lines = []; // An array to keep track of all the lines
87+
this.count = 0;
88+
this.restart();
89+
}
90+
nextLevel() {
8791
// For every line that is in the arraylist
8892
// create 4 more lines in a new arraylist
8993
this.lines = this.iterate(this.lines);
9094
this.count++;
9195
}
9296

93-
this.restart = function() {
97+
restart() {
9498
this.count = 0; // Reset count
9599
this.lines = []; // Empty the array list
96100
this.lines.push(new KochLine(this.start,this.end)); // Add the initial line (from one end p5.Vector to the other)
97101
}
98-
this.restart();
99102

100-
this.getCount = function() {
103+
getCount() {
101104
return this.count;
102105
}
103106

104107
// This is easy, just draw all the lines
105-
this.render = function() {
108+
render() {
106109
for(let i = 0; i < this.lines.length; i++) {
107110
this.lines[i].display();
108111
}
@@ -116,7 +119,7 @@ function KochFractal() {
116119
// Step 3: Return the new arraylist and it becomes the list of line segments for the structure
117120

118121
// As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . .
119-
this.iterate = function(before) {
122+
iterate(before) {
120123
let now = []; // Create emtpy list
121124
for(let i = 0; i < this.lines.length; i++) {
122125
let l = this.lines[i];

0 commit comments

Comments
 (0)