Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #125 from ferreira-mariana/master
Browse files Browse the repository at this point in the history
NOC in ES6: change from var to let
  • Loading branch information
shiffman authored Aug 24, 2023
2 parents 33cd32a + 27ada4b commit bf4fc4b
Show file tree
Hide file tree
Showing 34 changed files with 164 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion chp01_vectors/NOC_1_10_motion101_acceleration/mover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion chp02_forces/NOC_2_01_forces/mover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions chp04_systems/NOC_4_02_ArrayParticles/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions chp06_agents/NOC_6_01_Seek/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion chp06_agents/NOC_6_02_Arrive/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions chp07_CA/Exercise_7_01_WolframCA_randomizedrules/CA.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions chp07_CA/Exercise_7_01_WolframCA_randomizedrules/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
28 changes: 14 additions & 14 deletions chp07_CA/Exercise_7_04_WolframCA_scrolling/CA.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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));
}
};
Expand All @@ -47,22 +47,22 @@ 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++;
};

// 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) {
Expand All @@ -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];
};

Expand Down
4 changes: 2 additions & 2 deletions chp07_CA/Exercise_7_04_WolframCA_scrolling/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions chp07_CA/GameOfLifeWrapAround/GOL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -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];
}
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion chp07_CA/GameOfLifeWrapAround/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Each cell is now an object!

var gol;
let gol;

function setup() {
createCanvas(640, 360);
Expand Down
16 changes: 8 additions & 8 deletions chp07_CA/NOC_7_01_WolframCA_simple/CA.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion chp07_CA/NOC_7_01_WolframCA_simple/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// Simple demonstration of a Wolfram 1-dimensional cellular automata

var ca;
let ca;

function setup() {
createCanvas(640, 360);
Expand Down
Loading

0 comments on commit bf4fc4b

Please sign in to comment.