ruby-processing 2.4.3 → 2.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG +5 -0
  4. data/README.md +4 -2
  5. data/lib/ruby-processing/exporters/application_exporter.rb +1 -0
  6. data/lib/ruby-processing/version.rb +1 -1
  7. data/library/boids/boids.rb +14 -13
  8. data/library/vecmath/lib/vec.rb +20 -4
  9. data/samples/contributed/circle_collision.rb +92 -149
  10. data/samples/contributed/drawolver.rb +13 -27
  11. data/samples/contributed/elegant_ball.rb +158 -0
  12. data/samples/contributed/fern.rb +16 -3
  13. data/samples/contributed/flight_patterns.rb +16 -4
  14. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/bumpy_surface_noise.rb +4 -9
  15. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/data/java_args.txt +0 -0
  16. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/custom_shape/custom_shape.rb +1 -1
  17. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/particle_system/particle_system.rb +7 -10
  18. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/surface/surface.rb +2 -2
  19. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/liquidy.rb +7 -7
  20. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/polygons.rb +4 -9
  21. data/samples/external_library/java_processing/custom/README.md +15 -0
  22. data/samples/external_library/java_processing/custom/Rakefile +28 -0
  23. data/samples/external_library/java_processing/custom/landscape.rb +58 -0
  24. data/samples/external_library/java_processing/custom/src/nn/Connection.java +47 -0
  25. data/samples/external_library/java_processing/custom/src/nn/HiddenNeuron.java +20 -0
  26. data/samples/external_library/java_processing/custom/src/nn/InputNeuron.java +23 -0
  27. data/samples/external_library/java_processing/custom/src/nn/Network.java +136 -0
  28. data/samples/external_library/java_processing/custom/src/nn/Neuron.java +79 -0
  29. data/samples/external_library/java_processing/custom/src/nn/OutputNeuron.java +7 -0
  30. data/samples/external_library/java_processing/custom/xor.rb +88 -0
  31. data/samples/external_library/ruby_gem/README +1 -1
  32. data/samples/external_library/ruby_gem/data/data.json +8 -0
  33. data/samples/external_library/ruby_gem/draw_test.rb +171 -0
  34. data/samples/processing_app/basics/form/icosahedra.rb +39 -0
  35. data/samples/processing_app/basics/form/library/icosahedron/icosahedron.rb +60 -0
  36. data/samples/processing_app/basics/form/toroid.rb +78 -92
  37. data/samples/processing_app/basics/transform/birds.rb +6 -12
  38. data/samples/processing_app/basics/transform/cubes_in_cube.rb +25 -22
  39. data/samples/processing_app/basics/transform/library/cube/cube.rb +16 -16
  40. data/samples/processing_app/basics/transform/rotate_push_pop.rb +1 -1
  41. data/samples/processing_app/demos/graphics/bezier_patch.rb +18 -31
  42. data/samples/processing_app/demos/graphics/trefoil.rb +15 -15
  43. data/samples/processing_app/library/vecmath/acceleration_with_vectors.rb +3 -3
  44. data/samples/processing_app/library/vecmath/hilbert_fractal.rb +2 -2
  45. data/samples/processing_app/library/vecmath/library/flock/flock.rb +18 -21
  46. data/samples/processing_app/library/vecmath/library/hilbert/hilbert.rb +11 -8
  47. data/samples/processing_app/library/vecmath/library/wiggler/wiggler.rb +7 -15
  48. data/samples/processing_app/library/vecmath/seeking_neural.rb +172 -0
  49. data/samples/processing_app/topics/animation/animated_sprite.rb +5 -8
  50. data/samples/processing_app/topics/animation/sequential.rb +2 -3
  51. data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +7 -7
  52. data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
  53. data/samples/processing_app/topics/create_shapes/wiggle_pshape.rb +2 -1
  54. data/samples/processing_app/topics/lsystems/koch.rb +1 -1
  55. data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +24 -23
  56. data/samples/processing_app/topics/motion/circle_collision.rb +117 -160
  57. data/samples/processing_app/topics/motion/library/cube/cube.rb +1 -1
  58. data/samples/processing_app/topics/motion/morph.rb +1 -1
  59. data/samples/processing_app/topics/motion/reflection1.rb +17 -16
  60. data/samples/processing_app/topics/shaders/conway.rb +2 -2
  61. data/samples/processing_app/topics/shaders/data/conway.glsl +10 -10
  62. data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +9 -8
  63. data/samples/processing_app/topics/shaders/landscape.rb +1 -1
  64. data/samples/processing_app/topics/simulate/flocking.rb +1 -1
  65. data/samples/processing_app/topics/simulate/library/flock/flock.rb +62 -57
  66. data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +8 -28
  67. data/samples/processing_app/topics/simulate/simple_particle_system.rb +9 -7
  68. data/samples/processing_app/topics/simulate/smoke_particle_system.rb +12 -11
  69. data/vendors/Rakefile +2 -2
  70. metadata +26 -21
  71. data/samples/contributed/pong.rb +0 -177
  72. data/samples/contributed/simple_buffer.rb +0 -44
  73. data/samples/external_library/java_processing/pbox2d/contact_test.rb +0 -23
  74. data/samples/processing_app/basics/form/icosahedra/icosahedra.rb +0 -72
  75. data/samples/processing_app/basics/form/icosahedra/icosahedron.rb +0 -116
  76. data/samples/processing_app/basics/form/icosahedra/shape_3D.rb +0 -25
@@ -27,6 +27,6 @@ def draw
27
27
  shader(landscape)
28
28
  rect(0, 0, width, height)
29
29
 
30
- frame.set_title "frame: #{frame_count} - fps: #{frame_rate}"
30
+ frame.set_title "frame: #{frame_count} - fps: #{format("%.2f", frame_rate)}"
31
31
  end
32
32
 
@@ -8,7 +8,7 @@
8
8
  #
9
9
  # Click the mouse to add a new boid.
10
10
  #
11
- load_library :flock
11
+ load_libraries :flock, :vecmath
12
12
 
13
13
  attr_reader :flock
14
14
 
@@ -30,15 +30,20 @@ end
30
30
  class Boid
31
31
  include Processing::Proxy
32
32
 
33
- attr_reader :location, :velocity, :acceleration, :r, :maxforce, :maxspeed
33
+ MAXSPEED = 2
34
+ MAXFORCE = 0.03
35
+
36
+ attr_reader :width, :height
37
+ attr_reader :location, :velocity, :acceleration, :sz, :maxforce_squared, :maxspeed_squared
34
38
 
35
39
  def initialize(x, y)
36
- @acceleration = PVector.new(0, 0)
37
- @velocity = PVector.new(rand(-1.0 .. 1), rand(-1.0 .. 1))
38
- @location = PVector.new(x, y)
39
- @r = 2.0
40
- @maxspeed = 2
41
- @maxforce = 0.03
40
+ @width, @height = $app.width, $app.height
41
+ @acceleration = Vec2D.new(0, 0)
42
+ @velocity = Vec2D.new(rand(-1.0 .. 1), rand(-1.0 .. 1))
43
+ @location = Vec2D.new(x, y)
44
+ @sz = 4.0
45
+ @maxspeed_squared = MAXSPEED * MAXSPEED
46
+ @maxforce_squared = MAXFORCE * MAXFORCE
42
47
  end
43
48
 
44
49
  def run(boids)
@@ -50,7 +55,7 @@ class Boid
50
55
 
51
56
  def apply_force(force)
52
57
  # We could add mass here if we want A = F / M
53
- acceleration.add(force)
58
+ @acceleration += force
54
59
  end
55
60
 
56
61
  # We accumulate a new acceleration each time based on three rules
@@ -59,9 +64,9 @@ class Boid
59
64
  ali = align(boids) # Alignment
60
65
  coh = cohesion(boids) # Cohesion
61
66
  # Arbitrarily weight these forces
62
- sep.mult(1.5)
63
- ali.mult(1.0)
64
- coh.mult(1.0)
67
+ sep *= 1.5
68
+ ali *= 1.0
69
+ coh *= 1.0
65
70
  # Add the force vectors to acceleration
66
71
  apply_force(sep)
67
72
  apply_force(ali)
@@ -71,25 +76,25 @@ class Boid
71
76
  # Method to update location
72
77
  def update
73
78
  # Update velocity
74
- velocity.add(acceleration)
79
+ @velocity += acceleration
75
80
  # Limit speed
76
- velocity.limit(maxspeed)
77
- location.add(velocity)
81
+ velocity.set_mag(MAXSPEED) {velocity.mag_squared > maxspeed_squared}
82
+ @location += velocity
78
83
  # Reset accelertion to 0 each cycle
79
- acceleration.mult(0)
84
+ @acceleration *= 0
80
85
  end
81
86
 
82
87
  # A method that calculates and applies a steering force towards a target
83
88
  # STEER = DESIRED MINUS VELOCITY
84
89
  def seek(target)
85
- desired = PVector.sub(target,location) # A vector pointing from the location to the target
90
+ desired = target - location # A vector pointing from the location to the target
86
91
  # Normalize desired and scale to maximum speed
87
- desired.normalize
88
- desired.mult(maxspeed)
92
+ desired.normalize!
93
+ desired *= MAXSPEED
89
94
  # Steering = Desired minus Velocity
90
- steer = PVector.sub(desired,velocity)
91
- steer.limit(maxforce) # Limit to maximum steering force
92
- return steer
95
+ steer = desired - velocity
96
+ steer.set_mag(MAXFORCE) {steer.mag_squared > maxforce_squared} # Limit to maximum steering force
97
+ steer
93
98
  end
94
99
 
95
100
  def render
@@ -101,26 +106,26 @@ class Boid
101
106
  translate(location.x,location.y)
102
107
  rotate(theta)
103
108
  begin_shape(TRIANGLES)
104
- vertex(0, -r*2)
105
- vertex(-r, r*2)
106
- vertex(r, r*2)
109
+ vertex(0, -sz)
110
+ vertex(-sz * 0.5, sz)
111
+ vertex(sz * 0.5, sz)
107
112
  end_shape
108
113
  pop_matrix
109
114
  end
110
115
 
111
116
  # Wraparound
112
117
  def borders
113
- if (location.x < -r)
114
- location.x = $app.width+r
118
+ if (location.x < -sz * 0.5)
119
+ location.x = width + sz * 0.5
115
120
  end
116
- if (location.y < -r)
117
- location.y = $app.height+r
121
+ if (location.y < -sz * 0.5)
122
+ location.y = height + sz * 0.5
118
123
  end
119
- if (location.x > $app.width+r)
120
- location.x = -r
124
+ if (location.x > width + sz * 0.5)
125
+ location.x = -sz
121
126
  end
122
- if (location.y > $app.height+r)
123
- location.y = -r
127
+ if (location.y > height + sz * 0.5)
128
+ location.y = -sz * 0.5
124
129
  end
125
130
  end
126
131
 
@@ -128,33 +133,33 @@ class Boid
128
133
  # Method checks for nearby boids and steers away
129
134
  def separate boids
130
135
  desiredseparation = 25.0
131
- steer = PVector.new(0, 0, 0)
136
+ steer = Vec2D.new
132
137
  count = 0
133
138
  # For every boid in the system, check if it's too close
134
139
  boids.each do |other|
135
- d = PVector.dist(location, other.location)
140
+ d = Vec2D.dist(location, other.location)
136
141
  # If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
137
142
  if ((d > 0) && (d < desiredseparation))
138
143
  # Calculate vector pointing away from neighbor
139
- diff = PVector.sub(location,other.location)
140
- diff.normalize
141
- diff.div(d) # Weight by distance
142
- steer.add(diff)
144
+ diff = location - other.location
145
+ diff.normalize!
146
+ diff /= d # Weight by distance
147
+ steer += diff
143
148
  count += 1 # Keep track of how many
144
149
  end
145
150
  end
146
151
  # Average -- divide by how many
147
152
  if (count > 0)
148
- steer.div(count.to_f)
153
+ steer /= count.to_f
149
154
  end
150
155
 
151
156
  # As long as the vector is greater than 0
152
157
  if (steer.mag > 0)
153
158
  # Implement Reynolds: Steering = Desired - Velocity
154
- steer.normalize
155
- steer.mult(maxspeed)
156
- steer.sub(velocity)
157
- steer.limit(maxforce)
159
+ steer.normalize!
160
+ steer *= MAXSPEED
161
+ steer -= velocity
162
+ steer.set_mag(MAXFORCE) {steer.mag_squared > maxforce_squared}
158
163
  end
159
164
  return steer
160
165
  end
@@ -163,24 +168,24 @@ class Boid
163
168
  # For every nearby boid in the system, calculate the average velocity
164
169
  def align boids
165
170
  neighbordist = 50
166
- sum = PVector.new(0, 0)
171
+ sum = Vec2D.new(0, 0)
167
172
  count = 0
168
173
  boids.each do |other|
169
- d = PVector.dist(location, other.location)
174
+ d = Vec2D.dist(location, other.location)
170
175
  if ((d > 0) && (d < neighbordist))
171
- sum.add(other.velocity)
176
+ sum += other.velocity
172
177
  count += 1
173
178
  end
174
179
  end
175
180
  if (count > 0)
176
- sum.div(count.to_f)
177
- sum.normalize
178
- sum.mult(maxspeed)
179
- steer = PVector.sub(sum, velocity)
180
- steer.limit(maxforce)
181
+ sum /= (count.to_f)
182
+ sum.normalize!
183
+ sum *= MAXSPEED
184
+ steer = sum - velocity
185
+ steer.set_mag(MAXFORCE) {steer.mag_squared > maxforce_squared}
181
186
  return steer
182
187
  else
183
- return PVector.new(0,0)
188
+ return Vec2D.new
184
189
  end
185
190
  end
186
191
 
@@ -188,17 +193,17 @@ class Boid
188
193
  # For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
189
194
  def cohesion boids
190
195
  neighbordist = 50
191
- sum = PVector.new(0, 0) # Start with empty vector to accumulate all locations
196
+ sum = Vec2D.new # Start with empty vector to accumulate all locations
192
197
  count = 0
193
198
  boids.each do |other|
194
- d = PVector.dist(location, other.location)
199
+ d = Vec2D.dist(location, other.location)
195
200
  if ((d > 0) && (d < neighbordist))
196
- sum.add(other.location) # Add location
201
+ sum += other.location # Add location
197
202
  count += 1
198
203
  end
199
204
  end
200
- sum.div(count) unless (count == 0) # avoid div by zero
201
- return (count > 0)? seek(sum) : PVector.new(0, 0)
205
+ sum /= count unless (count == 0) # avoid div by zero
206
+ return (count > 0)? seek(sum) : Vec2D.new
202
207
  end
203
208
  end
204
209
 
@@ -5,6 +5,8 @@
5
5
  # Each burst is one instance of a particle system with Particles and
6
6
  # CrazyParticles (a subclass of Particle).
7
7
 
8
+ load_library :vecmath
9
+
8
10
  module Runnable
9
11
  def run
10
12
  self.reject! { |item| item.dead? }
@@ -12,28 +14,6 @@ module Runnable
12
14
  end
13
15
  end
14
16
 
15
- class Vector
16
- attr_accessor :x, :y
17
-
18
- def initialize(x, y)
19
- @x, @y = x, y
20
- end
21
-
22
- def +(other)
23
- return Vector.new(@x + other, @y + other) if other.is_a?(Numeric)
24
- return Vector.new(@x + other.x, @y + other.y) if other.is_a?(Vector)
25
- self
26
- end
27
-
28
- def heading
29
- -1 * Math::atan2(-@y, @x)
30
- end
31
-
32
- def magnitude
33
- @x * @x + @y * @y
34
- end
35
- end
36
-
37
17
 
38
18
  class ParticleSystem
39
19
  extend Enumerable
@@ -70,7 +50,7 @@ def setup
70
50
  size 640, 580
71
51
  color_mode(RGB, 255, 255, 255, 100)
72
52
  ellipse_mode(CENTER)
73
- @particle_systems = ParticleSystem.new(rand(21) + 5, Vector.new(width/2, height/2))
53
+ @particle_systems = ParticleSystem.new(rand(21) + 5, Vec2D.new(width/2, height/2))
74
54
  end
75
55
 
76
56
  def draw
@@ -80,7 +60,7 @@ end
80
60
 
81
61
  def mouse_pressed
82
62
  origin = rand(21) + 5
83
- vector = Vector.new(mouse_x, mouse_y)
63
+ vector = Vec2D.new(mouse_x, mouse_y)
84
64
  @particle_systems << ParticleSystem.new(origin, vector)
85
65
  end
86
66
 
@@ -88,8 +68,8 @@ end
88
68
  class Particle
89
69
  def initialize(origin)
90
70
  @origin = origin
91
- @velocity = Vector.new(rand(-1.0 .. 1), rand(-2.0 .. 0))
92
- @acceleration = Vector.new(0, 0.05)
71
+ @velocity = Vec2D.new(rand(-1.0 .. 1), rand(-2.0 .. 0))
72
+ @acceleration = Vec2D.new(0, 0.05)
93
73
  @radius = 10
94
74
  @lifespan = 100
95
75
  end
@@ -129,7 +109,7 @@ class Particle
129
109
  translate(@origin.x, @origin.y)
130
110
  rotate(@velocity.heading)
131
111
 
132
- length = @velocity.magnitude * scale
112
+ length = @velocity.mag * scale
133
113
 
134
114
  line 0, 0, length, 0
135
115
  line length, 0, length - arrow_size, arrow_size / 2
@@ -155,7 +135,7 @@ class CrazyParticle < Particle
155
135
 
156
136
  def update
157
137
  super
158
- @theta += @velocity.x * @velocity.magnitude / 10
138
+ @theta += @velocity.x * @velocity.mag / 10
159
139
  end
160
140
 
161
141
  def grow
@@ -4,11 +4,13 @@
4
4
  # time. A ParticleSystem (Array) object manages a variable size list of
5
5
  # particles.
6
6
 
7
+ load_library :vecmath
8
+
7
9
  attr_reader :ps
8
10
 
9
11
  def setup
10
12
  size(640,360)
11
- @ps = ParticleSystem.new(PVector.new(width/2, 50))
13
+ @ps = ParticleSystem.new(Vec2D.new(width/2, 50))
12
14
  end
13
15
 
14
16
  def draw
@@ -32,7 +34,7 @@ class ParticleSystem
32
34
 
33
35
  def initialize(loc)
34
36
  @particle_system = []
35
- @origin = loc.get
37
+ @origin = loc.dup
36
38
  end
37
39
 
38
40
  def each &block
@@ -60,9 +62,9 @@ class Particle
60
62
 
61
63
  attr_reader :loc, :vel, :acc, :lifespan
62
64
  def initialize(l)
63
- @acc = PVector.new(0, 0.05)
64
- @vel = PVector.new(rand(-1.0 .. 1), rand(-2.0 .. 0))
65
- @loc = l.get
65
+ @acc = Vec2D.new(0, 0.05)
66
+ @vel = Vec2D.new(rand(-1.0 .. 1), rand(-2.0 .. 0))
67
+ @loc = l.dup
66
68
  @lifespan = 255.0
67
69
  end
68
70
 
@@ -73,8 +75,8 @@ class Particle
73
75
 
74
76
  # Method to update loc
75
77
  def update
76
- vel.add(acc)
77
- loc.add(vel)
78
+ @vel += acc
79
+ @loc += vel
78
80
  @lifespan -= 1.0
79
81
  end
80
82
 
@@ -3,20 +3,21 @@
3
3
  #
4
4
  # A ParticleSystem (Array) object manages a variable size list of
5
5
  # particles.
6
+ load_library :vecmath
6
7
 
7
8
  attr_reader :ps, :img, :wind
8
9
 
9
10
  def setup
10
11
  size(640,360)
11
12
  @img = load_image("texture.png")
12
- @ps = ParticleSystem.new(0, PVector.new(width/2, height - 60), img)
13
+ @ps = ParticleSystem.new(0, Vec2D.new(width/2, height - 60), img)
13
14
  end
14
15
 
15
16
  def draw
16
17
  background(0)
17
18
  # Calculate a "wind" force based on mouse horizontal position
18
19
  dx = map(mouse_x, 0, width, -0.2, 0.2)
19
- @wind = PVector.new(dx, 0)
20
+ @wind = Vec2D.new(dx, 0)
20
21
  ps.apply_force(wind)
21
22
  ps.run
22
23
  2.times do
@@ -24,7 +25,7 @@ def draw
24
25
  end
25
26
 
26
27
  # Draw a horizontal arrow representing the wind force
27
- draw_vector(wind, PVector.new(width / 2, 50, 0), 500)
28
+ draw_vector(wind, Vec2D.new(width / 2, 50, 0), 500)
28
29
 
29
30
  end
30
31
 
@@ -33,7 +34,7 @@ def draw_vector(v, loc, scayl)
33
34
  push_matrix
34
35
  # Translate to location to render vector
35
36
  translate(loc.x, loc.y)
36
- rotate(v.heading2D)
37
+ rotate(v.heading)
37
38
  # Calculate length of vector & scale as necessary
38
39
  len = v.mag * scayl
39
40
  # Draw three lines to make an arrow
@@ -63,7 +64,7 @@ class ParticleSystem
63
64
 
64
65
  def initialize(num, location, image)
65
66
  @particles = []
66
- @origin = location.get
67
+ @origin = location.dup
67
68
  @image = image
68
69
  (0 ... num).each do
69
70
  particles << Particle.new(origin, image)
@@ -98,11 +99,11 @@ class Particle
98
99
  attr_reader :loc, :acc, :vel, :lifespan, :img, :generator
99
100
 
100
101
  def initialize(l, img_)
101
- @acc = PVector.new(0, 0)
102
+ @acc = Vec2D.new(0, 0)
102
103
  vx = random_gaussian * 0.3
103
104
  vy = random_gaussian * 0.3 - 1.0
104
- @vel = PVector.new(vx, vy)
105
- @loc = l.get()
105
+ @vel = Vec2D.new(vx, vy)
106
+ @loc = l.dup()
106
107
  @lifespan = 100.0
107
108
  @img = img_
108
109
  end
@@ -114,8 +115,8 @@ class Particle
114
115
 
115
116
  # Method to update location
116
117
  def update
117
- vel.add(acc)
118
- loc.add(vel)
118
+ @vel += acc
119
+ @loc += vel
119
120
  @lifespan -= 1.0
120
121
  end
121
122
 
@@ -128,7 +129,7 @@ class Particle
128
129
 
129
130
  # Method to add a force vector to all particles currently in the system
130
131
  def apply_force(f)
131
- @acc.add(f)
132
+ @acc += f
132
133
  end
133
134
 
134
135
  end