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
@@ -5,15 +5,20 @@
5
5
  load_library 'cs_grammar'
6
6
 
7
7
  def setup
8
+ size 150, 250
9
+ background 0
10
+ fill(200, 200, 0)
11
+ f = create_font('Arial', 16, true)
12
+ text_font(f)
8
13
  (0 .. 7).each do |i|
9
14
  grammar = Grammar.new(
10
- "baaaaaa",
15
+ 'baaaaaa',
11
16
  {
12
- "b<a" => "b", # context sensitive rule replace a when preceded by b
13
- "b" =>"a"
17
+ 'b<a' => 'b', # context sensitive rule replace a when preceded by b
18
+ 'b' =>'a'
14
19
  }
15
20
  )
16
- puts grammar.generate(i)
21
+ text grammar.generate(i), 30, i * 25
17
22
  end
18
23
  end
19
24
 
@@ -8,41 +8,39 @@ class DavidTour
8
8
  import 'grammar'
9
9
  attr_reader :draw_length, :xpos, :ypos, :theta, :axiom, :grammar
10
10
  DELTA = Math::PI/3 # 60 degrees
11
-
12
- def initialize xpos, ypos
13
- @axiom = "FX-XFX-XFX-XFX-XFX-XF" # Axiom
11
+
12
+ def initialize(xpos, ypos)
13
+ @axiom = 'FX-XFX-XFX-XFX-XFX-XF' # Axiom
14
14
  @theta = 0
15
15
  @grammar = Grammar.new(
16
16
  axiom,
17
- {
18
- "F" => "!F!-F-!F!", # Rules
19
- "X" => "!X"
20
- }
17
+ 'F' => '!F!-F-!F!', # Rules
18
+ 'X' => '!X'
21
19
  )
22
20
  @draw_length = 15
23
21
  @xpos = xpos
24
22
  @ypos = ypos
25
23
  end
26
-
27
- def create_grammar(gen)
24
+
25
+ def create_grammar(gen)
28
26
  @draw_length *= draw_length * 0.5**gen
29
27
  grammar.generate(gen)
30
28
  end
31
-
29
+
32
30
  def translate_rules(prod)
33
31
  swap = false
34
32
  points = [] # An empty array to store lines as a flat array of points
35
33
  prod.each do |ch|
36
34
  case(ch)
37
35
  when 'F'
38
- points << xpos << ypos << (@xpos += draw_length * Math.cos(theta)) << (@ypos -= draw_length * Math.sin(theta))
36
+ points << xpos << ypos << (@xpos += draw_length * Math.cos(theta)) << (@ypos -= draw_length * Math.sin(theta))
39
37
  when '+'
40
- @theta += DELTA
38
+ @theta += DELTA
41
39
  when '-'
42
40
  @theta += swap ? DELTA : -DELTA
43
41
  when '!'
44
42
  swap = !swap
45
- when 'X'
43
+ when 'X'
46
44
  else
47
45
  puts("character '#{ch}' not in grammar")
48
46
  end
@@ -65,13 +63,13 @@ def setup
65
63
  david = DavidTour.new(width * 0.6, height/4)
66
64
  production = david.create_grammar(5)
67
65
  @points = david.translate_rules(production)
68
- no_loop()
66
+ no_loop
69
67
  end
70
68
 
71
- def draw()
69
+ def draw
72
70
  background(0)
73
71
  stroke(255)
74
72
  points.each_slice(4) do |point|
75
73
  line(*point)
76
74
  end
77
- end
75
+ end
@@ -1,29 +1,19 @@
1
- #
2
- # Koch Curve
3
- # by Daniel Shiffman.
4
- #
5
- # Renders a simple fractal, the Koch snowflake.
6
- # Each recursive level is drawn in sequence.
7
- #
8
-
9
- load_libraries :koch, :vecmath
10
-
11
- attr_reader :k
1
+ load_library :vecmath, :fastmath, :koch
12
2
 
13
3
  def setup
14
- size(640, 360)
4
+ size(800, 250)
5
+ background(255)
15
6
  frame_rate(1) # Animate slowly
16
7
  @k = KochFractal.new(width, height)
8
+ smooth 8
17
9
  end
18
10
 
19
11
  def draw
20
- background(0)
12
+ background(255)
21
13
  # Draws the snowflake!
22
- k.render
14
+ @k.render
23
15
  # Iterate
24
- k.next_level
16
+ @k.next_level
25
17
  # Let's not do it more than 5 times. . .
26
- if (k.get_count > 5)
27
- k.restart
28
- end
18
+ @k.restart if @k.count > 5
29
19
  end
@@ -1,6 +1,6 @@
1
1
  ######################################
2
2
  # cs_grammar.rb a context sensitive
3
- # 1-L lsystem grammar for
3
+ # 1-L lsystem grammar for
4
4
  # ruby/ruby-processing
5
5
  # by Martin Prout (January 2013)
6
6
  ######################################
@@ -27,42 +27,40 @@ class Grammar
27
27
  add_rule pair[0], pair[1]
28
28
  end
29
29
  end
30
-
30
+
31
31
  def add_rule(pre, rule)
32
32
  if pre.length == 3
33
33
  if pre[1] == '<'
34
34
  @context[pre[2]] = pre
35
35
  elsif pre[1] == '>'
36
- @context[pre[0]] = pre
36
+ @context[pre[0]] = pre
37
37
  end
38
38
  @no_context[pre] = rule # key length == 3
39
39
  elsif pre.length == 1
40
40
  @no_context[pre] = rule # key length == 1
41
- else
42
- print "unrecognized grammar '#{pre}'"
41
+ else
42
+ print 'unrecognized grammar '#{pre}''
43
43
  end
44
44
  end
45
-
45
+
46
46
  def generate(repeat = 0) # repeat iteration grammar rules
47
47
  prod = axiom
48
- repeat.times do
49
- prod = new_production(prod)
50
- end
48
+ repeat.times { prod = new_production(prod) }
51
49
  return prod
52
- end
53
-
54
-
50
+ end
51
+
52
+
55
53
  def new_production prod # single iteration grammar rules
56
54
  @idx = -1
57
55
  prod.gsub!(/./) do |ch|
58
- get_rule(prod, ch)
56
+ get_rule(prod, ch)
59
57
  end
60
58
  end
61
-
59
+
62
60
  def get_rule prod, ch
63
61
  rule = ch # default is return original character as rule (no change)
64
62
  @idx += 1 # increment the index of axiom/production as a side effect
65
- if (context.has_key?(ch))
63
+ if context.key?(ch)
66
64
  if context[ch][1] == '<'
67
65
  cs_char = context[ch][0]
68
66
  rule = no_context[context[ch]] if cs_char == get_context(prod, idx, -1) # use context sensitive rule
@@ -71,18 +69,14 @@ class Grammar
71
69
  rule = no_context[context[ch]] if cs_char == get_context(prod, idx, 1) # use context sensitive rule
72
70
  end
73
71
  else
74
- rule = no_context[ch] if no_context.has_key?(ch) # context free rule if it exists
72
+ rule = no_context[ch] if no_context.key?(ch) # context free rule if it exists
75
73
  end
76
74
  return rule
77
75
  end
78
-
79
- def get_context prod, idx, inc
76
+
77
+ def get_context(prod, idx, inc)
80
78
  index = idx + inc
81
- while (ignore.include? prod[index])
82
- index += inc
83
- end
79
+ index += inc while ignore.include?(prod[index])
84
80
  return prod[index]
85
81
  end
86
- end
87
-
88
-
82
+ end
@@ -2,7 +2,7 @@
2
2
  # Simple lsystem grammar
3
3
  ############################
4
4
  class Grammar
5
-
5
+
6
6
  attr_reader :axiom, :rules
7
7
  def initialize(axiom, rules)
8
8
  @axiom = axiom
@@ -11,7 +11,7 @@ class Grammar
11
11
 
12
12
  def expand(production, iterations, &block)
13
13
  production.each_char do |token|
14
- if rules.has_key?(token) && iterations > 0
14
+ if rules.key?(token) && iterations > 0
15
15
  expand(rules[token], iterations - 1, &block)
16
16
  else
17
17
  yield token
@@ -19,15 +19,13 @@ class Grammar
19
19
  end
20
20
  end
21
21
 
22
- def each gen
23
- expand(axiom, gen) {|token| yield token }
22
+ def each(gen)
23
+ expand(axiom, gen) { |token| yield token }
24
24
  end
25
25
 
26
- def generate gen
26
+ def generate(gen)
27
27
  output = []
28
- each(gen){ |token| output << token }
28
+ each(gen) { |token| output << token }
29
29
  return output
30
30
  end
31
-
32
- end
33
-
31
+ end
@@ -1,118 +1,99 @@
1
- # Koch Curve
2
- # A class to manage the list of line segments in the snowflake pattern
1
+ class KochLine
2
+ include Processing::Proxy
3
+ attr_reader :start, :finish
4
+
5
+ def initialize(a, b)
6
+ @start = a.copy
7
+ @finish = b.copy
8
+ end
9
+
10
+ def display
11
+ stroke(120)
12
+ line(start.x, start.y, finish.x, finish.y)
13
+ end
3
14
 
15
+ # This is easy, just 1/3 of the way
16
+ def kochleft
17
+ v = finish - start
18
+ v /= 3
19
+ v + start
20
+ end
21
+
22
+ # More complicated, have to use a little trig to figure out where this vector is!
23
+ def kochmiddle
24
+ v = finish - start
25
+ v /= 3
26
+ p = start.copy
27
+ p += v
28
+ rotate_line(v, -60)
29
+ p + v
30
+ end
31
+
32
+ # Easy, just 2/3 of the way
33
+ def kochright
34
+ v = start - finish
35
+ v /= 3
36
+ v + finish
37
+ end
38
+
39
+ private
40
+ def rotate_line(v, theta)
41
+ xtemp = v.x
42
+ # Might need to check for rounding errors like with angleBetween function?
43
+ v.x = v.x * DegLut.cos(theta) - v.y * DegLut.sin(theta)
44
+ v.y = xtemp * DegLut.sin(theta) + v.y * DegLut.cos(theta)
45
+ end
46
+ end
4
47
 
5
48
  class KochFractal
6
- include Processing::Proxy
7
- attr_reader :start, :endk, :lines, :count
8
-
9
- def initialize width, height
49
+ attr_reader :count, :start, :finish, :lines
50
+ def initialize(width, height)
10
51
  @start = Vec2D.new(0, height - 20)
11
- @endk = Vec2D.new(width, height - 20)
52
+ @finish = Vec2D.new(width, height - 20)
53
+ @lines = []
12
54
  restart
13
55
  end
14
56
 
15
- def next_level
57
+ def next_level
16
58
  # For every line that is in the arraylist
17
59
  # create 4 more lines in a new arraylist
18
60
  @lines = iterate(lines)
19
61
  @count += 1
20
62
  end
21
63
 
22
- def restart
64
+ def restart
23
65
  @count = 0 # Reset count
24
- @lines = [] # Empty the array list
25
- lines << KochLine.new(start, endk) # Add the initial line (from one end Vector to the other)
66
+ lines.clear # Empty the array list
67
+ lines << KochLine.new(start, finish) # Add the initial line (from one end vector to the other)
26
68
  end
27
-
28
- def get_count
29
- return count
30
- end
31
-
32
- # This is easy, just draw all the lines
69
+
33
70
  def render
34
- lines.each do |line|
35
- line.display
36
- end
71
+ lines.each{ |line| line.display }
37
72
  end
38
73
 
39
74
  # This is where the **MAGIC** happens
40
- # Step 1: Create an empty array
41
- # Step 2: For every line currently in the array
75
+ # Step 1: Create an empty arraylist
76
+ # Step 2: For every line currently in the arraylist
42
77
  # - calculate 4 line segments based on Koch algorithm
43
- # - add all 4 line segments into the new array
44
- # Step 3: Return the new array and it becomes the list of line segments for the structure
45
-
46
- # As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . .
78
+ # - add all 4 line segments into the new arraylist
79
+ # Step 3: Return the new arraylist and it becomes the list of line segments for the structure
80
+
81
+ # As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . .
47
82
  def iterate(before)
48
- now = [] # Create empty list
49
- before.each do |line|
50
- # Calculate 5 koch Vectors (done for us by the line object)
51
- a = line.start
52
- b = line.kochleft
53
- c = line.kochmiddle
54
- d = line.kochright
55
- e = line.endk
56
- # Make line segments between all the Vectors and add them
57
- # Note how we can chain '<<' in ruby, could all be in one line.
58
- now << KochLine.new(a,b) << KochLine.new(b,c)
59
- now << KochLine.new(c,d) << KochLine.new(d,e)
83
+ now = [] # Create emtpy list
84
+ before.each do |l|
85
+ # Calculate 5 koch vectors (done for us by the line object)
86
+ a = l.start
87
+ b = l.kochleft
88
+ c = l.kochmiddle
89
+ d = l.kochright
90
+ e = l.finish
91
+ # Make line segments between all the vectors and add them
92
+ now << KochLine.new(a, b)
93
+ now << KochLine.new(b, c)
94
+ now << KochLine.new(c, d)
95
+ now << KochLine.new(d, e)
60
96
  end
61
- return now
97
+ now
62
98
  end
63
99
  end
64
-
65
- # The Nature of Code
66
- # Daniel Shiffman
67
- # http://natureofcode.com
68
-
69
- # Koch Curve
70
- # A class to describe one line segment in the fractal
71
- # Includes methods to calculate mid Vectors along the line according to the Koch algorithm
72
-
73
- class KochLine
74
- include Processing::Proxy
75
- # Two PVectors,
76
- # a is the "left" Vector and
77
- # b is the "right Vector
78
- attr_reader :start, :endk
79
-
80
- def initialize(start, endk)
81
- @start = start.dup
82
- @endk = endk.dup
83
- end
84
-
85
- def display
86
- stroke(255)
87
- line(start.x, start.y, endk.x, endk.y)
88
- end
89
-
90
- # This is easy, just 1/3 of the way
91
- def kochleft
92
- v = endk - start
93
- v /= 3.0
94
- v += start
95
- return v
96
- end
97
-
98
- # More complicated, have to use a little trig to figure out where this Vector is!
99
- def kochmiddle
100
- v = endk - start
101
- v /= 3.0
102
- p = start.dup
103
- p += v
104
- rot = radians(-60)
105
- r = Vec2D.new((v.x * cos(rot)) - v.y * sin(rot), (v.x * sin(rot)) + (v.y * cos(rot)))
106
- p += r
107
- return p
108
- end
109
-
110
- # Easy, just 2/3 of the way
111
- def kochright
112
- v = start - endk
113
- v /= 3.0
114
- v += endk
115
- return v
116
- end
117
- end
118
-