ruby-processing 2.4.4 → 2.5.0

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 (227) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rpextras.jar +0 -0
  3. data/lib/ruby-processing.rb +8 -6
  4. data/lib/ruby-processing/app.rb +54 -67
  5. data/lib/ruby-processing/config.rb +7 -7
  6. data/lib/ruby-processing/exporters/application_exporter.rb +37 -38
  7. data/lib/ruby-processing/exporters/base_exporter.rb +34 -37
  8. data/lib/ruby-processing/exporters/creator.rb +27 -25
  9. data/lib/ruby-processing/helper_methods.rb +18 -22
  10. data/lib/ruby-processing/helpers/numeric.rb +5 -6
  11. data/lib/ruby-processing/helpers/string.rb +11 -20
  12. data/lib/ruby-processing/library_loader.rb +33 -35
  13. data/lib/ruby-processing/runner.rb +118 -65
  14. data/lib/ruby-processing/runners/base.rb +21 -26
  15. data/lib/ruby-processing/runners/live.rb +3 -4
  16. data/lib/ruby-processing/runners/run.rb +1 -3
  17. data/lib/ruby-processing/runners/watch.rb +8 -8
  18. data/lib/ruby-processing/version.rb +1 -1
  19. data/lib/templates/application/lib/args.txt.erb +1 -1
  20. data/lib/templates/application/run.erb +3 -3
  21. data/library/control_panel/control_panel.rb +29 -29
  22. data/library/fastmath/fastmath.rb +3 -0
  23. data/library/file_chooser/file_chooser.rb +11 -13
  24. data/library/vecmath/vecmath.rb +6 -35
  25. data/samples/Rakefile +3 -1
  26. data/samples/configRP5/configRP5.pde +150 -0
  27. data/samples/contributed/Rakefile +6 -6
  28. data/samples/contributed/animator.rb +6 -6
  29. data/samples/contributed/bezier_playground.rb +29 -68
  30. data/samples/contributed/circle_collision.rb +6 -13
  31. data/samples/contributed/elegant_ball.rb +14 -17
  32. data/samples/contributed/empathy.rb +8 -5
  33. data/samples/contributed/fibonacci_sphere.rb +10 -49
  34. data/samples/contributed/full_screen.rb +1 -1
  35. data/samples/contributed/grapher.rb +10 -9
  36. data/samples/contributed/gravity.rb +11 -16
  37. data/samples/contributed/jwishy.rb +18 -20
  38. data/samples/contributed/quadraticvertex.rb +26 -26
  39. data/samples/external_library/java_processing/custom/landscape.rb +6 -4
  40. data/samples/external_library/java_processing/hemesh/twin_iso.rb +69 -71
  41. data/samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb +57 -0
  42. data/samples/external_library/java_processing/pbox2d/contact_test.rb +23 -0
  43. data/samples/external_library/java_processing/pbox2d/data/java_args.txt +2 -0
  44. data/samples/external_library/java_processing/pbox2d/library/custom_shape/custom_shape.rb +158 -0
  45. data/samples/external_library/java_processing/pbox2d/library/particle_system/particle_system.rb +212 -0
  46. data/samples/external_library/java_processing/pbox2d/library/surface/surface.rb +154 -0
  47. data/samples/external_library/java_processing/pbox2d/liquidy.rb +58 -0
  48. data/samples/external_library/java_processing/pbox2d/polygons.rb +58 -0
  49. data/samples/external_library/java_processing/toxiclibs/library/vbo/vbo.rb +52 -0
  50. data/samples/external_library/java_processing/toxiclibs/model_align.rb +9 -16
  51. data/samples/processing_app/basics/color/color_wheel.rb +1 -1
  52. data/samples/processing_app/basics/form/regular_polygon.rb +2 -2
  53. data/samples/processing_app/basics/form/rgb_cube.rb +2 -2
  54. data/samples/processing_app/basics/form/shape_primitives.rb +1 -1
  55. data/samples/processing_app/basics/form/star.rb +2 -2
  56. data/samples/processing_app/basics/input/clock.rb +21 -16
  57. data/samples/processing_app/basics/input/storing_input.rb +6 -6
  58. data/samples/processing_app/basics/math/additive_wave.rb +1 -1
  59. data/samples/processing_app/basics/math/sine.rb +2 -2
  60. data/samples/processing_app/basics/math/sine_cosine.rb +1 -1
  61. data/samples/processing_app/basics/math/sine_wave.rb +1 -1
  62. data/samples/processing_app/basics/objects/inheritance.rb +2 -0
  63. data/samples/processing_app/basics/objects/inheritance_two.rb +88 -0
  64. data/samples/processing_app/basics/objects/module.rb +89 -0
  65. data/samples/processing_app/{library/vecmath/drawolver.rb → basics/objects/module_two.rb} +1 -1
  66. data/samples/processing_app/basics/objects/multiple_constructors.rb +5 -0
  67. data/samples/processing_app/basics/objects/struct.rb +38 -0
  68. data/samples/processing_app/basics/structure/recursion2.rb +1 -1
  69. data/samples/processing_app/basics/transform/birds.rb +7 -10
  70. data/samples/processing_app/basics/transform/library/bird/bird.rb +3 -3
  71. data/samples/processing_app/basics/transform/rotate.rb +1 -1
  72. data/samples/processing_app/basics/transform/rotate1.rb +1 -1
  73. data/samples/processing_app/basics/transform/rotate_x_y.rb +1 -1
  74. data/samples/processing_app/demos/graphics/bezier_patch.rb +1 -38
  75. data/samples/processing_app/demos/graphics/trefoil.rb +50 -40
  76. data/samples/processing_app/demos/graphics/wiggling.rb +36 -38
  77. data/samples/processing_app/demos/performance/cubic_grid_retained.rb +14 -14
  78. data/samples/processing_app/demos/performance/esfera.rb +27 -39
  79. data/samples/processing_app/demos/performance/text_rendering.rb +1 -1
  80. data/samples/processing_app/library/fastmath/clock.rb +44 -0
  81. data/samples/processing_app/library/pdf/complex_3D.rb +26 -24
  82. data/samples/processing_app/library/vecmath/{Rakefile → arcball/Rakefile} +6 -6
  83. data/samples/processing_app/library/vecmath/arcball/arcball_box.rb +23 -0
  84. data/samples/processing_app/library/vecmath/arcball/arcball_radius.rb +23 -0
  85. data/samples/processing_app/library/vecmath/arcball/arcball_shape.rb +29 -0
  86. data/samples/processing_app/library/vecmath/{README.txt → vec2d/README.txt} +0 -0
  87. data/samples/processing_app/library/vecmath/vec2d/Rakefile +30 -0
  88. data/samples/processing_app/library/vecmath/{acceleration_with_vectors.rb → vec2d/acceleration_with_vectors.rb} +4 -5
  89. data/samples/processing_app/library/vecmath/{bouncing_ball.rb → vec2d/bouncing_ball.rb} +9 -15
  90. data/samples/processing_app/library/vecmath/{circle_collision.rb → vec2d/circle_collision.rb} +46 -51
  91. data/samples/processing_app/library/vecmath/{data → vec2d/data}/java_args.txt +0 -0
  92. data/samples/processing_app/library/vecmath/{data → vec2d/data}/sprite.png +0 -0
  93. data/samples/processing_app/library/vecmath/{data → vec2d/data}/texture.png +0 -0
  94. data/samples/processing_app/library/vecmath/{flocking.rb → vec2d/flocking.rb} +3 -6
  95. data/samples/processing_app/library/vecmath/{library → vec2d/library}/flock/flock.rb +47 -67
  96. data/samples/processing_app/library/vecmath/{library → vec2d/library}/particle/particle_system.rb +5 -5
  97. data/samples/processing_app/library/vecmath/vec2d/library/tile/tile.rb +85 -0
  98. data/samples/processing_app/library/vecmath/{morph.rb → vec2d/morph.rb} +17 -16
  99. data/samples/processing_app/library/vecmath/{multiple_particle_systems.rb → vec2d/multiple_particle_systems.rb} +11 -28
  100. data/samples/processing_app/library/vecmath/{particle_system_pshape.rb → vec2d/particle_system_pshape.rb} +4 -6
  101. data/samples/processing_app/library/vecmath/vec2d/penrose.rb +71 -0
  102. data/samples/processing_app/library/vecmath/{reflection1.rb → vec2d/reflection1.rb} +6 -6
  103. data/samples/processing_app/library/vecmath/{seeking_neural.rb → vec2d/seeking_neural.rb} +12 -16
  104. data/samples/processing_app/library/vecmath/{simple_particle_system.rb → vec2d/simple_particle_system.rb} +8 -12
  105. data/samples/processing_app/library/vecmath/{smoke_particle_system.rb → vec2d/smoke_particle_system.rb} +8 -13
  106. data/samples/processing_app/library/vecmath/{vector_math.rb → vec2d/vector_math.rb} +0 -0
  107. data/samples/processing_app/library/vecmath/{library/wiggler/wiggler.rb → vec2d/wiggle_pshape.rb} +28 -4
  108. data/samples/processing_app/library/vecmath/vec3d/Rakefile +30 -0
  109. data/samples/processing_app/library/vecmath/vec3d/drawolver.rb +86 -0
  110. data/samples/processing_app/library/vecmath/vec3d/frame_of_reference.rb +103 -0
  111. data/samples/processing_app/library/vecmath/{hilbert_fractal.rb → vec3d/hilbert_fractal.rb} +5 -33
  112. data/samples/processing_app/library/vecmath/vec3d/library/geometry/geometry.rb +3 -0
  113. data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/cylinder.rb +44 -0
  114. data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/mat4.rb +28 -0
  115. data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/plane.rb +60 -0
  116. data/samples/processing_app/library/vecmath/{library → vec3d/library}/hilbert/hilbert.rb +14 -17
  117. data/samples/processing_app/library/vecmath/{retained_menger.rb → vec3d/retained_menger.rb} +20 -55
  118. data/samples/processing_app/topics/advanced_data/Rakefile +6 -6
  119. data/samples/processing_app/topics/advanced_data/counting_words.rb +7 -7
  120. data/samples/processing_app/topics/advanced_data/data/data.xml +0 -15
  121. data/samples/processing_app/topics/advanced_data/data/struct_data.yml +5 -0
  122. data/samples/processing_app/topics/advanced_data/library/bubble/bubble.rb +8 -8
  123. data/samples/processing_app/topics/advanced_data/load_save_XML.rb +41 -42
  124. data/samples/processing_app/topics/advanced_data/load_save_json.rb +11 -11
  125. data/samples/processing_app/topics/advanced_data/load_save_struct_yaml.rb +22 -23
  126. data/samples/processing_app/topics/advanced_data/load_save_table.rb +12 -12
  127. data/samples/processing_app/topics/advanced_data/load_save_yaml.rb +10 -12
  128. data/samples/processing_app/topics/advanced_data/regex.rb +5 -6
  129. data/samples/processing_app/topics/advanced_data/threads.rb +27 -27
  130. data/samples/processing_app/topics/advanced_data/threads_two.rb +3 -2
  131. data/samples/processing_app/topics/advanced_data/word_frequency.rb +18 -16
  132. data/samples/processing_app/topics/advanced_data/yahoo_weather.rb +6 -5
  133. data/samples/processing_app/topics/animation/animated_sprite.rb +9 -7
  134. data/samples/processing_app/topics/animation/sequential.rb +19 -19
  135. data/samples/processing_app/topics/cellular_automata/library/ca/ca.rb +1 -1
  136. data/samples/processing_app/topics/cellular_automata/wolfram.rb +1 -1
  137. data/samples/processing_app/topics/create_shapes/begin_end_contour.rb +1 -1
  138. data/samples/processing_app/topics/create_shapes/library/wiggler/wiggler.rb +4 -4
  139. data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
  140. data/samples/processing_app/topics/create_shapes/path_pshape.rb +1 -1
  141. data/samples/processing_app/topics/create_shapes/polygon_pshape_oo3.rb +1 -1
  142. data/samples/processing_app/topics/create_shapes/polygon_shape.rb +3 -3
  143. data/samples/processing_app/topics/file_io/load_file1.rb +1 -1
  144. data/samples/processing_app/topics/file_io/save_frames.rb +5 -5
  145. data/samples/processing_app/topics/file_io/save_one_image.rb +2 -2
  146. data/samples/processing_app/topics/file_io/tile_images.rb +1 -1
  147. data/samples/processing_app/topics/gui/scrollbar.rb +3 -3
  148. data/samples/processing_app/topics/image_processing/blending.rb +13 -13
  149. data/samples/processing_app/topics/image_processing/blur.rb +1 -1
  150. data/samples/processing_app/topics/image_processing/brightness.rb +1 -1
  151. data/samples/processing_app/topics/image_processing/convolution.rb +1 -1
  152. data/samples/processing_app/topics/image_processing/edge_detection.rb +4 -4
  153. data/samples/processing_app/topics/image_processing/explode.rb +1 -1
  154. data/samples/processing_app/topics/image_processing/extrusion.rb +1 -1
  155. data/samples/processing_app/topics/image_processing/histogram.rb +1 -1
  156. data/samples/processing_app/topics/image_processing/linear_image.rb +1 -1
  157. data/samples/processing_app/topics/image_processing/pixel_array.rb +1 -1
  158. data/samples/processing_app/topics/image_processing/zoom.rb +2 -2
  159. data/samples/processing_app/topics/lsystems/Rakefile +6 -6
  160. data/samples/processing_app/topics/lsystems/chequer.rb +4 -10
  161. data/samples/processing_app/topics/lsystems/csplant.rb +25 -25
  162. data/samples/processing_app/topics/lsystems/cstest.rb +9 -4
  163. data/samples/processing_app/topics/lsystems/david_tour.rb +14 -16
  164. data/samples/processing_app/topics/lsystems/koch.rb +8 -18
  165. data/samples/processing_app/topics/lsystems/library/cs_grammar/cs_grammar.rb +18 -24
  166. data/samples/processing_app/topics/lsystems/library/grammar/grammar.rb +7 -9
  167. data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +75 -94
  168. data/samples/processing_app/topics/lsystems/library/stochastic_grammar/stochastic_grammar.rb +11 -13
  169. data/samples/processing_app/topics/lsystems/mpeano.rb +29 -32
  170. data/samples/processing_app/topics/lsystems/peano.rb +19 -21
  171. data/samples/processing_app/topics/lsystems/penrose.rb +31 -37
  172. data/samples/processing_app/topics/lsystems/penrose_snowflake.rb +10 -15
  173. data/samples/processing_app/topics/lsystems/pentagonal.rb +20 -23
  174. data/samples/processing_app/topics/lsystems/snake_kolam.rb +13 -13
  175. data/samples/processing_app/topics/lsystems/stochastic_test.rb +25 -28
  176. data/samples/processing_app/topics/lsystems/three_d_tree.rb +25 -24
  177. data/samples/processing_app/topics/motion/bouncy_bubbles.rb +17 -17
  178. data/samples/processing_app/topics/motion/cubes_in_cube.rb +1 -3
  179. data/samples/processing_app/topics/motion/library/cube/cube.rb +2 -2
  180. data/samples/processing_app/topics/motion/reflection2.rb +1 -1
  181. data/samples/processing_app/topics/shaders/blur_filter.rb +3 -3
  182. data/samples/processing_app/topics/shaders/bw_shader.rb +3 -3
  183. data/samples/processing_app/topics/shaders/conway.rb +5 -5
  184. data/samples/processing_app/topics/shaders/deform.rb +5 -5
  185. data/samples/processing_app/topics/shaders/edge_detect.rb +3 -5
  186. data/samples/processing_app/topics/shaders/glossy_fish_eye.rb +15 -21
  187. data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +21 -19
  188. data/samples/processing_app/topics/shaders/image_mask.rb +6 -6
  189. data/samples/processing_app/topics/shaders/landscape.rb +4 -4
  190. data/samples/processing_app/topics/shaders/monjori.rb +15 -15
  191. data/samples/processing_app/topics/shaders/nebula.rb +3 -3
  192. data/samples/processing_app/topics/shaders/sep_blur.rb +15 -15
  193. data/samples/processing_app/topics/shaders/toon_shading.rb +2 -2
  194. data/samples/processing_app/topics/simulate/chain.rb +16 -14
  195. data/samples/processing_app/topics/simulate/smoke_particle_system.rb +3 -3
  196. data/samples/processing_app/topics/vectors/acceleration_with_vectors.rb +3 -2
  197. data/samples/processing_app/topics/vectors/bouncing_ball.rb +1 -0
  198. data/samples/processing_app/topics/vectors/vector_math.rb +2 -1
  199. data/vendors/Rakefile +15 -3
  200. metadata +99 -86
  201. data/.gitignore +0 -13
  202. data/.travis.yml +0 -6
  203. data/.yardopts +0 -5
  204. data/CHANGELOG +0 -316
  205. data/CONTRIBUTING.md +0 -35
  206. data/Gemfile +0 -6
  207. data/LICENSE.md +0 -39
  208. data/README.md +0 -119
  209. data/Rakefile +0 -37
  210. data/bin/install_jruby_complete +0 -13
  211. data/lib/ruby-processing/exporters/exporters.rb +0 -7
  212. data/library/vecmath/lib/arcball.rb +0 -64
  213. data/library/vecmath/lib/quaternion.rb +0 -62
  214. data/library/vecmath/lib/vec.rb +0 -232
  215. data/ruby-processing.gemspec +0 -54
  216. data/samples/processing_app/library/pdf/.gitignore +0 -1
  217. data/samples/processing_app/library/vecmath/retained_test.rb +0 -65
  218. data/samples/processing_app/library/vecmath/retained_test2.rb +0 -65
  219. data/samples/processing_app/library/vecmath/wiggle_pshape.rb +0 -22
  220. data/test/README.md +0 -6
  221. data/test/basic.rb +0 -14
  222. data/test/draw_ex.rb +0 -12
  223. data/test/graphics.rb +0 -7
  224. data/test/p2d.rb +0 -14
  225. data/test/p3d.rb +0 -14
  226. data/test/rp5_test.rb +0 -86
  227. data/test/setup_ex.rb +0 -12
@@ -0,0 +1,30 @@
1
+ # Simple demo Rakefile to autorun samples in current directory
2
+ # adjust path to rp5 executable, and or opts as required
3
+
4
+ SAMPLES_DIR = './'
5
+
6
+ desc 'run demo'
7
+ task default: [:demo]
8
+
9
+ desc 'demo'
10
+ task :demo do
11
+ samples_list.shuffle.each{ |sample| run_sample sample }
12
+ end
13
+
14
+ def samples_list
15
+ files = []
16
+ Dir.chdir(SAMPLES_DIR)
17
+ Dir.glob('*.rb').each do |file|
18
+ files << File.join(SAMPLES_DIR, file)
19
+ end
20
+ return files
21
+ end
22
+
23
+ def run_sample(sample_name)
24
+ puts "Running #{sample_name}...quit to run next sample"
25
+ open("|rp5 --nojruby run #{sample_name}", 'r') do |io|
26
+ while l = io.gets
27
+ puts(l.chop)
28
+ end
29
+ end
30
+ end
@@ -5,7 +5,8 @@
5
5
  #
6
6
  # Demonstration of the basics of motion with vector.
7
7
  # A "Mover" object stores location, velocity, and acceleration as vectors
8
- # The motion is controlled by affecting the acceleration (in this case towards the mouse)
8
+ # The motion is controlled by affecting the acceleration (in this case
9
+ # towards the mouse)
9
10
  #
10
11
  # For more examples of simulating motion and physics with vectors, see
11
12
  # Simulate/ForcesWithVectors, Simulate/GravitationalAttraction3D
@@ -16,13 +17,12 @@ load_library 'vecmath'
16
17
  attr_reader :mover
17
18
 
18
19
  def setup
19
- size(640,360)
20
+ size(640, 360)
20
21
  @mover = Mover.new(width, height)
21
22
  end
22
23
 
23
24
  def draw
24
25
  background(0)
25
-
26
26
  # Update the location
27
27
  mover.update
28
28
  # Display the Mover
@@ -59,7 +59,7 @@ class Mover
59
59
  # Limit the velocity to topspeed, PVector has a limit function
60
60
  # here we supply a block that evaluates to a boolean to set
61
61
  # the velocit conditionally
62
- velocity.set_mag topspeed {velocity.mag_squared > topspeed**2}
62
+ velocity.set_mag(topspeed) { velocity.mag > topspeed }
63
63
  # Location changes by velocity vector
64
64
  @location += velocity
65
65
  end
@@ -70,6 +70,5 @@ class Mover
70
70
  fill(127)
71
71
  ellipse(location.x, location.y, 48, 48)
72
72
  end
73
-
74
73
  end
75
74
 
@@ -8,44 +8,38 @@
8
8
  # See AccelerationWithVectors for an example of how to simulate motion using vectors in an object
9
9
  #
10
10
  load_library :vecmath
11
+ RADIUS = 24
11
12
 
12
13
  attr_reader :loc, # Location of shape
13
14
  :velocity, # Velocity of shape
14
15
  :gravity # Gravity acts at the shape's acceleration
15
16
 
16
17
  def setup
17
- size(640,360)
18
+ size(640, 360)
18
19
  smooth 4
19
- @loc = Vec2D.new(100,100)
20
- @velocity = Vec2D.new(1.5,2.1)
21
- @gravity = Vec2D.new(0,0.2)
20
+ @loc = Vec2D.new(100, 100)
21
+ @velocity = Vec2D.new(1.5, 2.1)
22
+ @gravity = Vec2D.new(0, 0.2)
22
23
 
23
24
  end
24
25
 
25
26
  def draw
26
27
  background(0)
27
-
28
28
  # Add velocity to the location.
29
29
  @loc += velocity
30
30
  # Add gravity to velocity
31
31
  @velocity += gravity
32
-
33
32
  # Bounce off edges
34
- if ((loc.x > width) || (loc.x < 0))
35
- velocity.x *= -1
36
- end
37
- if (loc.y > height)
33
+ velocity.x *= -1 unless (RADIUS .. width - RADIUS).include?(loc.x)
34
+ if loc.y > (height - RADIUS)
38
35
  # We're reducing velocity ever so slightly
39
36
  # when it hits the bottom of the window
40
37
  velocity.y *= -0.95
41
- loc.y = height
38
+ loc.y = height - RADIUS
42
39
  end
43
-
44
40
  # Display circle at location vector
45
41
  stroke(255)
46
42
  stroke_weight(2)
47
43
  fill(127)
48
- ellipse(loc.x,loc.y,48,48)
44
+ ellipse(loc.x, loc.y, RADIUS * 2, RADIUS * 2)
49
45
  end
50
-
51
-
@@ -6,30 +6,30 @@ load_library :vecmath
6
6
  # This inner class demonstrates the use of Ruby-Processing's emulation of
7
7
  # Java inner classes. The Balls are able to call Processing::App methods.
8
8
  class Ball
9
- attr_accessor :x, :y, :r, :m, :vec
10
- def initialize(r = 0.0, vec = nil, x = 0.0, y = 0.0)
11
- @x, @y, @r = x, y, r
9
+ attr_accessor :position, :r, :m, :velocity, :current
10
+ def initialize(r = 0.0, velocity = nil, position = Vec2D.new)
11
+ @position, @velocity, @r = position, velocity, r
12
12
  @m = r * 0.1
13
- @vec = vec
14
13
  end
15
14
 
16
15
  def move
17
- @x += @vec.x
18
- @y += @vec.y
16
+ @position += velocity
19
17
  end
20
18
 
21
19
  def draw
22
- r = @r * 2
23
- ellipse @x, @y, r, r
24
- @px, @py = @x, @y
20
+ d = r * 2
21
+ ellipse position.x, position.y, d, d
22
+ @current = position.copy
25
23
  end
26
24
 
27
25
  def erase
28
- r = @r * 2
29
- rect @px, @py, r, r
26
+ d = r * 2
27
+ rect current.x, current.y, d, d
30
28
  end
31
29
  end
32
30
 
31
+ attr_reader :balls
32
+
33
33
 
34
34
  def setup
35
35
  size 400, 400
@@ -37,8 +37,8 @@ def setup
37
37
  frame_rate 30
38
38
  rect_mode RADIUS
39
39
  @balls = []
40
- 5.times { @balls << Ball.new(10, Vec2D.new(2.15, -1.35), *empty_space(15)) }
41
- 2.times { @balls << Ball.new(40, Vec2D.new(-1.65, 0.42), *empty_space(45)) }
40
+ 5.times { balls << Ball.new(10, Vec2D.new(2.15, -1.35), empty_space(15)) }
41
+ 2.times { balls << Ball.new(40, Vec2D.new(-1.65, 0.42), empty_space(45)) }
42
42
  @frame_time = nil
43
43
  @frame_count = 0
44
44
  end
@@ -55,12 +55,12 @@ def draw
55
55
  background 51
56
56
  else
57
57
  fill 51
58
- @balls.each { |ball| ball.erase }
58
+ balls.each { |ball| ball.erase }
59
59
  end
60
60
 
61
61
  # move the balls
62
62
  fill 240
63
- @balls.each do |ball|
63
+ balls.each do |ball|
64
64
  ball.move
65
65
  ball.draw
66
66
  check_boundary_collision ball
@@ -70,21 +70,17 @@ end
70
70
 
71
71
 
72
72
  def empty_space(r)
73
- x = y = nil
74
- while !x || !empty_space?(x, y, r) do
75
- x = rand(width - 3 * r) + r
76
- y = rand(height - 3 * r) + r
73
+ pos = nil
74
+ while !pos || !empty_space?(pos, r) do
75
+ pos = Vec2D.new(rand(r .. width - r), rand(r .. height - r))
77
76
  end
78
- return x, y
77
+ return pos
79
78
  end
80
79
 
81
80
 
82
- def empty_space?(x, y, r)
83
- @balls.each do |ball|
84
- vx = x - ball.x
85
- vy = y - ball.y
86
- mag = sqrt(vx * vx + vy * vy)
87
- return false if mag < r + ball.r
81
+ def empty_space?(position, r)
82
+ balls.each do |ball|
83
+ return false if position.dist(ball.position) < r + ball.r
88
84
  end
89
85
  return true
90
86
  end
@@ -92,14 +88,14 @@ end
92
88
 
93
89
  def check_object_collisions
94
90
 
95
- (0...(@balls.length)).each do |ia|
96
- ((ia+1)...(@balls.length)).each do |ib|
91
+ (0...(balls.length)).each do |ia|
92
+ ((ia+1)...(balls.length)).each do |ib|
97
93
 
98
- ba = @balls[ia]
99
- bb = @balls[ib]
94
+ ba = balls[ia]
95
+ bb = balls[ib]
100
96
 
101
97
  # get distances between the balls components
102
- bVect = Vec2D.new( bb.x - ba.x, bb.y - ba.y)
98
+ bVect = bb.position - ba.position
103
99
  # calculate magnitude of the vector separating the balls
104
100
  bVectMag = bVect.mag
105
101
  next if bVectMag >= ba.r + bb.r
@@ -118,15 +114,15 @@ def check_object_collisions
118
114
  # bTemp[0].x and bTemp[0].y will initialize
119
115
  # automatically to 0.0, which is what you want
120
116
  # since bb will rotate around ba
121
- bTemp[1].x = cosine * bVect.x + sine * bVect.y
122
- bTemp[1].y = cosine * bVect.y - sine * bVect.x
117
+ bTemp[1].position.x = cosine * bVect.x + sine * bVect.y
118
+ bTemp[1].position.y = cosine * bVect.y - sine * bVect.x
123
119
 
124
120
  # rotate Temporary velocities
125
121
  vTemp = [Vec2D.new, Vec2D.new]
126
- vTemp[0].x = cosine * ba.vec.x + sine * ba.vec.y
127
- vTemp[0].y = cosine * ba.vec.y - sine * ba.vec.x
128
- vTemp[1].x = cosine * bb.vec.x + sine * bb.vec.y
129
- vTemp[1].y = cosine * bb.vec.y - sine * bb.vec.x
122
+ vTemp[0].x = cosine * ba.velocity.x + sine * ba.velocity.y
123
+ vTemp[0].y = cosine * ba.velocity.y - sine * ba.velocity.x
124
+ vTemp[1].x = cosine * bb.velocity.x + sine * bb.velocity.y
125
+ vTemp[1].y = cosine * bb.velocity.y - sine * bb.velocity.x
130
126
 
131
127
  # Now that velocities are rotated, you can use 1D
132
128
  # conservation of momentum equations to calculate
@@ -144,22 +140,21 @@ def check_object_collisions
144
140
  # in the opposite direction
145
141
  # rotate balls
146
142
  bFinal = [Ball.new, Ball.new]
147
- bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y
148
- bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x
149
- bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y
150
- bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x
143
+ bFinal[0].position.x = cosine * bTemp[0].position.x - sine * bTemp[0].position.y
144
+ bFinal[0].position.y = cosine * bTemp[0].position.y + sine * bTemp[0].position.x
145
+ bFinal[1].position.x = cosine * bTemp[1].position.x - sine * bTemp[1].position.y
146
+ bFinal[1].position.y = cosine * bTemp[1].position.y + sine * bTemp[1].position.x
151
147
 
152
148
  # update balls to screen position
153
- bb.x = ba.x + bFinal[1].x
154
- bb.y = ba.y + bFinal[1].y
155
- ba.x = ba.x + bFinal[0].x
156
- ba.y = ba.y + bFinal[0].y
149
+ bb.position = ba.position + bFinal[1].position
150
+ ba.position = ba.position + bFinal[0].position
151
+
157
152
 
158
153
  # update velocities
159
- ba.vec.x = cosine * vFinal[0].x - sine * vFinal[0].y
160
- ba.vec.y = cosine * vFinal[0].y + sine * vFinal[0].x
161
- bb.vec.x = cosine * vFinal[1].x - sine * vFinal[1].y
162
- bb.vec.y = cosine * vFinal[1].y + sine * vFinal[1].x
154
+ ba.velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y
155
+ ba.velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x
156
+ bb.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y
157
+ bb.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x
163
158
  end
164
159
  end
165
160
 
@@ -167,8 +162,8 @@ end
167
162
 
168
163
  # reverse ball velocity if at sketch boundary
169
164
  def check_boundary_collision(ball)
170
- ball.vec.x *= -1 unless (ball.r .. width - ball.r).include? ball.x
171
- ball.vec.y *= -1 unless (ball.r .. height - ball.r).include? ball.y
165
+ ball.velocity.x *= -1 unless (ball.r .. width - ball.r).include? ball.position.x
166
+ ball.velocity.y *= -1 unless (ball.r .. height - ball.r).include? ball.position.y
172
167
  end
173
168
 
174
169
 
@@ -15,11 +15,8 @@ attr_reader :flock
15
15
 
16
16
  def setup
17
17
  size(640, 360)
18
- @flock = Flock.new
19
- # Add an initial set of boids into the system
20
- 150.times do
21
- flock << Boid.new(width/2, height/2)
22
- end
18
+ # Add an initial set of boids into the system at the center
19
+ @flock = Flock.new(150, Vec2D.new(width / 2.0, height / 2.0))
23
20
  end
24
21
 
25
22
  def draw
@@ -29,5 +26,5 @@ end
29
26
 
30
27
  # Add a new boid into the System
31
28
  def mouse_pressed
32
- flock << Boid.new(mouse_x, mouse_y)
29
+ flock << Boid.new(Vec2D.new(mouse_x, mouse_y))
33
30
  end
@@ -1,24 +1,13 @@
1
1
  # The Flock (a list of Boid objects)
2
+ require 'forwardable'
2
3
 
3
4
  class Flock
4
- extend Enumerable
5
+ extend Forwardable
6
+ def_delegators(:@boids, :each, :<<, :reject)
7
+ include Enumerable
5
8
 
6
- attr_reader :boids
7
-
8
- def initialize
9
- @boids = []
10
- end
11
-
12
- def each &block
13
- boids.each &block
14
- end
15
-
16
- def reject &block
17
- boids &block
18
- end
19
-
20
- def << obj
21
- boids << obj
9
+ def initialize(size, position)
10
+ @boids = (0 .. size).map{ Boid.new(position) }
22
11
  end
23
12
 
24
13
  def run
@@ -35,14 +24,16 @@ class Boid
35
24
  include Processing::Proxy
36
25
  import 'vecmath'
37
26
  attr_reader :location, :velocity, :acceleration, :r, :maxforce, :maxspeed
38
-
39
- def initialize(x, y)
27
+ attr_reader :width, :height
28
+ def initialize(loc)
40
29
  @acceleration = Vec2D.new
41
30
  @velocity = Vec2D.new(rand(-1.0 .. 1), rand(-1.0 .. 1))
42
- @location = Vec2D.new(x, y)
31
+ @location = loc
43
32
  @r = 2.0
44
33
  @maxspeed = 2
45
34
  @maxforce = 0.03
35
+ @width = $app.width
36
+ @height = $app.height
46
37
  end
47
38
 
48
39
  def run(boids)
@@ -58,7 +49,7 @@ class Boid
58
49
  end
59
50
 
60
51
  # We accumulate a new acceleration each time based on three rules
61
- def flock boids
52
+ def flock(boids)
62
53
  sep = separate(boids) # Separation
63
54
  ali = align(boids) # Alignment
64
55
  coh = cohesion(boids) # Cohesion
@@ -76,8 +67,8 @@ class Boid
76
67
  def update
77
68
  # Update velocity
78
69
  @velocity += acceleration
79
- # Limit speed
80
- velocity.set_mag(maxspeed) {velocity.mag_squared > maxspeed**2}
70
+ # Limit speed
71
+ velocity.set_mag(maxspeed) { velocity.mag > maxspeed }
81
72
  @location += velocity
82
73
  # Reset accelertion to 0 each cycle
83
74
  @acceleration *= 0
@@ -93,86 +84,79 @@ class Boid
93
84
  # Steering = Desired minus Velocity
94
85
  steer = desired - velocity
95
86
  # Limit to maximum steering force
96
- steer.set_mag(maxforce) {steer.mag_squared > maxforce**2}
87
+ steer.set_mag(maxforce) { steer.mag > maxforce }
97
88
  return steer
98
89
  end
99
90
 
100
91
  def render
101
92
  # Draw a triangle rotated in the direction of velocity
102
- theta = velocity.heading + Math::PI/2.0
103
- fill(200,100)
93
+ theta = velocity.heading + Math::PI/2
94
+ fill(200, 100)
104
95
  stroke(255)
105
96
  push_matrix
106
97
  translate(location.x,location.y)
107
98
  rotate(theta)
108
99
  begin_shape(TRIANGLES)
109
- vertex(0, -r*2)
110
- vertex(-r, r*2)
111
- vertex(r, r*2)
100
+ vertex(0, -r * 2)
101
+ vertex(-r, r * 2)
102
+ vertex(r, r * 2)
112
103
  end_shape
113
104
  pop_matrix
114
105
  end
115
106
 
116
107
  # Wraparound
117
108
  def borders
118
- if (location.x < -r)
119
- location.x = $app.width+r
120
- end
121
- if (location.y < -r)
122
- location.y = $app.height+r
123
- end
124
- if (location.x > $app.width+r)
125
- location.x = -r
126
- end
127
- if (location.y > $app.height+r)
128
- location.y = -r
129
- end
109
+ location.x = width + r if location.x < -r
110
+ location.y = height + r if location.y < -r
111
+ location.x = -r if location.x > width + r
112
+ location.y = -r if location.y > height + r
130
113
  end
131
114
 
132
115
  # Separation
133
116
  # Method checks for nearby boids and steers away
134
- def separate boids
117
+ def separate(boids)
135
118
  desiredseparation = 25.0
136
119
  steer = Vec2D.new
137
120
  count = 0
138
121
  # For every other bird in the system, check if it's too close
139
- boids.reject{|bd| bd.equal? self}.each do |other|
140
- d = Vec2D.dist(location, other.location)
122
+ boids.reject{ |bd| bd.equal? self }.each do |other|
123
+ d = location.dist(other.location)
141
124
  # If the distance is greater than 0 and less than an arbitrary amount
142
- if ((d > 0) && (d < desiredseparation))
125
+ if (0.0001 .. desiredseparation).include? d
143
126
  # Calculate vector pointing away from neighbor
144
127
  diff = location - other.location
145
128
  diff.normalize!
146
- diff /= d # Weight by distance
129
+ diff /= d # Weight by distance
147
130
  steer += diff
148
131
  count += 1 # Keep track of how many
149
132
  end
150
133
  end
151
134
  # Average -- divide by how many
152
- if (count > 0)
135
+ if count > 0
153
136
  steer /= count.to_f
154
137
  end
155
138
 
156
139
  # As long as the vector is greater than 0
157
- if (steer.mag > 0)
140
+ if steer.mag > 0
158
141
  # Implement Reynolds: Steering = Desired - Velocity
159
142
  steer.normalize!
160
143
  steer *= maxspeed
161
144
  steer -= velocity
162
- steer.set_mag(maxforce){steer.mag_squared > maxforce**2}
145
+ steer.set_mag(maxforce) { steer.mag > maxforce }
163
146
  end
164
147
  return steer
165
148
  end
166
149
 
167
150
  # Alignment
168
151
  # For every other nearby boid in the system, calculate the average velocity
169
- def align boids
152
+ def align(boids)
170
153
  neighbordist = 50
171
154
  sum = Vec2D.new
155
+ steer = Vec2D.new
172
156
  count = 0
173
- boids.reject{|bd| bd.equal? self}.each do |other|
174
- d = Vec2D.dist_squared(location, other.location)
175
- if ((d > 0) && (d < neighbordist * neighbordist))
157
+ boids.reject{ |bd| bd.equal? self }.each do |other|
158
+ d = location.dist(other.location)
159
+ if (0 .. neighbordist).include? d
176
160
  sum += other.velocity
177
161
  count += 1
178
162
  end
@@ -182,30 +166,26 @@ class Boid
182
166
  sum.normalize!
183
167
  sum *= maxspeed
184
168
  steer = sum - velocity
185
- steer.set_mag(maxforce){steer.mag_squared > maxforce**2}
186
- return steer
187
- else
188
- return Vec2D.new
169
+ steer.set_mag(maxforce) { steer.mag > maxforce }
189
170
  end
171
+ return steer
190
172
  end
191
173
 
192
174
  # Cohesion
193
- # For the average location (i.e. center) of all other nearby boids, calculate steering vector towards that location
194
- def cohesion boids
175
+ # For the average location (i.e. center) of all other nearby boids, calculate
176
+ # steering vector towards that location
177
+ def cohesion(boids)
195
178
  neighbordist = 50
196
179
  sum = Vec2D.new # Start with empty vector to accumulate all locations
197
180
  count = 0
198
- boids.reject{|bd| bd.equal? self}.each do |other|
199
- d = Vec2D.dist_squared(location, other.location)
200
- if ((d > 0) && (d < neighbordist * neighbordist))
181
+ boids.reject { |bd| bd.equal? self }.each do |other|
182
+ d = location.dist(other.location)
183
+ if (0.0001 .. neighbordist).include? d
201
184
  sum += other.location # Add location
202
185
  count += 1
203
186
  end
204
187
  end
205
- sum /= count unless (count == 0) # avoid div by zero
206
- return (count > 0)? seek(sum) : Vec2D.new
188
+ sum /= count unless count == 0 # avoid div by zero
189
+ (count > 0) ? seek(sum) : Vec2D.new
207
190
  end
208
191
  end
209
-
210
-
211
-