8
8
*/
9
9
10
10
11
- var flock ;
11
+ let flock ;
12
12
13
- var text ;
13
+ let text ;
14
14
15
15
function setup ( ) {
16
- createCanvas ( 640 , 360 ) ;
16
+ createCanvas ( 640 , 360 ) ;
17
17
createP ( "Drag the mouse to generate new boids." ) ;
18
18
19
19
flock = new Flock ( ) ;
20
20
// Añade un conjunto inicial de boids al sistema
21
- for ( var i = 0 ; i < 100 ; i ++ ) {
22
- var b = new Boid ( width / 2 , height / 2 ) ;
21
+ for ( let i = 0 ; i < 100 ; i ++ ) {
22
+ let b = new Boid ( width / 2 , height / 2 ) ;
23
23
flock . addBoid ( b ) ;
24
24
}
25
25
}
@@ -31,7 +31,7 @@ function draw() {
31
31
32
32
// Añade un nuevo boid al sistema
33
33
function mouseDragged ( ) {
34
- flock . addBoid ( new Boid ( mouseX , mouseY ) ) ;
34
+ flock . addBoid ( new Boid ( mouseX , mouseY ) ) ;
35
35
}
36
36
37
37
// The Nature of Code
@@ -47,7 +47,7 @@ function Flock() {
47
47
}
48
48
49
49
Flock . prototype . run = function ( ) {
50
- for ( var i = 0 ; i < this . boids . length ; i ++ ) {
50
+ for ( let i = 0 ; i < this . boids . length ; i ++ ) {
51
51
this . boids [ i ] . run ( this . boids ) ; // Pasar la lista entera de boids a cada boid de forma individual
52
52
}
53
53
}
@@ -63,9 +63,9 @@ Flock.prototype.addBoid = function(b) {
63
63
// Clase Boid
64
64
// Métodos para Separación, Cohesión, alineamiento
65
65
66
- function Boid ( x , y ) {
67
- this . acceleration = createVector ( 0 , 0 ) ;
68
- this . velocity = createVector ( random ( - 1 , 1 ) , random ( - 1 , 1 ) ) ;
66
+ function Boid ( x , y ) {
67
+ this . acceleration = createVector ( 0 , 0 ) ;
68
+ this . velocity = createVector ( random ( - 1 , 1 ) , random ( - 1 , 1 ) ) ;
69
69
this . position = createVector ( x , y ) ;
70
70
this . r = 3.0 ;
71
71
this . maxspeed = 3 ; // Velocidad máxima
@@ -86,9 +86,9 @@ Boid.prototype.applyForce = function(force) {
86
86
87
87
// Acumular una nueva aceleración cada vez basado en tres reglas
88
88
Boid . prototype . flock = function ( boids ) {
89
- var sep = this . separate ( boids ) ; // Separación
90
- var ali = this . align ( boids ) ; // Alineamiento
91
- var coh = this . cohesion ( boids ) ; // Cohesión
89
+ let sep = this . separate ( boids ) ; // Separación
90
+ let ali = this . align ( boids ) ; // Alineamiento
91
+ let coh = this . cohesion ( boids ) ; // Cohesión
92
92
// Dar un peso arbitrario a cada fuerza
93
93
sep . mult ( 1.5 ) ;
94
94
ali . mult ( 1.0 ) ;
@@ -113,28 +113,28 @@ Boid.prototype.update = function() {
113
113
// Un método que calcula y aplica una fuerza de viraje hacia una posición objetivo
114
114
// VIRAJE = DESEADO - VELOCIDAD
115
115
Boid . prototype . seek = function ( target ) {
116
- var desired = p5 . Vector . sub ( target , this . position ) ; // Un vector apuntando desde la ubicación hacia el objetivo
116
+ let desired = p5 . Vector . sub ( target , this . position ) ; // Un vector apuntando desde la ubicación hacia el objetivo
117
117
// Normalizar deseado y escalar según velocidad máxima
118
118
desired . normalize ( ) ;
119
119
desired . mult ( this . maxspeed ) ;
120
120
// Viraje = Deseado - Velocidad
121
- var steer = p5 . Vector . sub ( desired , this . velocity ) ;
121
+ let steer = p5 . Vector . sub ( desired , this . velocity ) ;
122
122
steer . limit ( this . maxforce ) ; // Limita al máximo de fuerza de viraje
123
123
return steer ;
124
124
}
125
125
126
126
Boid . prototype . render = function ( ) {
127
127
// Dibuja un triángulo rotado en la dirección de la velocidad
128
- var theta = this . velocity . heading ( ) + radians ( 90 ) ;
128
+ let theta = this . velocity . heading ( ) + radians ( 90 ) ;
129
129
fill ( 127 ) ;
130
130
stroke ( 200 ) ;
131
131
push ( ) ;
132
- translate ( this . position . x , this . position . y ) ;
132
+ translate ( this . position . x , this . position . y ) ;
133
133
rotate ( theta ) ;
134
134
beginShape ( ) ;
135
- vertex ( 0 , - this . r * 2 ) ;
136
- vertex ( - this . r , this . r * 2 ) ;
137
- vertex ( this . r , this . r * 2 ) ;
135
+ vertex ( 0 , - this . r * 2 ) ;
136
+ vertex ( - this . r , this . r * 2 ) ;
137
+ vertex ( this . r , this . r * 2 ) ;
138
138
endShape ( CLOSE ) ;
139
139
pop ( ) ;
140
140
}
@@ -143,23 +143,23 @@ Boid.prototype.render = function() {
143
143
Boid . prototype . borders = function ( ) {
144
144
if ( this . position . x < - this . r ) this . position . x = width + this . r ;
145
145
if ( this . position . y < - this . r ) this . position . y = height + this . r ;
146
- if ( this . position . x > width + this . r ) this . position . x = - this . r ;
147
- if ( this . position . y > height + this . r ) this . position . y = - this . r ;
146
+ if ( this . position . x > width + this . r ) this . position . x = - this . r ;
147
+ if ( this . position . y > height + this . r ) this . position . y = - this . r ;
148
148
}
149
149
150
150
// Separación
151
151
// Método que revisa los boids cercanos y vira para alejarse de ellos
152
152
Boid . prototype . separate = function ( boids ) {
153
- var desiredseparation = 25.0 ;
154
- var steer = createVector ( 0 , 0 ) ;
155
- var count = 0 ;
153
+ let desiredseparation = 25.0 ;
154
+ let steer = createVector ( 0 , 0 ) ;
155
+ let count = 0 ;
156
156
// Por cada boid en el sistema, revisa si está muy cerca
157
- for ( var i = 0 ; i < boids . length ; i ++ ) {
158
- var d = p5 . Vector . dist ( this . position , boids [ i ] . position ) ;
157
+ for ( let i = 0 ; i < boids . length ; i ++ ) {
158
+ let d = p5 . Vector . dist ( this . position , boids [ i ] . position ) ;
159
159
// Si la distancia es mayor a 0 y menor que una cantidad arbitraria (0 cuando eres tú mismo)
160
160
if ( ( d > 0 ) && ( d < desiredseparation ) ) {
161
161
// Calcular el vector apuntando a alejarse del vecino
162
- var diff = p5 . Vector . sub ( this . position , boids [ i ] . position ) ;
162
+ let diff = p5 . Vector . sub ( this . position , boids [ i ] . position ) ;
163
163
diff . normalize ( ) ;
164
164
diff . div ( d ) ; // Peso por distancia
165
165
steer . add ( diff ) ;
@@ -185,11 +185,11 @@ Boid.prototype.separate = function(boids) {
185
185
// Alineamiento
186
186
// Para cada boid cercano en el sistema, calcula la velocidad promedio
187
187
Boid . prototype . align = function ( boids ) {
188
- var neighbordist = 50 ;
189
- var sum = createVector ( 0 , 0 ) ;
190
- var count = 0 ;
191
- for ( var i = 0 ; i < boids . length ; i ++ ) {
192
- var d = p5 . Vector . dist ( this . position , boids [ i ] . position ) ;
188
+ let neighbordist = 50 ;
189
+ let sum = createVector ( 0 , 0 ) ;
190
+ let count = 0 ;
191
+ for ( let i = 0 ; i < boids . length ; i ++ ) {
192
+ let d = p5 . Vector . dist ( this . position , boids [ i ] . position ) ;
193
193
if ( ( d > 0 ) && ( d < neighbordist ) ) {
194
194
sum . add ( boids [ i ] . velocity ) ;
195
195
count ++ ;
@@ -199,22 +199,22 @@ Boid.prototype.align = function(boids) {
199
199
sum . div ( count ) ;
200
200
sum . normalize ( ) ;
201
201
sum . mult ( this . maxspeed ) ;
202
- var steer = p5 . Vector . sub ( sum , this . velocity ) ;
202
+ let steer = p5 . Vector . sub ( sum , this . velocity ) ;
203
203
steer . limit ( this . maxforce ) ;
204
204
return steer ;
205
205
} else {
206
- return createVector ( 0 , 0 ) ;
206
+ return createVector ( 0 , 0 ) ;
207
207
}
208
208
}
209
209
210
210
// Cohesión
211
211
// Para la ubicación promedio (centro) de todos los boids cercanos, calcula el vector de viraje hacia esa ubicación.
212
212
Boid . prototype . cohesion = function ( boids ) {
213
- var neighbordist = 50 ;
214
- var sum = createVector ( 0 , 0 ) ; // Empieza con un vector vacío para acumular todas las posiciones
215
- var count = 0 ;
216
- for ( var i = 0 ; i < boids . length ; i ++ ) {
217
- var d = p5 . Vector . dist ( this . position , boids [ i ] . position ) ;
213
+ let neighbordist = 50 ;
214
+ let sum = createVector ( 0 , 0 ) ; // Empieza con un vector vacío para acumular todas las posiciones
215
+ let count = 0 ;
216
+ for ( let i = 0 ; i < boids . length ; i ++ ) {
217
+ let d = p5 . Vector . dist ( this . position , boids [ i ] . position ) ;
218
218
if ( ( d > 0 ) && ( d < neighbordist ) ) {
219
219
sum . add ( boids [ i ] . position ) ; // Añada posición
220
220
count ++ ;
@@ -224,6 +224,6 @@ Boid.prototype.cohesion = function(boids) {
224
224
sum . div ( count ) ;
225
225
return this . seek ( sum ) ; // Vira hacia la posición
226
226
} else {
227
- return createVector ( 0 , 0 ) ;
227
+ return createVector ( 0 , 0 ) ;
228
228
}
229
229
}
0 commit comments