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
@@ -56,17 +56,17 @@ class Particle
56
56
  s_shape.no_stroke
57
57
  s_shape.texture(sprite)
58
58
  s_shape.normal(0, 0, 1)
59
- s_shape.vertex(-part_size/2, -part_size/2, 0, 0)
60
- s_shape.vertex(+part_size/2, -part_size/2, sprite.width, 0)
61
- s_shape.vertex(+part_size/2, +part_size/2, sprite.width, sprite.height)
62
- s_shape.vertex(-part_size/2, +part_size/2, 0, sprite.height)
59
+ s_shape.vertex(-part_size / 2.0, -part_size / 2.0, 0, 0)
60
+ s_shape.vertex(+part_size / 2.0, -part_size / 2.0, sprite.width, 0)
61
+ s_shape.vertex(+part_size / 2.0, +part_size / 2.0, sprite.width, sprite.height)
62
+ s_shape.vertex(-part_size / 2.0, +part_size / 2.0, 0, sprite.height)
63
63
  s_shape.end_shape
64
64
 
65
65
  # Initialize center vector
66
66
  @center = Vec2D.new
67
67
 
68
68
  # Set the particle starting location
69
- rebirth(width/2, height/2)
69
+ rebirth(width / 2.0, height / 2.0)
70
70
  end
71
71
 
72
72
  def rebirth(x, y)
@@ -0,0 +1,85 @@
1
+ module Tiler
2
+ @acute = false
3
+
4
+ def self.acute(x)
5
+ @acute = x
6
+ end
7
+
8
+ # setup the initial tiling with all red tiles
9
+ def self.tile(a, b, c)
10
+ tile = @acute ? ATile.new(0, a, b, c) : Tile.new(0, a, b, c)
11
+ end
12
+ end
13
+
14
+ class Tile
15
+ include Processing::Proxy
16
+ PHI = (1.0 + Math.sqrt(5)) / 2.0 # golden ratio
17
+ RED = [255, 0, 0]
18
+ BLUE = [0, 0, 255]
19
+ COLORS = [RED, BLUE]
20
+ attr_reader :a, :b, :c, :col
21
+
22
+ def initialize(col, a, b, c)
23
+ @col, @a, @b, @c = col, a, b, c
24
+ end
25
+
26
+ def display
27
+ no_stroke
28
+ fill(*COLORS[col])
29
+ triangle(a.x, a.y, b.x, b.y, c.x, c.y)
30
+ # fill(0, 0, 255)
31
+ # ellipse(a.x, a.y, 4,4)
32
+ # ellipse(b.x, b.y, 4,4)
33
+ # ellipse(c.x, c.y, 4,4)
34
+ end
35
+
36
+ def subdivide
37
+ result = []
38
+ if (col == 0)
39
+ # Subdivide red triangle
40
+ p = b - a
41
+ p /= PHI
42
+ p += a
43
+ result << Tile.new(0, c, p, b)
44
+ result << Tile.new(1, p, c, a)
45
+ else
46
+ # Subdivide blue triangle
47
+ q = a - b
48
+ q /= PHI
49
+ q += b
50
+ r = c - b
51
+ r /= PHI
52
+ r += b
53
+ result << Tile.new(1, r, c, a)
54
+ result << Tile.new(1, q, r, b)
55
+ result << Tile.new(0, r, q, a)
56
+ end
57
+ result
58
+ end
59
+ end
60
+
61
+ class ATile < Tile
62
+ def subdivide
63
+ result = []
64
+ if (col == 0)
65
+ # Subdivide red (half kite) triangle
66
+ q = b - a
67
+ q /= PHI
68
+ q += a
69
+ r = c - b
70
+ r /= PHI
71
+ r += b
72
+ result << ATile.new(1, r, q, b)
73
+ result << ATile.new(0, q, a, r)
74
+ result << ATile.new(0, c, a, r)
75
+ else
76
+ # Subdivide blue (half dart) triangle
77
+ p = a - c
78
+ p /= PHI
79
+ p += c
80
+ result << ATile.new(1, b, p, a)
81
+ result << ATile.new(0, p, c, b)
82
+ end
83
+ result
84
+ end
85
+ end
@@ -1,12 +1,16 @@
1
1
  #
2
- # Morph.
2
+ # Morph.
3
3
  #
4
4
  # Changing one shape into another by interpolating
5
5
  # vertices from one to another
6
6
  #
7
7
  load_library :vecmath
8
8
 
9
- attr_reader :circle, :square, :morph, :state, :v1, :v2
9
+ attr_reader :circle, :square, :morph, :state, :v1, :v2, :renderer
10
+
11
+ ALPHA = Math::PI / 4.0
12
+ OMEGA = TAU + ALPHA
13
+ THETA = Math::PI / 20.0
10
14
 
11
15
  def setup
12
16
  size(640, 360)
@@ -14,19 +18,18 @@ def setup
14
18
  @square = []
15
19
  @morph = []
16
20
  @state = false
17
- frameRate(15)
21
+ @renderer = AppRender.new(self)
22
+ frame_rate(15)
18
23
  # Create a circle using vectors pointing from center
19
- (0 .. TAU).step(Math::PI / 20.0) do |angle|
24
+ (ALPHA .. OMEGA).step(THETA) do |angle|
20
25
  # Note we are not starting from 0 in order to match the
21
- # path of a circle.
22
- v = Vec2D.from_angle(angle + Math::PI / 4.0 )
23
- v *= 100
24
- circle << v
26
+ # path of a circle.
27
+ circle << Vec2D.from_angle(angle) * 100
25
28
  # Let's fill out morph Array with blank Vec2Ds while we are at it
26
29
  morph << Vec2D.new
27
30
  end
28
31
 
29
- # A square is a bunch of vertices along straight lines
32
+ # A square is a bunch of vertices along straight line
30
33
  # Top of square
31
34
  (-50 .. 50).step(10) do |x|
32
35
  square << Vec2D.new(x, -50)
@@ -54,30 +57,28 @@ def draw
54
57
  # Look at each vertex
55
58
  circle.length.times do |i|
56
59
  # Are we lerping to the circle or square?
57
- v1 = (state)? circle[i] : square[i]
60
+ v1 = state ? circle[i] : square[i]
58
61
  # Get the vertex we will draw
59
62
  v2 = morph[i]
60
63
 
61
64
  # Lerp to the target
62
65
  v2.lerp!(v1, 0.1)
63
66
  # Check how far we are from target
64
- @total_distance += Vec2D.dist(v1, v2)
67
+ @total_distance += v1.dist(v2)
65
68
  end
66
69
 
67
70
  # If all the vertices are close, switch shape
68
- if (@total_distance < 0.08)
69
- @state = !state
70
- end
71
+ @state = !state if @total_distance < 0.08
71
72
 
72
73
  # Draw relative to center
73
- translate(width/2, height/2)
74
+ translate(width / 2, height / 2)
74
75
  stroke_weight(4)
75
76
  # Draw a polygon that makes up all the vertices
76
77
  begin_shape
77
78
  no_fill
78
79
  stroke(255)
79
80
  morph.each do |v|
80
- vertex(v.x, v.y)
81
+ v.to_vertex(renderer)
81
82
  end
82
83
  end_shape(CLOSE)
83
84
  end
@@ -4,17 +4,21 @@
4
4
 
5
5
  # Each burst is one instance of a particle system with Particles and
6
6
  # CrazyParticles (a subclass of Particle).
7
+
8
+ require 'forwardable'
7
9
  load_library :vecmath
8
10
 
9
11
  module Runnable
10
12
  def run
11
13
  self.reject! { |item| item.dead? }
12
- self.each { |item| item.run }
14
+ self.each { |item| item.run }
13
15
  end
14
16
  end
15
17
 
16
18
  class ParticleSystem
17
- extend Enumerable
19
+ extend Forwardable
20
+ def_delegators(:@particle_systems, :each, :<<, :reject!, :empty?)
21
+ include Enumerable
18
22
  include Runnable
19
23
 
20
24
  attr_reader :particle_systems
@@ -26,20 +30,8 @@ class ParticleSystem
26
30
  number.times { self << kind.new(origin) }
27
31
  end
28
32
 
29
- def each &block
30
- particle_systems.each &block
31
- end
32
-
33
- def << obj
34
- particle_systems << obj
35
- end
36
-
37
- def reject! &block
38
- particle_systems.reject! &block
39
- end
40
-
41
33
  def dead?
42
- particle_systems.empty?
34
+ self.empty?
43
35
  end
44
36
  end
45
37
 
@@ -48,7 +40,9 @@ def setup
48
40
  size 640, 580
49
41
  color_mode(RGB, 255, 255, 255, 100)
50
42
  ellipse_mode(CENTER)
51
- @particle_systems = ParticleSystem.new(rand(21) + 5, Vec2D.new(width/2, height/2))
43
+ origin = rand(5 .. 16)
44
+ start_pos = Vec2D.new(width / start2, height / 2)
45
+ @particle_systems = ParticleSystem.new(origin, start_pos)
52
46
  end
53
47
 
54
48
  def draw
@@ -57,7 +51,7 @@ def draw
57
51
  end
58
52
 
59
53
  def mouse_pressed
60
- origin = rand(21) + 5
54
+ origin = rand(5 .. 16)
61
55
  vector = Vec2D.new(mouse_x, mouse_y)
62
56
  @particle_systems << ParticleSystem.new(origin, vector)
63
57
  end
@@ -102,18 +96,13 @@ class Particle
102
96
  def render_velocity_vector
103
97
  scale = 10
104
98
  arrow_size = 4
105
-
106
99
  push_matrix
107
-
108
100
  translate(origin.x, origin.y)
109
101
  rotate(velocity.heading)
110
-
111
102
  length = velocity.mag * scale
112
-
113
103
  line 0, 0, length, 0
114
104
  line length, 0, length - arrow_size, arrow_size / 2
115
105
  line length, 0, length - arrow_size, -arrow_size / 2
116
-
117
106
  pop_matrix
118
107
  end
119
108
  end
@@ -143,16 +132,10 @@ class CrazyParticle < Particle
143
132
 
144
133
  def render_rotation_line
145
134
  push_matrix
146
-
147
135
  translate(origin.x, origin.y)
148
136
  rotate(@theta)
149
-
150
137
  stroke(255, lifespan)
151
-
152
138
  line(0, 0, 25, 0)
153
-
154
139
  pop_matrix
155
140
  end
156
141
  end
157
-
158
-
@@ -7,32 +7,30 @@ load_library :vecmath
7
7
  load_library :particle
8
8
 
9
9
  # Particle System object and image
10
- attr_reader :ps
10
+ attr_reader :ps
11
11
 
12
12
  def setup
13
13
  size(640, 360, P2D)
14
14
  # Load the image
15
- sprite = loadImage("sprite.png")
15
+ sprite = loadImage('sprite.png')
16
16
  # A new particle system with 10,000 particles
17
- @ps = ParticleSystem.new(width, height, sprite, 10000)
17
+ @ps = ParticleSystem.new(width, height, sprite, 10_000)
18
18
  # Writing to the depth buffer is disabled to avoid rendering
19
19
  # artifacts due to the fact that the particles are semi-transparent
20
20
  # but not z-sorted.
21
21
  hint(DISABLE_DEPTH_MASK)
22
22
  end
23
23
 
24
- def draw
24
+ def draw
25
25
  background(0)
26
26
  # Update and display system
27
27
  ps.update
28
28
  ps.display
29
29
  # Set the particle system's emitter location to the mouse
30
30
  ps.set_emitter(mouse_x, mouse_y)
31
-
32
31
  # Display frame rate
33
32
  fill(255, 0, 255)
34
33
  text_size(16)
35
34
  text("Frame rate: #{frame_rate.round(1)}", 10, 20)
36
-
37
35
  end
38
36
 
@@ -0,0 +1,71 @@
1
+ # Penrose Tile Generator
2
+ # Using a variant of the "ArrayList" recursion technique: http://natureofcode.com/book/chapter-8-fractals/chapter08_section4
3
+ # Penrose Algorithm from: http://preshing.com/20110831/penrose-tiling-explained
4
+ # Daniel Shiffman May 2013
5
+ # Translated (and refactored) to ruby-processing Jan 2014 by Martin Prout
6
+
7
+ load_libraries :vecmath, :tile, :control_panel
8
+ attr_reader :tris, :s, :panel, :hide, :acute
9
+
10
+ def setup
11
+ size(1024, 576)
12
+ control_panel do |c|
13
+ c.title = 'Tiler Control'
14
+ c.look_feel 'Nimbus'
15
+ c.checkbox :seed
16
+ c.checkbox :acute
17
+ c.button :generate
18
+ c.button :reset!
19
+ @panel = c
20
+ end
21
+ @hide = false
22
+ init false # defaults to regular penrose
23
+ end
24
+
25
+ def draw
26
+ # only make control_panel visible once, or again when hide is false
27
+ unless hide
28
+ @hide = true
29
+ panel.set_visible(hide)
30
+ end
31
+ background(255)
32
+ translate(width / 2, height / 2)
33
+ tris.each do |t|
34
+ t.display
35
+ end
36
+ end
37
+
38
+ def generate
39
+ next_level = []
40
+ tris.each do |t|
41
+ more = t.subdivide
42
+ more.each do |m|
43
+ next_level << m
44
+ end
45
+ end
46
+ @tris = next_level
47
+ end
48
+
49
+ def reset!
50
+ Tiler.acute(acute) # set the Tiler first
51
+ init @seed
52
+ java.lang.System.gc # but does it do any good?
53
+ end
54
+
55
+ def init(alt_seed)
56
+ @tris = []
57
+ 10.times do |i| # create 36 degree segments
58
+ a = Vec2D.new
59
+ b = Vec2D.from_angle((2 * i - 1) * PI / 10)
60
+ c = Vec2D.from_angle((2 * i + 1) * PI / 10)
61
+ b *= 370
62
+ c *= 370
63
+ if alt_seed
64
+ tile = i.even? ? Tiler.tile(b, a, c) : Tiler.tile(c, a, b)
65
+ tris << tile
66
+ else
67
+ tile = i.even? ? Tiler.tile(a, b, c) : Tiler.tile(a, c, b)
68
+ tris << tile
69
+ end
70
+ end
71
+ end
@@ -20,7 +20,7 @@ def setup
20
20
  @base2 = Vec2D.new(width, height)
21
21
  @coords = create_ground
22
22
  # start ellipse at middle top of screen
23
- @position = Vec2D.new(width/2, 0)
23
+ @position = Vec2D.new(width / 2, 0)
24
24
  # set initial random direction
25
25
  @direction = Vec2D.new(rand, rand)
26
26
 
@@ -58,7 +58,7 @@ def draw
58
58
  # detect and handle collision
59
59
  coords.each do |coord|
60
60
  # check distance between ellipse and base top coordinates
61
- if (Vec2D.dist(position, coord) < radius)
61
+ if position.dist(coord) < radius
62
62
 
63
63
  # calculate dot product of incident vector and base top normal
64
64
  dot = incidence.dot(normal)
@@ -73,17 +73,17 @@ def draw
73
73
 
74
74
  # detect boundary collision
75
75
  # right
76
- if (position.x > width - radius)
76
+ if position.x > (width - radius)
77
77
  @position.x = width - radius
78
78
  @direction.x *= -1
79
79
  end
80
80
  # left
81
- if (position.x < radius)
81
+ if position.x < radius
82
82
  @position.x = radius
83
83
  @direction.x *= -1
84
84
  end
85
85
  # top
86
- if (position.y < radius)
86
+ if position.y < radius
87
87
  @position.y = radius
88
88
  @direction.y *= -1
89
89
  # randomize base top
@@ -95,7 +95,7 @@ end
95
95
 
96
96
  def create_ground
97
97
  # calculate length of base top
98
- @base_length = Vec2D.dist(base1, base2)
98
+ @base_length = base1.dist(base2)
99
99
  # fill base top coordinate array
100
100
  coords = []
101
101
  (0 ... base_length.ceil).each do |i|
@@ -9,23 +9,23 @@ module SeekingNeural
9
9
  class Perceptron
10
10
  # Perceptron is created with n weights and learning constant
11
11
  def initialize(n, c)
12
- @weights = Array.new(n){ rand(0 .. 1.0) }
12
+ @weights = Array.new(n) { rand(0 .. 1.0) }
13
13
  @c = c
14
14
  end
15
15
 
16
16
  # Function to train the Perceptron
17
17
  # Weights are adjusted based on vehicle's error
18
18
  def train(forces, error)
19
- trained = @weights.zip(forces.map{|f| f.to_a}
20
- .map{|a, b| (a * error.x + b * error.y) * @c})
21
- .map {|w, c| constrain(w + c, 0.0, 1.0)}
22
- @weights = trained
19
+ trained = @weights.zip(forces.map { |f| f.to_a }
20
+ .map { |a, b| (a * error.x + b * error.y) * @c })
21
+ .map { |w, c| constrain(w + c, 0.0, 1.0) }
22
+ @weights = trained
23
23
  end
24
24
 
25
25
  # Give me a steering result
26
26
  def feedforward(forces)
27
27
  # Sum all values
28
- forces.zip(@weights).map{|a, b| a * b}.inject(Vec2D.new, :+)
28
+ forces.zip(@weights).map { |a, b| a * b }.inject(Vec2D.new, :+)
29
29
  end
30
30
  end
31
31
 
@@ -33,11 +33,10 @@ module SeekingNeural
33
33
  # Daniel Shiffman <http://www.shiffman.net>
34
34
 
35
35
  class Vehicle
36
-
37
36
  MAX_SPEED = 4
38
37
  MAX_FORCE = 0.1
39
- attr_reader :brain, :sz, :location, :targets, :desired
40
- attr_reader :maxforce_squared, :maxspeed_squared
38
+
39
+ attr_reader :brain, :sz, :location, :targets, :desired
41
40
 
42
41
  def initialize(n, x, y)
43
42
  @brain = Perceptron.new(n, 0.001)
@@ -45,8 +44,6 @@ module SeekingNeural
45
44
  @velocity = Vec2D.new
46
45
  @location = Vec2D.new(x, y)
47
46
  @sz = 6.0
48
- @maxspeed_squared = MAX_SPEED * MAX_SPEED
49
- @maxforce_squared = MAX_FORCE * MAX_FORCE
50
47
  end
51
48
 
52
49
  # Method to update location
@@ -54,11 +51,10 @@ module SeekingNeural
54
51
  # Update velocity
55
52
  @velocity += @acceleration
56
53
  # Limit speed
57
- @velocity.set_mag(MAX_SPEED) {@velocity.mag_squared > maxspeed_squared}
54
+ @velocity.set_mag(MAX_SPEED) { @velocity.mag > MAX_SPEED }
58
55
  @location += @velocity
59
56
  # Reset acceleration to 0 each cycle
60
57
  @acceleration *= 0
61
-
62
58
  @location.x = constrain(location.x, 0, width)
63
59
  @location.y = constrain(location.y, 0, height)
64
60
  end
@@ -71,7 +67,7 @@ module SeekingNeural
71
67
  # Here is where the brain processes everything
72
68
  def steer(targets, desired)
73
69
  # Steer towards all targets
74
- forces = targets.map{|target| seek(target) }
70
+ forces = targets.map { |target| seek(target) }
75
71
 
76
72
  # That array of forces is the input to the brain
77
73
  result = brain.feedforward(forces)
@@ -94,7 +90,7 @@ module SeekingNeural
94
90
  desired *= MAX_SPEED
95
91
  # Steering = Desired minus velocity
96
92
  steer = desired - @velocity
97
- steer.set_mag(MAX_FORCE) {steer.mag_squared > maxforce_squared} # Limit to a maximum steering force
93
+ steer.set_mag(MAX_FORCE) { steer.mag > MAX_FORCE } # Limit to a maximum steering force
98
94
  steer
99
95
  end
100
96
 
@@ -127,7 +123,7 @@ attr_reader :targets, :desired, :v
127
123
  def setup
128
124
  size(640, 360)
129
125
  # The Vehicle's desired location
130
- @desired = Vec2D.new(width/2, height/2)
126
+ @desired = Vec2D.new(width / 2, height / 2)
131
127
 
132
128
  # Create a list of targets
133
129
  make_targets