diff --git a/chp00_introduction/NOC_I_01_RandomWalkTraditional/sketch.js b/chp00_introduction/NOC_I_01_RandomWalkTraditional/sketch.js index 591d18a..0ade478 100644 --- a/chp00_introduction/NOC_I_01_RandomWalkTraditional/sketch.js +++ b/chp00_introduction/NOC_I_01_RandomWalkTraditional/sketch.js @@ -27,7 +27,7 @@ class Walker { } step() { - var choice = floor(random(4)); + let choice = floor(random(4)); if (choice === 0) { this.x++; } else if (choice == 1) { diff --git a/chp01_vectors/NOC_1_10_motion101_acceleration/mover.js b/chp01_vectors/NOC_1_10_motion101_acceleration/mover.js index 4012070..e0dde1c 100644 --- a/chp01_vectors/NOC_1_10_motion101_acceleration/mover.js +++ b/chp01_vectors/NOC_1_10_motion101_acceleration/mover.js @@ -14,7 +14,7 @@ class Mover{ update() { // Compute a vector that points from position to mouse - var mouse = createVector(mouseX,mouseY); + let mouse = createVector(mouseX,mouseY); this.acceleration = p5.Vector.sub(mouse,this.position); // Set magnitude of acceleration this.acceleration.setMag(0.2); diff --git a/chp01_vectors/NOC_1_11_motion101_acceleration_array/mover.js b/chp01_vectors/NOC_1_11_motion101_acceleration_array/mover.js index d6c105f..ab0a1e6 100644 --- a/chp01_vectors/NOC_1_11_motion101_acceleration_array/mover.js +++ b/chp01_vectors/NOC_1_11_motion101_acceleration_array/mover.js @@ -14,7 +14,7 @@ class Mover{ update() { // Compute a vector that points from position to mouse - var mouse = createVector(mouseX,mouseY); + let mouse = createVector(mouseX,mouseY); this.acceleration = p5.Vector.sub(mouse,this.position); // Set magnitude of acceleration this.acceleration.setMag(0.2); diff --git a/chp01_vectors/NOC_1_11_motion101_acceleration_array/sketch.js b/chp01_vectors/NOC_1_11_motion101_acceleration_array/sketch.js index 9699fc4..8cd9792 100644 --- a/chp01_vectors/NOC_1_11_motion101_acceleration_array/sketch.js +++ b/chp01_vectors/NOC_1_11_motion101_acceleration_array/sketch.js @@ -10,7 +10,7 @@ let movers = []; function setup() { createCanvas(640,360); - for (var i = 0; i < 20; i++) { + for (let i = 0; i < 20; i++) { movers[i] = new Mover(); } } diff --git a/chp02_forces/NOC_2_01_forces/mover.js b/chp02_forces/NOC_2_01_forces/mover.js index 885ea8a..789890f 100644 --- a/chp02_forces/NOC_2_01_forces/mover.js +++ b/chp02_forces/NOC_2_01_forces/mover.js @@ -11,7 +11,7 @@ class Mover { } applyForce(force) { - var f = p5.Vector.div(force, this.mass); + let f = p5.Vector.div(force, this.mass); this.acceleration.add(f); } diff --git a/chp04_systems/NOC_4_02_ArrayParticles/sketch.js b/chp04_systems/NOC_4_02_ArrayParticles/sketch.js index 0cb2c12..11cf66b 100644 --- a/chp04_systems/NOC_4_02_ArrayParticles/sketch.js +++ b/chp04_systems/NOC_4_02_ArrayParticles/sketch.js @@ -13,8 +13,8 @@ function draw() { particles.push(new Particle(createVector(width / 2, 50))); // Looping through backwards to delete - for (var i = particles.length - 1; i >= 0; i--) { - var p = particles[i]; + for (let i = particles.length - 1; i >= 0; i--) { + let p = particles[i]; p.run(); if (p.isDead()) { //remove the particle diff --git a/chp04_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/confetti.js b/chp04_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/confetti.js index a558fc2..f133380 100644 --- a/chp04_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/confetti.js +++ b/chp04_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/confetti.js @@ -13,7 +13,7 @@ class Confetti extends Particle { strokeWeight(2); push(); translate(this.position.x, this.position.y); - var theta = map(this.position.x, 0, width, 0, TWO_PI * 2); + let theta = map(this.position.x, 0, width, 0, TWO_PI * 2); rotate(theta); rect(0, 0, 12, 12); pop(); diff --git a/chp06_agents/NOC_6_01_Seek/vehicle.js b/chp06_agents/NOC_6_01_Seek/vehicle.js index 430a055..b5e4991 100644 --- a/chp06_agents/NOC_6_01_Seek/vehicle.js +++ b/chp06_agents/NOC_6_01_Seek/vehicle.js @@ -34,13 +34,13 @@ class Vehicle { // STEER = DESIRED MINUS VELOCITY seek(target) { - var desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target + let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target // Scale to maximum speed desired.setMag(this.maxspeed); // Steering = Desired minus velocity - var steer = p5.Vector.sub(desired, this.velocity); + let steer = p5.Vector.sub(desired, this.velocity); steer.limit(this.maxforce); // Limit to maximum steering force this.applyForce(steer); diff --git a/chp06_agents/NOC_6_02_Arrive/vehicle.js b/chp06_agents/NOC_6_02_Arrive/vehicle.js index 1176072..1a7409c 100644 --- a/chp06_agents/NOC_6_02_Arrive/vehicle.js +++ b/chp06_agents/NOC_6_02_Arrive/vehicle.js @@ -37,7 +37,7 @@ class Vehicle { let d = desired.mag(); // Scale with arbitrary damping within 100 pixels if (d < 100) { - var m = map(d, 0, 100, 0, this.maxspeed); + let m = map(d, 0, 100, 0, this.maxspeed); desired.setMag(m); } else { desired.setMag(this.maxspeed); diff --git a/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/CA.js b/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/CA.js index 779bcac..8790b8b 100644 --- a/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/CA.js +++ b/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/CA.js @@ -15,14 +15,14 @@ function CA(r) { // Make a random ruleset this.randomize = function() { - for (var i = 0; i < 8; i++) { + for (let i = 0; i < 8; i++) { this.ruleset[i] = Math.floor(random(2)); } }; // Reset to generation 0 this.restart = function() { - for (var i = 0; i < this.cells.length; i++) { + for (let i = 0; i < this.cells.length; i++) { this.cells[i] = 0; } // We arbitrarily start with just the middle cell having a state of "1" @@ -34,13 +34,13 @@ function CA(r) { // The process of creating the new generation this.generate = function() { // First we create an empty array for the new values - var nextgen = new Array(this.cells.length); + let nextgen = new Array(this.cells.length); // For every spot, determine new state by examing current state, and neighbor states // Ignore edges that only have one neighor - for (var i = 1; i < this.cells.length-1; i++) { - var left = this.cells[i-1]; // Left neighbor state - var me = this.cells[i]; // Current state - var right = this.cells[i+1]; // Right neighbor state + for (let i = 1; i < this.cells.length-1; i++) { + let left = this.cells[i-1]; // Left neighbor state + let me = this.cells[i]; // Current state + let right = this.cells[i+1]; // Right neighbor state nextgen[i] = this.rules(left, me, right); // Compute next generation state based on ruleset } // The current generation is the new generation @@ -50,7 +50,7 @@ function CA(r) { // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' this.display = function() { - for (var i = 0; i < this.cells.length; i++) { + for (let i = 0; i < this.cells.length; i++) { if (this.cells[i] == 1) fill(200); else fill(51); noStroke(); diff --git a/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/sketch.js b/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/sketch.js index d58708b..ad34112 100644 --- a/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/sketch.js +++ b/chp07_CA/Exercise_7_01_WolframCA_randomizedrules/sketch.js @@ -9,15 +9,15 @@ // Mouse click restarts as well // An object to describe a Wolfram elementary Cellular Automata -var ca; +let ca; -var delay = 0; +let delay = 0; function setup() { createCanvas(640, 360); background(51); // An initial rule system - var ruleset = [0, 1, 0, 1, 1, 0, 1, 0]; + let ruleset = [0, 1, 0, 1, 1, 0, 1, 0]; ca = new CA(ruleset); } diff --git a/chp07_CA/Exercise_7_04_WolframCA_scrolling/CA.js b/chp07_CA/Exercise_7_04_WolframCA_scrolling/CA.js index 173bcee..8ca494f 100644 --- a/chp07_CA/Exercise_7_04_WolframCA_scrolling/CA.js +++ b/chp07_CA/Exercise_7_04_WolframCA_scrolling/CA.js @@ -16,14 +16,14 @@ function CA(r) { this.rows = height/this.w; // Store a history of generations in 2D array, not just one this.matrix = new Array(this.cols); - for( var i = 0; i < this.cols; i++) { + for(let i = 0; i < this.cols; i++) { this.matrix[i] = new Array(this.rows); } // Reset to generation 0 this.restart = function() { - for (var i = 0; i < this.cols; i++) { - for (var j = 0; j < this.rows; j++) { + for (let i = 0; i < this.cols; i++) { + for (let j = 0; j < this.rows; j++) { this.matrix[i][j] = 0; } } @@ -34,7 +34,7 @@ function CA(r) { // Make a random ruleset this.randomize = function() { - for (var i = 0; i < 8; i++) { + for (let i = 0; i < 8; i++) { this.ruleset[i] = Math.floor(random(2)); } }; @@ -47,10 +47,10 @@ function CA(r) { // For every spot, determine new state by examing current state, and neighbor states // Ignore edges that only have one neighor - for (var i = 0; i < this.cols; i++) { - var left = this.matrix[(i+this.cols-1)%this.cols][this.generation%this.rows]; // Left neighbor state - var me = this.matrix[i][this.generation%this.rows]; // Current state - var right = this.matrix[(i+1)%this.cols][this.generation%this.rows]; // Right neighbor state + for (let i = 0; i < this.cols; i++) { + let left = this.matrix[(i+this.cols-1)%this.cols][this.generation%this.rows]; // Left neighbor state + let me = this.matrix[i][this.generation%this.rows]; // Current state + let right = this.matrix[(i+1)%this.cols][this.generation%this.rows]; // Right neighbor state this.matrix[i][(this.generation+1)%this.rows] = this.rules(left, me, right); // Compute next generation state based on ruleset } this.generation++; @@ -58,11 +58,11 @@ function CA(r) { // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' this.display = function() { - var offset = this.generation%this.rows; + let offset = this.generation%this.rows; - for (var i = 0; i < this.cols; i++) { - for (var j = 0; j < this.rows; j++) { - var y = j - offset; + for (let i = 0; i < this.cols; i++) { + for (let j = 0; j < this.rows; j++) { + let y = j - offset; if (y <= 0) y = this.rows + y; // Only draw if cell state is 1 if (this.matrix[i][j] == 1) { @@ -77,8 +77,8 @@ function CA(r) { // Implementing the Wolfram rules // This is the concise conversion to binary way this.rules = function(a, b, c) { - var s = "" + a + b + c; - var index = parseInt(s, 2); + let s = "" + a + b + c; + let index = parseInt(s, 2); return this.ruleset[index]; }; diff --git a/chp07_CA/Exercise_7_04_WolframCA_scrolling/sketch.js b/chp07_CA/Exercise_7_04_WolframCA_scrolling/sketch.js index 04b2dc0..1ca13cb 100644 --- a/chp07_CA/Exercise_7_04_WolframCA_scrolling/sketch.js +++ b/chp07_CA/Exercise_7_04_WolframCA_scrolling/sketch.js @@ -9,11 +9,11 @@ // Also implements wrap around // An object to describe a Wolfram elementary Cellular Automata -var ca; +let ca; function setup() { createCanvas(360, 600); - var ruleset = new Array(0,1,0,1,1,0,1,0); // Rule 90 + let ruleset = new Array(0,1,0,1,1,0,1,0); // Rule 90 //int[] ruleset = {0,1,1,1,1,0,1,1}; // Rule 222 //int[] ruleset = {0,1,1,1,1,1,0,1}; // Rule 190 diff --git a/chp07_CA/GameOfLifeWrapAround/GOL.js b/chp07_CA/GameOfLifeWrapAround/GOL.js index 85c858c..112fd26 100644 --- a/chp07_CA/GameOfLifeWrapAround/GOL.js +++ b/chp07_CA/GameOfLifeWrapAround/GOL.js @@ -8,13 +8,13 @@ function GOL() { this.columns = width/this.w; this.rows = height/this.w; this.board = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { + for (let i = 0; i < this.columns; i++) { this.board[i] = new Array(this.rows); } this.init = function() { - for (var i =0;i < this.columns;i++) { - for (var j =0;j < this.rows;j++) { + for (let i =0;i < this.columns;i++) { + for (let j =0;j < this.rows;j++) { this.board[i][j] = Math.floor(random(2)); } } @@ -24,19 +24,19 @@ function GOL() { // The process of creating the new generation this.generate = function() { - var next = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { + let next = new Array(this.columns); + for (let i = 0; i < this.columns; i++) { next[i] = new Array(this.rows); } // Loop through every spot in our 2D array and check spots neighbors - for (var x = 0; x < this.columns; x++) { - for (var y = 0; y < this.rows; y++) { + for (let x = 0; x < this.columns; x++) { + for (let y = 0; y < this.rows; y++) { // Add up all the states in a 3x3 surrounding grid - var neighbors = 0; + let neighbors = 0; - for (var i = -1; i <= 1; i++) { - for (var j = -1; j <= 1; j++) { + for (let i = -1; i <= 1; i++) { + for (let j = -1; j <= 1; j++) { neighbors += this.board[(x+i+this.columns)%this.columns][(y+j+this.rows)%this.rows]; } } @@ -59,8 +59,8 @@ function GOL() { // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' this.display = function() { - for ( var i = 0; i < this.columns;i++) { - for ( var j = 0; j < this.rows;j++) { + for (let i = 0; i < this.columns;i++) { + for (let j = 0; j < this.rows;j++) { if ((this.board[i][j] == 1)) fill(0); else fill(255); stroke(0); diff --git a/chp07_CA/GameOfLifeWrapAround/sketch.js b/chp07_CA/GameOfLifeWrapAround/sketch.js index b9c0b3a..dc48a4f 100644 --- a/chp07_CA/GameOfLifeWrapAround/sketch.js +++ b/chp07_CA/GameOfLifeWrapAround/sketch.js @@ -6,7 +6,7 @@ // Each cell is now an object! -var gol; +let gol; function setup() { createCanvas(640, 360); diff --git a/chp07_CA/NOC_7_01_WolframCA_simple/CA.js b/chp07_CA/NOC_7_01_WolframCA_simple/CA.js index 6c592dd..be97291 100644 --- a/chp07_CA/NOC_7_01_WolframCA_simple/CA.js +++ b/chp07_CA/NOC_7_01_WolframCA_simple/CA.js @@ -9,7 +9,7 @@ function CA() { this.w = 10; // An array of 0s and 1s this.cells = new Array(width/this.w); - for (var i = 0; i < this.cells.length; i++) { + for (let i = 0; i < this.cells.length; i++) { this.cells[i] = 0; } // We arbitrarily start with just the middle cell having a state of "1" @@ -22,16 +22,16 @@ function CA() { // The process of creating the new generation this.generate = function() { // First we create an empty array filled with 0s for the new values - var nextgen = []; - for (var i = 0; i < this.cells.length; i++) { + let nextgen = []; + for (let i = 0; i < this.cells.length; i++) { nextgen[i] = 0; } // For every spot, determine new state by examing current state, and neighbor states // Ignore edges that only have one neighor - for (var i = 1; i < this.cells.length-1; i++) { - var left = this.cells[i-1]; // Left neighbor state - var me = this.cells[i]; // Current state - var right = this.cells[i+1]; // Right neighbor state + for (let i = 1; i < this.cells.length-1; i++) { + let left = this.cells[i-1]; // Left neighbor state + let me = this.cells[i]; // Current state + let right = this.cells[i+1]; // Right neighbor state nextgen[i] = this.rules(left, me, right); // Compute next generation state based on ruleset } // The current generation is the new generation @@ -41,7 +41,7 @@ function CA() { // This is the easy part, just draw the cells this.display = function() { - for (var i = 0; i < this.cells.length; i++) { + for (let i = 0; i < this.cells.length; i++) { if (this.cells[i] == 1) fill(200); else fill(51); noStroke(); diff --git a/chp07_CA/NOC_7_01_WolframCA_simple/sketch.js b/chp07_CA/NOC_7_01_WolframCA_simple/sketch.js index a3953f5..ab0a719 100644 --- a/chp07_CA/NOC_7_01_WolframCA_simple/sketch.js +++ b/chp07_CA/NOC_7_01_WolframCA_simple/sketch.js @@ -5,7 +5,7 @@ // Simple demonstration of a Wolfram 1-dimensional cellular automata -var ca; +let ca; function setup() { createCanvas(640, 360); diff --git a/chp07_CA/NOC_7_02_GameOfLifeSimple/GOL.js b/chp07_CA/NOC_7_02_GameOfLifeSimple/GOL.js index cc84f9d..251da44 100644 --- a/chp07_CA/NOC_7_02_GameOfLifeSimple/GOL.js +++ b/chp07_CA/NOC_7_02_GameOfLifeSimple/GOL.js @@ -8,17 +8,17 @@ function GOL() { this.columns = width/this.w; this.rows = height/this.w; this.board = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { + for (let i = 0; i < this.columns; i++) { this.board[i] = new Array(this.rows); } // Going to use multiple 2D arrays and swap them this.next = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { + for (let i = 0; i < this.columns; i++) { this.next[i] = new Array(this.rows); } this.init = function() { - for (var i =0;i < this.columns;i++) { - for (var j =0;j < this.rows;j++) { + for (let i =0;i < this.columns;i++) { + for (let j =0;j < this.rows;j++) { // Lining the edges with 0s if (i === 0 || j === 0 || i == this.columns-1 || j == this.rows-1) this.board[i][j] = 0; // Filling the rest randomly @@ -35,13 +35,13 @@ function GOL() { // Loop through every spot in our 2D array and check spots neighbors - for (var x = 1; x < this.columns-1; x++) { - for (var y = 1; y < this.rows-1; y++) { + for (let x = 1; x < this.columns-1; x++) { + for (let y = 1; y < this.rows-1; y++) { // Add up all the states in a 3x3 surrounding grid - var neighbors = 0; + let neighbors = 0; - for (var i = -1; i <= 1; i++) { - for (var j = -1; j <= 1; j++) { + for (let i = -1; i <= 1; i++) { + for (let j = -1; j <= 1; j++) { neighbors += this.board[x+i][y+j]; } } @@ -59,15 +59,15 @@ function GOL() { } // Swap! - var temp = this.board; + let temp = this.board; this.board = this.next; this.next = temp; }; // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' this.display = function() { - for ( var i = 0; i < this.columns;i++) { - for ( var j = 0; j < this.rows;j++) { + for (let i = 0; i < this.columns;i++) { + for (let j = 0; j < this.rows;j++) { if ((this.board[i][j] == 1)) fill(0); else fill(255); stroke(0); diff --git a/chp07_CA/NOC_7_02_GameOfLifeSimple/sketch.js b/chp07_CA/NOC_7_02_GameOfLifeSimple/sketch.js index 9700ec1..083c6a4 100644 --- a/chp07_CA/NOC_7_02_GameOfLifeSimple/sketch.js +++ b/chp07_CA/NOC_7_02_GameOfLifeSimple/sketch.js @@ -8,7 +8,7 @@ // to describe each individual cell and a "cellular automata" class // to describe a collection of cells -var gol; +let gol; function setup() { createCanvas(640, 360); diff --git a/chp07_CA/NOC_7_03_GameOfLifeOOP/GOL.js b/chp07_CA/NOC_7_03_GameOfLifeOOP/GOL.js index 8a62454..f2239fa 100644 --- a/chp07_CA/NOC_7_03_GameOfLifeOOP/GOL.js +++ b/chp07_CA/NOC_7_03_GameOfLifeOOP/GOL.js @@ -10,13 +10,13 @@ function GOL() { // Game of life board this.board = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { + for (let i = 0; i < this.columns; i++) { this.board[i] = new Array(this.rows); } this.init = function() { - for (var i = 0; i < this.columns; i++) { - for (var j = 0; j < this.rows; j++) { + for (let i = 0; i < this.columns; i++) { + for (let j = 0; j < this.rows; j++) { this.board[i][j] = new Cell(i*this.w, j*this.w, this.w); } } @@ -25,20 +25,20 @@ function GOL() { // The process of creating the new generation this.generate = function() { - for ( var i = 0; i < this.columns;i++) { - for ( var j = 0; j < this.rows;j++) { + for (let i = 0; i < this.columns;i++) { + for (let j = 0; j < this.rows;j++) { this.board[i][j].savePrevious(); } } // Loop through every spot in our 2D array and check spots neighbors - for (var x = 0; x < this.columns; x++) { - for (var y = 0; y < this.rows; y++) { + for (let x = 0; x < this.columns; x++) { + for (let y = 0; y < this.rows; y++) { // Add up all the states in a 3x3 surrounding grid - var neighbors = 0; - for (var i = -1; i <= 1; i++) { - for (var j = -1; j <= 1; j++) { + let neighbors = 0; + for (let i = -1; i <= 1; i++) { + for (let j = -1; j <= 1; j++) { neighbors += this.board[(x+i+this.columns)%this.columns][(y+j+this.rows)%this.rows].previous; } } @@ -58,8 +58,8 @@ function GOL() { // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' this.display = function() { - for ( var i = 0; i < this.columns;i++) { - for ( var j = 0; j < this.rows;j++) { + for (let i = 0; i < this.columns;i++) { + for (let j = 0; j < this.rows;j++) { this.board[i][j].display(); } } diff --git a/chp07_CA/NOC_7_03_GameOfLifeOOP/sketch.js b/chp07_CA/NOC_7_03_GameOfLifeOOP/sketch.js index b9c0b3a..dc48a4f 100644 --- a/chp07_CA/NOC_7_03_GameOfLifeOOP/sketch.js +++ b/chp07_CA/NOC_7_03_GameOfLifeOOP/sketch.js @@ -6,7 +6,7 @@ // Each cell is now an object! -var gol; +let gol; function setup() { createCanvas(640, 360); diff --git a/chp08_fractals/NOC_8_04_CantorSet/sketch.js b/chp08_fractals/NOC_8_04_CantorSet/sketch.js index 3aa11cc..733476b 100644 --- a/chp08_fractals/NOC_8_04_CantorSet/sketch.js +++ b/chp08_fractals/NOC_8_04_CantorSet/sketch.js @@ -21,7 +21,7 @@ function draw() { function cantor(x, y, len) { - var h = 30; + let h = 30; // recursive exit condition if (len >= 1) { diff --git a/chp08_fractals/NOC_8_05_Koch/KochFractal.js b/chp08_fractals/NOC_8_05_Koch/KochFractal.js index caaf80e..ae8e6b7 100644 --- a/chp08_fractals/NOC_8_05_Koch/KochFractal.js +++ b/chp08_fractals/NOC_8_05_Koch/KochFractal.js @@ -31,7 +31,7 @@ function KochFractal() { // This is easy, just draw all the lines this.render = function() { - for(var i = 0; i < this.lines.length; i++) { + for(let i = 0; i < this.lines.length; i++) { this.lines[i].display(); } } @@ -45,15 +45,15 @@ function KochFractal() { // As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . . this.iterate = function(before) { - var now = []; // Create emtpy list - for(var i = 0; i < before.length; i++) { - var l = before[i]; + let now = []; // Create emtpy list + for(let i = 0; i < before.length; i++) { + let l = before[i]; // Calculate 5 koch p5.Vectors (done for us by the line object) - var a = l.kochA(); - var b = l.kochB(); - var c = l.kochC(); - var d = l.kochD(); - var e = l.kochE(); + let a = l.kochA(); + let b = l.kochB(); + let c = l.kochC(); + let d = l.kochD(); + let e = l.kochE(); // Make line segments between all the p5.Vectors and add them now.push(new KochLine(a,b)); now.push(new KochLine(b,c)); diff --git a/chp08_fractals/NOC_8_05_Koch/KochLine.js b/chp08_fractals/NOC_8_05_Koch/KochLine.js index 59cd133..adcfa17 100644 --- a/chp08_fractals/NOC_8_05_Koch/KochLine.js +++ b/chp08_fractals/NOC_8_05_Koch/KochLine.js @@ -24,7 +24,7 @@ function KochLine(a,b) { // This is easy, just 1/3 of the way this.kochB = function() { - var v = p5.Vector.sub(this.end, this.start); + let v = p5.Vector.sub(this.end, this.start); v.div(3); v.add(this.start); return v; @@ -32,8 +32,8 @@ function KochLine(a,b) { // More complicated, have to use a little trig to figure out where this p5.Vector is! this.kochC = function() { - var a = this.start.copy(); // Start at the beginning - var v = p5.Vector.sub(this.end, this.start); + let a = this.start.copy(); // Start at the beginning + let v = p5.Vector.sub(this.end, this.start); v.div(3); a.add(v); // Move to point B v.rotate(-PI/3); // Rotate 60 degrees @@ -43,7 +43,7 @@ function KochLine(a,b) { // Easy, just 2/3 of the way this.kochD = function() { - var v = p5.Vector.sub(this.end, this.start); + let v = p5.Vector.sub(this.end, this.start); v.mult(2/3.0); v.add(this.start); return v; diff --git a/chp08_fractals/NOC_8_05_Koch/sketch.js b/chp08_fractals/NOC_8_05_Koch/sketch.js index 749c648..70170e6 100644 --- a/chp08_fractals/NOC_8_05_Koch/sketch.js +++ b/chp08_fractals/NOC_8_05_Koch/sketch.js @@ -6,7 +6,7 @@ // Renders a simple fractal, the Koch snowflake // Each recursive level drawn in sequence -var k; +let k; function setup() { createCanvas(640,360); diff --git a/chp08_fractals/NOC_8_05_KochSimple/KochLine.js b/chp08_fractals/NOC_8_05_KochSimple/KochLine.js index 132d6d6..6716a26 100644 --- a/chp08_fractals/NOC_8_05_KochSimple/KochLine.js +++ b/chp08_fractals/NOC_8_05_KochSimple/KochLine.js @@ -24,7 +24,7 @@ function KochLine(a,b) { // This is easy, just 1/3 of the way this.kochB = function() { - var v = p5.Vector.sub(this.end, this.start); + let v = p5.Vector.sub(this.end, this.start); v.div(3); v.add(this.start); return v; @@ -32,8 +32,8 @@ function KochLine(a,b) { // More complicated, have to use a little trig to figure out where this p5.Vector is! this.kochC = function() { - var a = this.start.copy(); // Start at the beginning - var v = p5.Vector.sub(this.end, this.start); + let a = this.start.copy(); // Start at the beginning + let v = p5.Vector.sub(this.end, this.start); v.div(3); a.add(v); // Move to point B v.rotate(-PI/3); // Rotate 60 degrees @@ -43,7 +43,7 @@ function KochLine(a,b) { // Easy, just 2/3 of the way this.kochD = function() { - var v = p5.Vector.sub(this.end, this.start); + let v = p5.Vector.sub(this.end, this.start); v.mult(2/3.0); v.add(this.start); return v; diff --git a/chp08_fractals/NOC_8_05_KochSimple/sketch.js b/chp08_fractals/NOC_8_05_KochSimple/sketch.js index 95eea78..bb6268f 100644 --- a/chp08_fractals/NOC_8_05_KochSimple/sketch.js +++ b/chp08_fractals/NOC_8_05_KochSimple/sketch.js @@ -6,37 +6,37 @@ // Renders a simple fractal, the Koch snowflake // Each recursive level drawn in sequence -var lines = []; // A list to keep track of all the lines +let lines = []; // A list to keep track of all the lines function setup() { createCanvas(383, 200); - var start = createVector(0, 150); - var end = createVector(width, 150); + let start = createVector(0, 150); + let end = createVector(width, 150); lines.push(new KochLine(start, end)); - for (var i = 0; i < 5; i++) { + for (let i = 0; i < 5; i++) { generate(); } } function draw() { background(51); - for (var i = 0; i < lines.length; i++) { - var l = lines[i]; + for (let i = 0; i < lines.length; i++) { + let l = lines[i]; l.display(); } } function generate() { - var next = []; // Create emtpy list - for (var i = 0; i < lines.length; i++) { - var l = lines[i]; + let next = []; // Create emtpy list + for (let i = 0; i < lines.length; i++) { + let l = lines[i]; // Calculate 5 koch p5.Vectors (done for us by the line object) - var a = l.kochA(); - var b = l.kochB(); - var c = l.kochC(); - var d = l.kochD(); - var e = l.kochE(); + let a = l.kochA(); + let b = l.kochB(); + let c = l.kochC(); + let d = l.kochD(); + let e = l.kochE(); // Make line segments between all the p5.Vectors and add them next.push(new KochLine(a, b)); next.push(new KochLine(b, c)); diff --git a/chp08_fractals/NOC_8_06_Tree/sketch.js b/chp08_fractals/NOC_8_06_Tree/sketch.js index aa30c33..4843c59 100644 --- a/chp08_fractals/NOC_8_06_Tree/sketch.js +++ b/chp08_fractals/NOC_8_06_Tree/sketch.js @@ -6,7 +6,7 @@ // Renders a simple tree-like structure via recursion // Branching angle calculated as a function of horizontal mouse position -var theta; +let theta; function setup() { createCanvas(640, 360); diff --git a/chp08_fractals/NOC_8_07_TreeStochastic/sketch.js b/chp08_fractals/NOC_8_07_TreeStochastic/sketch.js index 8f20ef4..eae5818 100644 --- a/chp08_fractals/NOC_8_07_TreeStochastic/sketch.js +++ b/chp08_fractals/NOC_8_07_TreeStochastic/sketch.js @@ -7,7 +7,7 @@ // Angles and number of branches are random function setup() { - var test = createP('Click mouse to generate a new tree'); + let test = createP('Click mouse to generate a new tree'); test.position(10,372); createCanvas(640, 360); @@ -39,7 +39,7 @@ function newTree() { function branch(h) { // thickness of the branch is mapped to its length - var sw = map(h, 2, 120, 1, 5); + let sw = map(h, 2, 120, 1, 5); strokeWeight(sw); // Draw the actual branch line(0, 0, 0, -h); @@ -53,10 +53,10 @@ function branch(h) { // Here, ours is when the length of the branch is 2 pixels or less if (h > 2) { // A random number of branches - var n = Math.floor(random(1, 4)); - for (var i = 0; i < n; i++) { + let n = Math.floor(random(1, 4)); + for (let i = 0; i < n; i++) { // Picking a random angle - var theta = random(-PI/3, PI/3); + let theta = random(-PI/3, PI/3); push(); // Save the current state of transformation (i.e. where are we now) rotate(theta); // Rotate by theta branch(h); // Ok, now call myself to branch again diff --git a/chp08_fractals/NOC_8_08_SimpleLSystem/sketch.js b/chp08_fractals/NOC_8_08_SimpleLSystem/sketch.js index e70eb22..1167391 100644 --- a/chp08_fractals/NOC_8_08_SimpleLSystem/sketch.js +++ b/chp08_fractals/NOC_8_08_SimpleLSystem/sketch.js @@ -7,14 +7,14 @@ // No drawing // Start with 'A' -var current = 'A'; +let current = 'A'; // Number of generations -var count = 0; +let count = 0; -var instructions; -var show; +let instructions; +let show; -var results = ''; +let results = ''; function setup() { instructions = createP('Click the mouse to generate.'); @@ -29,11 +29,11 @@ function setup() { function generate() { // A new StringBuffer for the next generation - var next = ''; + let next = ''; // Look through the current String to replace according to L-System rules - for (var i = 0; i < current.length; i++) { - var c = current.charAt(i); + for (let i = 0; i < current.length; i++) { + let c = current.charAt(i); if (c === 'A') { // If we find A replace with AB next += 'AB'; diff --git a/chp08_fractals/NOC_8_09_LSystem/LSystem.js b/chp08_fractals/NOC_8_09_LSystem/LSystem.js index 7cde195..dcbc639 100644 --- a/chp08_fractals/NOC_8_09_LSystem/LSystem.js +++ b/chp08_fractals/NOC_8_09_LSystem/LSystem.js @@ -16,15 +16,15 @@ function LSystem(axiom, r) { // Generate the next generation this.generate = function() { // An empty StringBuffer that we will fill - var nextgen = ''; + let nextgen = ''; // For every character in the sentence - for (var i = 0; i < this.sentence.length; i++) { + for (let i = 0; i < this.sentence.length; i++) { // What is the character // We will replace it with itself unless it matches one of our rules - var replace = this.sentence.charAt(i); + let replace = this.sentence.charAt(i); // Check every rule - for (var j = 0; j < this.ruleset.length; j++) { - var a = this.ruleset[j].getA(); + for (let j = 0; j < this.ruleset.length; j++) { + let a = this.ruleset[j].getA(); // if we match the Rule, get the replacement String out of the Rule if (a === replace) { replace = this.ruleset[j].getB(); diff --git a/chp08_fractals/NOC_8_09_LSystem/Turtle.js b/chp08_fractals/NOC_8_09_LSystem/Turtle.js index 0c954c4..5e67245 100644 --- a/chp08_fractals/NOC_8_09_LSystem/Turtle.js +++ b/chp08_fractals/NOC_8_09_LSystem/Turtle.js @@ -9,8 +9,8 @@ function Turtle(s, l, t) { this.render = function() { stroke(255); - for (var i = 0; i < this.todo.length; i++) { - var c = this.todo.charAt(i); + for (let i = 0; i < this.todo.length; i++) { + let c = this.todo.charAt(i); if (c === 'F' || c === 'G') { line(0,0,this.len,0); translate(this.len,0); diff --git a/chp08_fractals/NOC_8_09_LSystem/sketch.js b/chp08_fractals/NOC_8_09_LSystem/sketch.js index 02a21dd..ded9b34 100644 --- a/chp08_fractals/NOC_8_09_LSystem/sketch.js +++ b/chp08_fractals/NOC_8_09_LSystem/sketch.js @@ -2,8 +2,8 @@ // Daniel Shiffman // http://natureofcode.com -var lsys; -var turtle; +let lsys; +let turtle; function setup() { createCanvas(600, 600); @@ -25,7 +25,7 @@ function setup() { turtle = new Turtle(lsys.getSentence(),width-1,PI/2); */ - var ruleset = []; + let ruleset = []; ruleset[0] = new Rule('F', "FF+[+F-F-F]-[-F+F+F]"); lsys = new LSystem("F", ruleset); turtle = new Turtle(lsys.getSentence(), height/3, radians(25)); @@ -41,7 +41,7 @@ function draw() { turtle.render(); } -var counter = 0; +let counter = 0; function mousePressed() { if (counter < 5) { diff --git a/chp09_ga/NOC_9_02_SmartRockets_superbasic/population.js b/chp09_ga/NOC_9_02_SmartRockets_superbasic/population.js index 8428fd4..6e72ad9 100644 --- a/chp09_ga/NOC_9_02_SmartRockets_superbasic/population.js +++ b/chp09_ga/NOC_9_02_SmartRockets_superbasic/population.js @@ -14,22 +14,22 @@ class Population { this.matingPool = []; // ArrayList which we will use for our "mating pool" this.generations = 0; // Number of generations //make a new set of creatures - for (var i = 0; i < num; i++) { - var location = createVector(width / 2, height + 20); + for (let i = 0; i < num; i++) { + let location = createVector(width / 2, height + 20); this.population[i] = new Rocket(location, new DNA()); } } live() { // Run every rocket - for (var i = 0; i < this.population.length; i++) { + for (let i = 0; i < this.population.length; i++) { this.population[i].run(); } } // Calculate fitness for each creature fitness() { - for (var i = 0; i < this.population.length; i++) { + for (let i = 0; i < this.population.length; i++) { this.population[i].calcFitness(); } } @@ -40,17 +40,17 @@ class Population { this.matingPool = []; // Calculate total fitness of whole population - var maxFitness = this.getMaxFitness(); + let maxFitness = this.getMaxFitness(); // Calculate fitness for each member of the population (scaled to value between 0 and 1) // Based on fitness, each member will get added to the mating pool a certain number of times // A higher fitness = more entries to mating pool = more likely to be picked as a parent // A lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (var i = 0; i < this.population.length; i++) { - var fitnessNormal = map(this.population[i].getFitness(), 0, maxFitness, 0, 1); - var n = floor(fitnessNormal * 100); // Arbitrary multiplier + for (let i = 0; i < this.population.length; i++) { + let fitnessNormal = map(this.population[i].getFitness(), 0, maxFitness, 0, 1); + let n = floor(fitnessNormal * 100); // Arbitrary multiplier - for (var j = 0; j < n; j++) { + for (let j = 0; j < n; j++) { this.matingPool.push(this.population[i]); } } @@ -59,22 +59,22 @@ class Population { // Making the next generation reproduction() { // Refill the population with children from the mating pool - for (var i = 0; i < this.population.length; i++) { + for (let i = 0; i < this.population.length; i++) { // Sping the wheel of fortune to pick two parents - var m = floor(random(this.matingPool.length)); - var d = floor(random(this.matingPool.length)); + let m = floor(random(this.matingPool.length)); + let d = floor(random(this.matingPool.length)); // Pick two parents - var mom = this.matingPool[m]; - var dad = this.matingPool[d]; + let mom = this.matingPool[m]; + let dad = this.matingPool[d]; // Get their genes - var momgenes = mom.getDNA(); - var dadgenes = dad.getDNA(); + let momgenes = mom.getDNA(); + let dadgenes = dad.getDNA(); // Mate their genes - var child = momgenes.crossover(dadgenes); + let child = momgenes.crossover(dadgenes); // Mutate their genes child.mutate(this.mutationRate); // Fill the new population with the new child - var location = createVector(width / 2, height + 20); + let location = createVector(width / 2, height + 20); this.population[i] = new Rocket(location, child); } this.generations++; @@ -86,8 +86,8 @@ class Population { // Find highest fitness for the population getMaxFitness() { - var record = 0; - for (var i = 0; i < this.population.length; i++) { + let record = 0; + for (let i = 0; i < this.population.length; i++) { if (this.population[i].getFitness() > record) { record = this.population[i].getFitness(); }