ruby-processing 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,10 +5,51 @@
5
5
  # Each burst is one instance of a particle system with Particles and
6
6
  # CrazyParticles (a subclass of Particle).
7
7
 
8
+ module Runnable
9
+ def run
10
+ self.reject! { |item| item.dead? }
11
+ self.each { |item| item.run }
12
+ end
13
+ end
14
+
15
+
16
+ class Vector
17
+ attr_accessor :x, :y
18
+
19
+ def initialize(x, y)
20
+ @x, @y = x, y
21
+ end
8
22
 
9
- require 'ruby-processing'
23
+ def +(other)
24
+ return Vector.new(@x + other, @y + other) if other.is_a?(Numeric)
25
+ return Vector.new(@x + other.x, @y + other.y) if other.is_a?(Vector)
26
+ self
27
+ end
28
+
29
+ def heading
30
+ -1 * Math::atan2(-@y, @x)
31
+ end
32
+
33
+ def magnitude
34
+ @x * @x + @y * @y
35
+ end
36
+ end
10
37
 
11
- class MultipleParticleSystems < Processing::App
38
+
39
+ class ParticleSystem < Array
40
+ include Runnable
41
+ alias_method :dead?, :empty?
42
+
43
+ def initialize(number, origin)
44
+ super()
45
+ @origin = origin
46
+ kind = rand < 0.5 ? Sketch::Particle : Sketch::CrazyParticle
47
+ number.times { self << kind.new(origin) }
48
+ end
49
+ end
50
+
51
+
52
+ class Sketch < Processing::App
12
53
  def setup
13
54
  smooth
14
55
  color_mode(RGB, 255, 255, 255, 100)
@@ -24,28 +65,12 @@ class MultipleParticleSystems < Processing::App
24
65
  end
25
66
 
26
67
  def mouse_pressed
27
- @particle_systems << ParticleSystem.new(rand(21) + 5, Vector.new(mouse_x, mouse_y))
68
+ origin = rand(21) + 5
69
+ vector = Vector.new(mouse_x, mouse_y)
70
+ @particle_systems << ParticleSystem.new(origin, vector)
28
71
  end
29
-
30
- module Runnable
31
- def run
32
- self.reject! { |item| item.dead? }
33
- self.each { |item| item.run }
34
- end
35
- end
36
-
37
- class ParticleSystem < Array
38
- include Runnable
39
- alias_method :dead?, :empty?
40
-
41
- def initialize(number, origin)
42
- super()
43
- @origin = origin
44
- kind = rand < 0.5 ? Particle : CrazyParticle
45
- number.times { self << kind.new(origin) }
46
- end
47
- end
48
-
72
+
73
+
49
74
  class Particle
50
75
  def initialize(origin)
51
76
  @origin = origin
@@ -76,30 +101,31 @@ class MultipleParticleSystems < Processing::App
76
101
  end
77
102
 
78
103
  def render
79
- $app.stroke(255, @lifespan)
80
- $app.fill(100, @lifespan)
81
- $app.ellipse(@origin.x, @origin.y, @radius, @radius)
104
+ stroke(255, @lifespan)
105
+ fill(100, @lifespan)
106
+ ellipse(@origin.x, @origin.y, @radius, @radius)
82
107
  end
83
108
 
84
109
  def render_velocity_vector
85
110
  scale = 10
86
111
  arrow_size = 4
87
112
 
88
- $app.push_matrix
89
-
90
- $app.translate(@origin.x, @origin.y)
91
- $app.rotate(@velocity.heading)
113
+ push_matrix
114
+
115
+ translate(@origin.x, @origin.y)
116
+ rotate(@velocity.heading)
92
117
 
93
118
  length = @velocity.magnitude * scale
94
119
 
95
- $app.line 0, 0, length, 0
96
- $app.line length, 0, length - arrow_size, arrow_size / 2
97
- $app.line length, 0, length - arrow_size, -arrow_size / 2
98
-
99
- $app.pop_matrix
120
+ line 0, 0, length, 0
121
+ line length, 0, length - arrow_size, arrow_size / 2
122
+ line length, 0, length - arrow_size, -arrow_size / 2
123
+
124
+ pop_matrix
100
125
  end
101
126
  end
102
-
127
+
128
+
103
129
  class CrazyParticle < Particle
104
130
  def initialize(origin)
105
131
  super
@@ -123,44 +149,19 @@ class MultipleParticleSystems < Processing::App
123
149
  end
124
150
 
125
151
  def render_rotation_line
126
- $app.push_matrix
127
-
128
- $app.translate(@origin.x, @origin.y)
129
- $app.rotate(@theta)
130
-
131
- $app.stroke(255, @lifespan)
132
-
133
- $app.line(0, 0, 25, 0)
134
-
135
- $app.pop_matrix
136
- end
137
- end
138
-
139
- class Vector
140
- attr_accessor :x, :y
141
-
142
- def initialize(x, y)
143
- @x, @y = x, y
144
- end
145
-
146
- def +(other)
147
- if other.is_a?(Numeric)
148
- Vector.new(@x + other, @y + other)
149
- elsif other.is_a?(Vector)
150
- Vector.new(@x + other.x, @y + other.y)
151
- else
152
- self
153
- end
154
- end
155
-
156
- def heading
157
- -1 * Math::atan2(-@y, @x)
158
- end
159
-
160
- def magnitude
161
- @x * @x + @y * @y
152
+ push_matrix
153
+
154
+ translate(@origin.x, @origin.y)
155
+ rotate(@theta)
156
+
157
+ stroke(255, @lifespan)
158
+
159
+ line(0, 0, 25, 0)
160
+
161
+ pop_matrix
162
162
  end
163
163
  end
164
+
164
165
  end
165
166
 
166
- $app = MultipleParticleSystems.new :width => 640, :height => 340, :title => 'MultipleParticleSystems'
167
+ Sketch.new :width => 640, :height => 340
@@ -1,26 +1,17 @@
1
1
  # Taken from the Processing Examples.
2
-
3
- require 'ruby-processing'
4
-
5
- class Reflection < Processing::App
6
-
7
- def setup
8
- render_mode(P3D)
9
- no_stroke
10
- color_mode(RGB, 1)
11
- fill 0.4
12
- end
13
-
14
- def draw
15
- background 0
16
- translate( width/2, height/2 )
17
- light_specular(1, 1, 1)
18
- directional_light(0.8, 0.8, 0.8, 0, 0, -1)
19
- s = mouse_x.to_f / width.to_f
20
- specular s, s, s
21
- sphere 50
22
- end
23
2
 
3
+ def setup
4
+ size 200, 200, P3D
5
+ no_stroke
6
+ color_mode RGB, 1
7
+ fill 0.4
24
8
  end
25
9
 
26
- Reflection.new(:width => 200, :height => 200, :title => "Reflection")
10
+ def draw
11
+ background 0
12
+ translate width/2, height/2
13
+ light_specular 1, 1, 1
14
+ directional_light 0.8, 0.8, 0.8, 0, 0, -1
15
+ specular mouse_x.to_f / width.to_f
16
+ sphere 50
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-processing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Ashkenas
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2009-03-28 00:00:00 -04:00
20
+ date: 2009-04-05 00:00:00 -04:00
21
21
  default_executable: rp5
22
22
  dependencies: []
23
23