ruby-processing 2.6.2 → 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rpextras.jar +0 -0
  3. data/lib/ruby-processing.rb +5 -8
  4. data/lib/ruby-processing/app.rb +26 -27
  5. data/lib/ruby-processing/config.rb +0 -2
  6. data/lib/ruby-processing/exporters/application_exporter.rb +1 -1
  7. data/lib/ruby-processing/exporters/base_exporter.rb +1 -1
  8. data/lib/ruby-processing/exporters/creator.rb +11 -11
  9. data/lib/ruby-processing/helper_methods.rb +33 -31
  10. data/lib/ruby-processing/helpers/numeric.rb +1 -1
  11. data/lib/ruby-processing/helpers/range.rb +11 -0
  12. data/lib/ruby-processing/library_loader.rb +11 -16
  13. data/lib/ruby-processing/runner.rb +22 -24
  14. data/lib/ruby-processing/runners/watch.rb +1 -1
  15. data/lib/ruby-processing/version.rb +1 -1
  16. data/library/boids/boids.rb +39 -70
  17. data/library/control_panel/control_panel.rb +3 -3
  18. data/samples/contributed/circle_collision.rb +3 -3
  19. data/samples/contributed/fern.rb +7 -10
  20. data/samples/contributed/fibonacci_sphere.rb +2 -2
  21. data/samples/contributed/full_screen.rb +23 -22
  22. data/samples/contributed/grapher.rb +3 -3
  23. data/samples/contributed/gravity.rb +1 -1
  24. data/samples/contributed/mandelbrot.rb +10 -13
  25. data/samples/external_library/java_processing/box2d_processing/bumpy_surface_noise.rb +1 -1
  26. data/samples/external_library/java_processing/box2d_processing/library/custom_shape/custom_shape.rb +2 -2
  27. data/samples/external_library/java_processing/box2d_processing/library/particle_system/particle_system.rb +1 -1
  28. data/samples/external_library/java_processing/box2d_processing/library/surface/surface.rb +2 -2
  29. data/samples/external_library/java_processing/box2d_processing/liquidy.rb +1 -1
  30. data/samples/external_library/java_processing/fisica/bubbles.rb +2 -2
  31. data/samples/external_library/java_processing/fisica/joints.rb +4 -4
  32. data/samples/external_library/java_processing/generative_design/node_spring_attractor.rb +2 -2
  33. data/samples/external_library/java_processing/geomerative/hello_world_rotate_firstletter.rb +7 -7
  34. data/samples/external_library/java_processing/hemesh/twin_iso.rb +3 -3
  35. data/samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb +1 -1
  36. data/samples/external_library/java_processing/pbox2d/library/custom_shape/custom_shape.rb +2 -2
  37. data/samples/external_library/java_processing/pbox2d/library/particle_system/particle_system.rb +1 -1
  38. data/samples/external_library/java_processing/pbox2d/library/surface/surface.rb +2 -2
  39. data/samples/external_library/java_processing/pbox2d/liquidy.rb +1 -1
  40. data/samples/external_library/java_processing/peasy_cam/library/hilbert/hilbert.rb +4 -4
  41. data/samples/external_library/ruby_gem/draw_test.rb +2 -2
  42. data/samples/external_library/ruby_gem/game_of_life.rb +3 -3
  43. data/samples/processing_app/basics/arrays/array_objects.rb +2 -2
  44. data/samples/processing_app/basics/color/hue.rb +1 -1
  45. data/samples/processing_app/basics/control/conditionals1.rb +5 -24
  46. data/samples/processing_app/basics/control/conditionals2.rb +1 -1
  47. data/samples/processing_app/basics/form/bezier_ellipse.rb +1 -1
  48. data/samples/processing_app/basics/form/brick_tower.rb +4 -8
  49. data/samples/processing_app/basics/form/regular_polygon.rb +1 -1
  50. data/samples/processing_app/basics/form/star.rb +1 -1
  51. data/samples/processing_app/basics/form/triangle_strip.rb +1 -1
  52. data/samples/processing_app/basics/input/storing_input.rb +1 -1
  53. data/samples/processing_app/basics/lights/spot.rb +1 -1
  54. data/samples/processing_app/basics/math/additive_wave.rb +2 -2
  55. data/samples/processing_app/basics/math/double_random.rb +2 -2
  56. data/samples/processing_app/basics/math/noise_wave.rb +1 -1
  57. data/samples/processing_app/basics/math/random.rb +1 -1
  58. data/samples/processing_app/basics/objects/module.rb +1 -1
  59. data/samples/processing_app/basics/objects/struct.rb +1 -1
  60. data/samples/processing_app/basics/structure/loop.rb +19 -14
  61. data/samples/processing_app/basics/structure/recursion2.rb +2 -2
  62. data/samples/processing_app/basics/textures/texture3.rb +13 -33
  63. data/samples/processing_app/basics/transform/birds.rb +5 -5
  64. data/samples/processing_app/basics/transform/cubes_in_cube.rb +3 -3
  65. data/samples/processing_app/basics/transform/rotate.rb +1 -1
  66. data/samples/processing_app/basics/typography/kinetic_type.rb +1 -1
  67. data/samples/processing_app/demos/graphics/bezier_patch.rb +34 -34
  68. data/samples/processing_app/demos/graphics/trefoil.rb +1 -1
  69. data/samples/processing_app/demos/graphics/wiggling.rb +5 -5
  70. data/samples/processing_app/demos/performance/esfera.rb +4 -4
  71. data/samples/processing_app/demos/performance/text_rendering.rb +1 -1
  72. data/samples/processing_app/library/dxf/simple_export.rb +3 -3
  73. data/samples/processing_app/library/pdf/complex_3D.rb +11 -11
  74. data/samples/processing_app/library/vecmath/vec2d/bouncing_ball.rb +1 -1
  75. data/samples/processing_app/library/vecmath/vec2d/circle_collision.rb +3 -3
  76. data/samples/processing_app/library/vecmath/vec2d/library/flock/flock.rb +5 -5
  77. data/samples/processing_app/library/vecmath/vec2d/library/particle/{particle_system.rb → particle.rb} +28 -21
  78. data/samples/processing_app/library/vecmath/vec2d/morph.rb +3 -3
  79. data/samples/processing_app/library/vecmath/vec2d/particle_system_pshape.rb +3 -3
  80. data/samples/processing_app/library/vecmath/vec2d/reflection1.rb +2 -2
  81. data/samples/processing_app/library/vecmath/vec2d/seeking_neural.rb +3 -2
  82. data/samples/processing_app/library/vecmath/vec2d/simple_particle_system.rb +1 -1
  83. data/samples/processing_app/library/vecmath/vec3d/frame_of_reference.rb +4 -4
  84. data/samples/processing_app/topics/advanced_data/library/word/word.rb +1 -1
  85. data/samples/processing_app/topics/advanced_data/load_save_XML.rb +1 -1
  86. data/samples/processing_app/topics/advanced_data/load_save_json.rb +1 -1
  87. data/samples/processing_app/topics/advanced_data/load_save_struct_yaml.rb +1 -1
  88. data/samples/processing_app/topics/advanced_data/load_save_yaml.rb +1 -1
  89. data/samples/processing_app/topics/advanced_data/threads_two.rb +1 -2
  90. data/samples/processing_app/topics/cellular_automata/game_of_life.rb +3 -3
  91. data/samples/processing_app/topics/cellular_automata/library/ca/ca.rb +2 -2
  92. data/samples/processing_app/topics/cellular_automata/library/cell/cell.rb +4 -4
  93. data/samples/processing_app/topics/cellular_automata/library/simple_cell/simple_cell.rb +1 -1
  94. data/samples/processing_app/topics/cellular_automata/spore1.rb +1 -1
  95. data/samples/processing_app/topics/cellular_automata/spore2.rb +1 -1
  96. data/samples/processing_app/topics/create_shapes/group_pshape.rb +2 -2
  97. data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +3 -3
  98. data/samples/processing_app/topics/create_shapes/library/polygon/polygon.rb +2 -2
  99. data/samples/processing_app/topics/create_shapes/library/star/star.rb +3 -3
  100. data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +1 -1
  101. data/samples/processing_app/topics/create_shapes/path_pshape.rb +1 -1
  102. data/samples/processing_app/topics/drawing/scribble_plotter.rb +2 -2
  103. data/samples/processing_app/topics/gui/button.rb +16 -16
  104. data/samples/processing_app/topics/gui/rollover.rb +13 -13
  105. data/samples/processing_app/topics/image_processing/blur.rb +2 -2
  106. data/samples/processing_app/topics/image_processing/edge_detection.rb +2 -2
  107. data/samples/processing_app/topics/lsystems/cstest.rb +1 -1
  108. data/samples/processing_app/topics/lsystems/library/stochastic_grammar/stochastic_grammar.rb +1 -1
  109. data/samples/processing_app/topics/motion/bouncy_bubbles.rb +1 -1
  110. data/samples/processing_app/topics/motion/brownian.rb +2 -2
  111. data/samples/processing_app/topics/motion/circle_collision.rb +1 -1
  112. data/samples/processing_app/topics/motion/cubes_in_cube.rb +1 -1
  113. data/samples/processing_app/topics/motion/library/cube/cube.rb +2 -2
  114. data/samples/processing_app/topics/motion/morph.rb +11 -11
  115. data/samples/processing_app/topics/motion/moving_on_curves.rb +4 -4
  116. data/samples/processing_app/topics/motion/puff.rb +4 -4
  117. data/samples/processing_app/topics/motion/reflection1.rb +3 -3
  118. data/samples/processing_app/topics/motion/reflection2.rb +1 -1
  119. data/samples/processing_app/topics/shaders/bw_shader.rb +1 -1
  120. data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +2 -2
  121. data/samples/processing_app/topics/simulate/flocking.rb +6 -6
  122. data/samples/processing_app/topics/simulate/library/flock/flock.rb +33 -41
  123. data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +34 -60
  124. data/samples/processing_app/topics/simulate/simple_particle_system.rb +20 -32
  125. data/samples/processing_app/topics/simulate/smoke_particle_system.rb +45 -57
  126. data/samples/processing_app/topics/simulate/springs.rb +40 -44
  127. metadata +4 -5
  128. data/samples/processing_app/library/vecmath/vec2d/multiple_particle_systems.rb +0 -141
  129. data/samples/processing_app/library/vecmath/vec2d/smoke_particle_system.rb +0 -133
@@ -28,8 +28,8 @@ def draw
28
28
  (1 ... img.height - 1).each do |y|
29
29
  (1 ... img.width - 1).each do |x|
30
30
  sum = 0 # Kernel sum for this pixel
31
- (-1 .. 1).each do |ky|
32
- (-1 .. 1).each do |kx|
31
+ (-1..1).each do |ky|
32
+ (-1..1).each do |kx|
33
33
  # Calculate the adjacent pixel for this kernel point
34
34
  pos = (y + ky) * img.width + (x + kx)
35
35
  # Image is grayscale, red/green/blue are identical
@@ -25,8 +25,8 @@ def draw
25
25
  (1 ... img.height - 1).each do |y|
26
26
  (1 ... img.width - 1).each do |x|
27
27
  sum = 0 # Kernel sum for this pixel
28
- (-1 .. 1).each do |ky|
29
- (-1 .. 1).each do |kx|
28
+ (-1..1).each do |ky|
29
+ (-1..1).each do |kx|
30
30
  # Calculate the adjacent pixel for this kernel point
31
31
  pos = (y + ky)*img.width + (x + kx)
32
32
  # Image is grayscale, red/green/blue are identical
@@ -10,7 +10,7 @@ def setup
10
10
  fill(200, 200, 0)
11
11
  f = create_font('Arial', 16, true)
12
12
  text_font(f)
13
- (0 .. 7).each do |i|
13
+ (0..7).each do |i|
14
14
  grammar = Grammar.new(
15
15
  'baaaaaa',
16
16
  {
@@ -17,7 +17,7 @@ class StochasticGrammar
17
17
  #####################################################
18
18
 
19
19
  def stochastic_rule(rules)
20
- total = rules.inject(0) do |sum, rule_and_weight|
20
+ total = rules.reduce(0) do |sum, rule_and_weight|
21
21
  sum + rule_and_weight[PROB]
22
22
  end
23
23
  srand
@@ -16,7 +16,7 @@ def setup
16
16
  size(640, 360)
17
17
  @balls = []
18
18
  (0 ... NUM_BALLS).each do |i|
19
- balls << Ball.new(rand(width), rand(height), rand(30 .. 70), i, balls)
19
+ balls << Ball.new(rand(width), rand(height), rand(30..70), i, balls)
20
20
  end
21
21
  noStroke
22
22
  fill(255, 204)
@@ -24,8 +24,8 @@ def draw
24
24
  ay[i-1] = ay[i]
25
25
  end
26
26
  # Put a new value at the end of the array
27
- ax[NUM-1] += rand(-RANGE .. RANGE)
28
- ay[NUM-1] += rand(-RANGE .. RANGE)
27
+ ax[NUM-1] += rand(-RANGE..RANGE)
28
+ ay[NUM-1] += rand(-RANGE..RANGE)
29
29
 
30
30
  # Constrain all points to the screen
31
31
  ax[NUM-1] = constrain(ax[NUM-1], 0, width)
@@ -10,7 +10,7 @@ class Ball
10
10
  @position = Vec2D.new(x, y)
11
11
  @r = r
12
12
  @m = r * 0.1
13
- @velocity = Vec2D.new(rand(-3.0 .. 3), rand(-3.0 .. 3))
13
+ @velocity = Vec2D.new(rand(-3.0..3), rand(-3.0..3))
14
14
  end
15
15
 
16
16
  def update
@@ -27,7 +27,7 @@ def setup
27
27
  @cubies = []
28
28
  CUBE_NO.times do
29
29
  # Cubies are randomly sized
30
- cubie_size = rand(5 .. 15)
30
+ cubie_size = rand(5..15)
31
31
  cubies << Cube.new(cubie_size, cubie_size, cubie_size)
32
32
  end
33
33
  smooth 8
@@ -22,7 +22,7 @@ class Cube
22
22
  # Random velocity vector
23
23
  @velocity = Vect.random_3d
24
24
  # Random rotation
25
- @rotation = Vect.new(rand(40 .. 100), rand(40 .. 100), rand(40 .. 100))
25
+ @rotation = Vect.new(rand(40..100), rand(40..100), rand(40..100))
26
26
 
27
27
  # cube composed of 6 quads
28
28
  #front
@@ -116,7 +116,7 @@ end
116
116
  Vect = Struct.new(:x, :y, :z) do
117
117
 
118
118
  def self.random_3d
119
- Vect.new(rand(-1 .. 1.0), rand(-1 .. 1.0), rand(-1 .. 1.0))
119
+ Vect.new(rand(-1..1.0), rand(-1..1.0), rand(-1..1.0))
120
120
  end
121
121
 
122
122
  def add v
@@ -1,6 +1,6 @@
1
1
  #
2
- # Morph.
3
- # see vecmath library for a variant using Vec2D instead of PVector
2
+ # Morph.
3
+ # see vecmath library for a variant using Vec2D instead of PVector
4
4
  # Changing one shape into another by interpolating
5
5
  # vertices from one to another
6
6
  #
@@ -14,10 +14,10 @@ def setup
14
14
  @morph = []
15
15
  @state = false
16
16
  # Create a circle using vectors pointing from center
17
- (0 .. 360).step(9) do |angle|
17
+ (0..360).step(9) do |angle|
18
18
  # Note we are not starting from 0 in order to match the
19
- # path of a circle.
20
- v = PVector.from_angle(radians(angle-135))
19
+ # path of a circle.
20
+ v = PVector.from_angle((angle - 135).radians)
21
21
  v.mult(100)
22
22
  circle << v
23
23
  # Let's fill out morph Array with blank PVectors while we are at it
@@ -26,11 +26,11 @@ def setup
26
26
 
27
27
  # A square is a bunch of vertices along straight lines
28
28
  # Top of square
29
- (-50 .. 50).step(10) do |x|
29
+ (-50..50).step(10) do |x|
30
30
  square << PVector.new(x, -50)
31
31
  end
32
32
  # Right side
33
- (-50 .. 50).step(10) do |y|
33
+ (-50..50).step(10) do |y|
34
34
  square << PVector.new(50, y)
35
35
  end
36
36
  # Bottom, NB: can't negative step ruby so use your loaf
@@ -48,25 +48,25 @@ def draw
48
48
 
49
49
  # We will keep how far the vertices are from their target
50
50
  total_distance = 0
51
-
51
+
52
52
  # Look at each vertex
53
53
  circle.length.times do |i|
54
54
  # Are we lerping to the circle or square?
55
55
  v1 = (state)? circle[i] : square[i]
56
56
  # Get the vertex we will draw
57
57
  v2 = morph[i]
58
-
58
+
59
59
  # Lerp to the target
60
60
  v2.lerp(v1, 0.1)
61
61
  # Check how far we are from target
62
62
  total_distance += PVector.dist(v1, v2)
63
63
  end
64
-
64
+
65
65
  # If all the vertices are close, switch shape
66
66
  if (total_distance < 0.1)
67
67
  @state = !state
68
68
  end
69
-
69
+
70
70
  # Draw relative to center
71
71
  translate(width/2, height/2)
72
72
  stroke_weight(4)
@@ -1,6 +1,6 @@
1
1
  #
2
- # Moving On Curves.
3
- #
2
+ # Moving On Curves.
3
+ #
4
4
  # In this example, the circles moves along the curve y = x^4.
5
5
  # Click the mouse to have it move to a new position.
6
6
  #
@@ -13,7 +13,7 @@ def setup
13
13
  @begin_x = 20.0 # Initial x-coordinate
14
14
  @begin_y = 10.0 # Initial y-coordinate
15
15
  @end_x = 570.0 # Final x-coordinate
16
- @end_y = 320.0 # Final y-coordinate
16
+ @end_y = 320.0 # Final y-coordinate
17
17
  @exponent = 4 # Determines the curve
18
18
  @x = 0.0 # Current x-coordinate
19
19
  @y = 0.0 # Current y-coordinate
@@ -29,7 +29,7 @@ def draw
29
29
  @pct += STEP
30
30
  if (pct < 1.0)
31
31
  @x = begin_x + (pct * dist_x)
32
- @y = begin_y + (pow(pct, exponent) * dist_y)
32
+ @y = begin_y + pct**exponent * dist_y
33
33
  end
34
34
  fill(255)
35
35
  ellipse(x, y, 20, 20)
@@ -21,10 +21,10 @@ def setup
21
21
  @speed_x = 0.7
22
22
  @speed_y = 0.9
23
23
  # Fill body arrays
24
- @radii_x = Array.new(CELLS, rand(-7 .. 7))
25
- @radii_y = Array.new(CELLS, rand(-7 .. 7) )
26
- @frequency = Array.new(CELLS, rand(-7 .. 7) )
27
- @cell_radius= Array.new(CELLS, rand(16 .. 30) )
24
+ @radii_x = Array.new(CELLS, rand(-7..7))
25
+ @radii_y = Array.new(CELLS, rand(-7..7) )
26
+ @frequency = Array.new(CELLS, rand(-7..7) )
27
+ @cell_radius= Array.new(CELLS, rand(16..30) )
28
28
  @angle = Array.new(CELLS, 0)
29
29
  @px = Array.new(CELLS, 0)
30
30
  @py = Array.new(CELLS, 0)
@@ -22,7 +22,7 @@ def setup
22
22
  # start ellipse at middle top of screen
23
23
  @position = Vec2D.new(width/2, 0)
24
24
  # set initial random direction
25
- @direction = Vec2D.new(rand(-1.0 .. 1), rand(-1.0 .. 1))
25
+ @direction = Vec2D.new(rand(-1.0..1), rand(-1.0..1))
26
26
  @velocity = Vec2D.new
27
27
  end
28
28
 
@@ -87,8 +87,8 @@ def draw
87
87
  @position.y = radius
88
88
  @direction.y *= -1
89
89
  # randomize base top
90
- @base1.y = rand(height - 100 .. height)
91
- @base2.y = rand(height - 100 .. height)
90
+ @base1.y = rand(height - 100..height)
91
+ @base2.y = rand(height - 100..height)
92
92
  @coords = create_ground
93
93
  end
94
94
  end
@@ -21,7 +21,7 @@ def setup
21
21
  # Initialize ground peak heights
22
22
  @peakHeights = []
23
23
  (SEGS + 1).times do
24
- @peakHeights << rand(height - 40.0 .. height - 30.0)
24
+ @peakHeights << rand(height - 40.0..height - 30.0)
25
25
  end
26
26
  @ground = []
27
27
 
@@ -26,7 +26,7 @@ def create_can(r, h, detail, tex)
26
26
  sh.begin_shape(QUAD_STRIP)
27
27
  sh.no_stroke
28
28
  sh.texture(tex)
29
- (0 .. detail).each do |i|
29
+ (0..detail).each do |i|
30
30
  angle = TAU / detail
31
31
  x = sin(i * angle)
32
32
  z = cos(i * angle)
@@ -53,10 +53,10 @@ def draw
53
53
  rotate_z(frame_count * 0.005) # dynamic frameCount-based rotation over the Z axis
54
54
 
55
55
  background(0) # black background
56
- perspective(PI/3.0, width.to_f / height, 0.1, 1000000) # perspective for close shapes
56
+ perspective(PI/3.0, width.to_f / height, 0.1, 1_000_000) # perspective for close shapes
57
57
  scale(750) # scale by 750 (the model itself is unit length
58
58
 
59
- displace.set('time', millis / 5000.0) # feed time to the GLSL shader
59
+ displace.set('time', millis / 5_000.0) # feed time to the GLSL shader
60
60
  shader(displace) # use shader
61
61
  shape(height_map) # display the PShape
62
62
 
@@ -1,11 +1,11 @@
1
1
  #
2
- # Flocking
3
- # by Daniel Shiffman.
4
- #
2
+ # Flocking
3
+ # by Daniel Shiffman.
4
+ #
5
5
  # An implementation of Craig Reynold's Boids program to simulate
6
- # the flocking behavior of birds. Each boid steers itself based on
6
+ # the flocking behavior of birds. Each boid steers itself based on
7
7
  # rules of avoidance, alignment, and coherence.
8
- #
8
+ #
9
9
  # Click the mouse to add a new boid.
10
10
  #
11
11
  load_libraries :flock, :vecmath
@@ -17,7 +17,7 @@ def setup
17
17
  @flock = Flock.new
18
18
  # Add an initial set of boids into the system
19
19
  150.times do
20
- flock << Boid.new(width/2, height/2)
20
+ flock << Boid.new(width / 2, height / 2)
21
21
  end
22
22
  end
23
23
 
@@ -1,63 +1,55 @@
1
1
  # The Flock (a list of Boid objects)
2
+ require 'forwardable'
3
+
4
+ class Flock
5
+ include Enumerable
6
+ extend Forwardable
7
+ def_delegators(:@boids, :<<, :each)
2
8
 
3
- class Flock
4
- extend Enumerable
5
-
6
9
  attr_reader :boids
7
-
8
- def initialize
9
- @boids = []
10
- end
11
10
 
12
- def each &block
13
- boids.each &block
14
- end
15
-
16
- def << obj
17
- boids << obj
11
+ def initialize
12
+ @boids = []
18
13
  end
19
14
 
20
15
  def run
21
- self.each do |bird|
16
+ each do |bird|
22
17
  bird.run(self) # Passing the entire list of boids to each boid individually
23
18
  end
24
19
  end
25
-
26
20
  end
27
21
 
28
22
  # The Boid class
29
23
 
30
24
  class Boid
31
25
  include Processing::Proxy
32
-
26
+
33
27
  MAXSPEED = 2
34
28
  MAXFORCE = 0.03
35
-
29
+
36
30
  attr_reader :width, :height
37
- attr_reader :location, :velocity, :acceleration, :sz, :maxforce_squared, :maxspeed_squared
38
-
31
+ attr_reader :location, :velocity, :acceleration, :sz
32
+
39
33
  def initialize(x, y)
40
34
  @width, @height = $app.width, $app.height
41
35
  @acceleration = Vec2D.new(0, 0)
42
- @velocity = Vec2D.new(rand(-1.0 .. 1), rand(-1.0 .. 1))
36
+ @velocity = Vec2D.new(rand(-1.0..1), rand(-1.0..1))
43
37
  @location = Vec2D.new(x, y)
44
38
  @sz = 4.0
45
- @maxspeed_squared = MAXSPEED * MAXSPEED
46
- @maxforce_squared = MAXFORCE * MAXFORCE
47
39
  end
48
-
40
+
49
41
  def run(boids)
50
42
  flock(boids)
51
43
  update
52
44
  borders
53
45
  render
54
46
  end
55
-
47
+
56
48
  def apply_force(force)
57
49
  # We could add mass here if we want A = F / M
58
50
  @acceleration += force
59
51
  end
60
-
52
+
61
53
  # We accumulate a new acceleration each time based on three rules
62
54
  def flock boids
63
55
  sep = separate(boids) # Separation
@@ -72,18 +64,18 @@ class Boid
72
64
  apply_force(ali)
73
65
  apply_force(coh)
74
66
  end
75
-
67
+
76
68
  # Method to update location
77
69
  def update
78
70
  # Update velocity
79
71
  @velocity += acceleration
80
72
  # Limit speed
81
- velocity.set_mag(MAXSPEED) {velocity.mag_squared > maxspeed_squared}
73
+ velocity.set_mag(MAXSPEED) {velocity.mag > MAXSPEED}
82
74
  @location += velocity
83
75
  # Reset accelertion to 0 each cycle
84
76
  @acceleration *= 0
85
77
  end
86
-
78
+
87
79
  # A method that calculates and applies a steering force towards a target
88
80
  # STEER = DESIRED MINUS VELOCITY
89
81
  def seek(target)
@@ -93,13 +85,13 @@ class Boid
93
85
  desired *= MAXSPEED
94
86
  # Steering = Desired minus Velocity
95
87
  steer = desired - velocity
96
- steer.set_mag(MAXFORCE) {steer.mag_squared > maxforce_squared} # Limit to maximum steering force
88
+ steer.set_mag(MAXFORCE) {steer.mag > MAXFORCE} # Limit to maximum steering force
97
89
  steer
98
90
  end
99
91
 
100
92
  def render
101
93
  # Draw a triangle rotated in the direction of velocity
102
- theta = velocity.heading + radians(90)
94
+ theta = velocity.heading + 90.radians
103
95
  fill(200,100)
104
96
  stroke(255)
105
97
  push_matrix
@@ -115,7 +107,7 @@ class Boid
115
107
 
116
108
  # Wraparound
117
109
  def borders
118
- if (location.x < -sz * 0.5)
110
+ if (location.x < -sz * 0.5)
119
111
  location.x = width + sz * 0.5
120
112
  end
121
113
  if (location.y < -sz * 0.5)
@@ -128,7 +120,7 @@ class Boid
128
120
  location.y = -sz * 0.5
129
121
  end
130
122
  end
131
-
123
+
132
124
  # Separation
133
125
  # Method checks for nearby boids and steers away
134
126
  def separate boids
@@ -137,7 +129,7 @@ class Boid
137
129
  count = 0
138
130
  # For every boid in the system, check if it's too close
139
131
  boids.each do |other|
140
- d = Vec2D.dist(location, other.location)
132
+ d = location.dist(other.location)
141
133
  # If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
142
134
  if ((d > 0) && (d < desiredseparation))
143
135
  # Calculate vector pointing away from neighbor
@@ -152,18 +144,18 @@ class Boid
152
144
  if (count > 0)
153
145
  steer /= count.to_f
154
146
  end
155
-
147
+
156
148
  # As long as the vector is greater than 0
157
149
  if (steer.mag > 0)
158
150
  # Implement Reynolds: Steering = Desired - Velocity
159
151
  steer.normalize!
160
152
  steer *= MAXSPEED
161
153
  steer -= velocity
162
- steer.set_mag(MAXFORCE) {steer.mag_squared > maxforce_squared}
154
+ steer.set_mag(MAXFORCE) {steer.mag > MAXFORCE}
163
155
  end
164
156
  return steer
165
157
  end
166
-
158
+
167
159
  # Alignment
168
160
  # For every nearby boid in the system, calculate the average velocity
169
161
  def align boids
@@ -171,7 +163,7 @@ class Boid
171
163
  sum = Vec2D.new(0, 0)
172
164
  count = 0
173
165
  boids.each do |other|
174
- d = Vec2D.dist(location, other.location)
166
+ d = location.dist(other.location)
175
167
  if ((d > 0) && (d < neighbordist))
176
168
  sum += other.velocity
177
169
  count += 1
@@ -182,13 +174,13 @@ class Boid
182
174
  sum.normalize!
183
175
  sum *= MAXSPEED
184
176
  steer = sum - velocity
185
- steer.set_mag(MAXFORCE) {steer.mag_squared > maxforce_squared}
177
+ steer.set_mag(MAXFORCE) {steer.mag > MAXFORCE}
186
178
  return steer
187
179
  else
188
180
  return Vec2D.new
189
181
  end
190
182
  end
191
-
183
+
192
184
  # Cohesion
193
185
  # For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
194
186
  def cohesion boids
@@ -196,14 +188,14 @@ class Boid
196
188
  sum = Vec2D.new # Start with empty vector to accumulate all locations
197
189
  count = 0
198
190
  boids.each do |other|
199
- d = Vec2D.dist(location, other.location)
191
+ d = location.dist(other.location)
200
192
  if ((d > 0) && (d < neighbordist))
201
193
  sum += other.location # Add location
202
194
  count += 1
203
195
  end
204
196
  end
205
197
  sum /= count unless (count == 0) # avoid div by zero
206
- return (count > 0)? seek(sum) : Vec2D.new
198
+ return (count > 0)? seek(sum) : Vec2D.new
207
199
  end
208
200
  end
209
201