ruby-processing 2.4.2 → 2.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,14 @@
1
1
  # Drawolver: draw 2D & revolve 3D
2
2
 
3
- # Example to show how to extend Ruby classes in a useful way and how to
4
- # use PVector and the Array is extended to yield one_of_each
5
- # pair of pts. See the drawolver library. Also features the use each_cons,
6
- # possibly a rare use for this ruby Enumerable method?
7
- # 2010-03-22 - fjenett (last revised by monkstone 2013-09-13)
3
+ # This example demonstrates the use of the `vecmath` library (a ruby-processing
4
+ # replacement for PVector). Also demonstrated is how to extend an instance of a
5
+ # here an instance of a core ruby class. In this case the ruby Array class,
6
+ # is extended to yield one_of_each pair of pts (see ExtendedArray module). This
7
+ # examples also includes a possibly rare use of the each_cons Enumerable method?
8
+ # For simpler illustrations of `vecmath` usage see the library examples.
9
+ # 2010-03-22 - fjenett (last revised by monkstone 2014-02-18)
10
+
11
+ load_library :vecmath
8
12
 
9
13
  attr_reader :drawing_mode, :points, :rot_x, :rot_y, :vertices
10
14
 
@@ -24,7 +28,6 @@ module ExtendedArray
24
28
  end
25
29
  end
26
30
 
27
-
28
31
  def setup
29
32
  size 1024, 768, P3D
30
33
  frame_rate 30
@@ -71,15 +74,15 @@ end
71
74
 
72
75
  def mouse_pressed
73
76
  reset_scene
74
- points << RPVector.new(mouse_x, mouse_y)
77
+ points << Vec3D.new(mouse_x, mouse_y)
75
78
  end
76
79
 
77
80
  def mouse_dragged
78
- points << RPVector.new(mouse_x, mouse_y)
81
+ points << Vec3D.new(mouse_x, mouse_y)
79
82
  end
80
83
 
81
84
  def mouse_released
82
- points << RPVector.new(mouse_x, mouse_y)
85
+ points << Vec3D.new(mouse_x, mouse_y)
83
86
  recalculate_shape
84
87
  end
85
88
 
@@ -87,16 +90,16 @@ def recalculate_shape
87
90
  @vertices = []
88
91
  points.each_cons(2) do |ps, pe|
89
92
  b = points.last - points.first
90
- len = b.mag
91
- b.normalize
93
+ # len = b.mag
94
+ b.normalize!
92
95
  a = ps - points.first
93
96
  dot = a.dot b
94
- b = b * dot
97
+ b *= dot
95
98
  normal = points.first + b
96
99
  c = ps - normal
97
- nlen = c.mag
100
+ # nlen = c.mag
98
101
  vertices << []
99
- (0..TWO_PI).step(PI/15) do |ang|
102
+ (0..TAU).step(PI/15) do |ang|
100
103
  e = normal + c * cos(ang)
101
104
  e.z = c.mag * sin(ang)
102
105
  vertices.last << e
@@ -105,27 +108,3 @@ def recalculate_shape
105
108
  @drawing_mode = false
106
109
  end
107
110
 
108
- # a wrapper around PVector that implements operators methods for +, -, *, /
109
- #
110
- class RPVector < Java::ProcessingCore::PVector
111
-
112
- def + (vect)
113
- RPVector.new self.x + vect.x, self.y + vect.y, self.z + vect.z
114
- end
115
-
116
- def - (vect)
117
- RPVector.new self.x - vect.x, self.y - vect.y, self.z - vect.z
118
- end
119
-
120
- def * (scalar)
121
- RPVector.new self.x * scalar, self.y * scalar, self.z * scalar
122
- end
123
-
124
- def / (scalar)
125
- RPVector.new(self.x / scalar, self.y / scalar, self.z / scalar) unless scalar == 0
126
- end
127
-
128
- end
129
-
130
-
131
-
@@ -2,11 +2,11 @@
2
2
  # This is a full-screen demo
3
3
  # Since processing-2.0 it is opengl
4
4
 
5
- full_screen
5
+ #full_screen # not working correctly in processing-2.1+?
6
6
 
7
7
 
8
8
  def setup
9
- size 1000, 1000, P3D
9
+ size displayWidth, displayHeight, P3D
10
10
  no_stroke
11
11
  end
12
12
 
@@ -9,10 +9,11 @@
9
9
 
10
10
  load_library :control_panel
11
11
 
12
- attr_reader :debug, :save_one, :step_angle, :cr, :detail, :panel
12
+ attr_reader :debug, :save_one, :step_angle, :cr, :detail, :panel, :hide
13
13
 
14
14
  def setup
15
15
  size 450, 320
16
+ @hide = false
16
17
  control_panel do |c|
17
18
  c.title = "controller"
18
19
  c.menu(:detail, ['4', '5', '6', '7', '8', '9', '10' ], '7')
@@ -27,7 +28,10 @@ def setup
27
28
  end
28
29
 
29
30
  def draw
30
- panel.set_visible true
31
+ if (!hide)
32
+ panel.set_visible true
33
+ @hide = true
34
+ end
31
35
  background color('#BDF018')
32
36
  translate width / 2, height / 2
33
37
  @step_angle = TWO_PI / (detail.to_i - 1)
@@ -73,6 +77,10 @@ def draw
73
77
  end
74
78
  end
75
79
 
80
+ def mouse_pressed
81
+ @hide = false
82
+ end
83
+
76
84
  def cos_x(n)
77
85
  cos(step_angle * n) * 100
78
86
  end
@@ -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
@@ -172,8 +172,8 @@ class Boid
172
172
  sum = Vec2D.new
173
173
  count = 0
174
174
  boids.each do |other|
175
- d = Vec2D.dist(location, other.location)
176
- if ((d > 0) && (d < neighbordist))
175
+ d = Vec2D.dist_squared(location, other.location)
176
+ if ((d > 0) && (d < neighbordist * neighbordist))
177
177
  sum += other.velocity
178
178
  count += 1
179
179
  end
@@ -199,8 +199,8 @@ class Boid
199
199
  sum = Vec2D.new # Start with empty vector to accumulate all locations
200
200
  count = 0
201
201
  boids.each do |other|
202
- d = Vec2D.dist(location, other.location)
203
- if ((d > 0) && (d < neighbordist))
202
+ d = Vec2D.dist_squared(location, other.location)
203
+ if ((d > 0) && (d < neighbordist * neighbordist))
204
204
  sum += other.location # Add location
205
205
  count += 1
206
206
  end
@@ -16,126 +16,126 @@ Z = 2
16
16
  attr_reader :arcball, :menger
17
17
 
18
18
  def setup
19
- size(640, 480, P3D)
20
- smooth(8)
21
- camera
22
- camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0)
23
- @arcball = ArcBall.new(0, 0, min(width - 20, height - 20) * 0.8)
24
- @menger = create_shape(GROUP)
25
- create_menger(0, 0, 0, height * 0.8)
19
+ size(640, 480, P3D)
20
+ smooth(8)
21
+ camera
22
+ camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0)
23
+ @arcball = ArcBall.new(0, 0, min(width - 20, height - 20) * 0.8)
24
+ @menger = create_shape(GROUP)
25
+ create_menger(0, 0, 0, height * 0.8)
26
26
  end
27
27
 
28
28
  def draw
29
- background(20, 20, 200)
30
- no_stroke
31
- lights
32
- update
33
- define_lights
34
- render
35
- end
29
+ background(20, 20, 200)
30
+ no_stroke
31
+ lights
32
+ update
33
+ define_lights
34
+ render
35
+ end
36
36
 
37
37
  def render
38
38
  menger.set_fill(color(224, 223, 219))
39
- menger.set_ambient(50)
40
- menger.set_specular(150)
41
- shape(menger)
39
+ menger.set_ambient(50)
40
+ menger.set_specular(150)
41
+ shape(menger)
42
42
  end
43
43
 
44
44
  def create_menger(xx, yy, zz, sz)
45
- u = sz / 3.0
46
- if (sz < MIN_SIZE) # recursion limited by minimum cube size
47
- no_stroke
48
- menger.add_child(create_cube(xx, yy, zz, sz)) # create and add a cube
49
- else
50
- PTS.each do |i|
51
- PTS.each do |j|
52
- PTS.each do |k|
53
- if ((i.abs + j.abs + k.abs) > 1)
54
- create_menger(xx + (i * u), yy + (j * u), zz + (k * u), u)
55
- end
56
- end
57
- end
58
- end
59
- end
45
+ u = sz / 3.0
46
+ if (sz < MIN_SIZE) # recursion limited by minimum cube size
47
+ no_stroke
48
+ menger.add_child(create_cube(xx, yy, zz, sz)) # create and add a cube
49
+ else
50
+ PTS.each do |i|
51
+ PTS.each do |j|
52
+ PTS.each do |k|
53
+ if ((i.abs + j.abs + k.abs) > 1)
54
+ create_menger(xx + (i * u), yy + (j * u), zz + (k * u), u)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
60
  end
61
61
 
62
62
  def create_cube(xx, yy, zz, sz)
63
- dim = sz / 2.0
64
- cube = create_shape
65
- cube.begin_shape(QUADS)
66
- # Front face
67
- cube.fill(255)
68
- cube.normal(0, 0, 1)
69
- cube.vertex(-dim + xx, -dim + yy, -dim + zz)
70
- cube.vertex(+dim + xx, -dim + yy, -dim + zz)
71
- cube.vertex(+dim + xx, +dim + yy, -dim + zz)
72
- cube.vertex(-dim + xx, +dim + yy, -dim + zz)
73
-
74
- # Back face
75
-
76
- cube.normal(0, 0, -1)
77
- cube.vertex(-dim + xx, -dim + yy, +dim + zz)
78
- cube.vertex(+dim + xx, -dim + yy, +dim + zz)
79
- cube.vertex(+dim + xx, +dim + yy, +dim + zz)
80
- cube.vertex(-dim + xx, +dim + yy, +dim + zz)
81
-
82
- # Left face
83
-
84
- cube.normal(1, 0, 0)
85
- cube.vertex(-dim + xx, -dim + yy, -dim + zz)
86
- cube.vertex(-dim + xx, -dim + yy, +dim + zz)
87
- cube.vertex(-dim + xx, +dim + yy, +dim + zz)
88
- cube.vertex(-dim + xx, +dim + yy, -dim + zz)
89
-
90
- # Right face
91
-
92
- cube.normal(-1, 0, 0)
93
- cube.vertex(+dim + xx, -dim + yy, -dim + zz)
94
- cube.vertex(+dim + xx, -dim + yy, +dim + zz)
95
- cube.vertex(+dim + xx, +dim + yy, +dim + zz)
96
- cube.vertex(+dim + xx, +dim + yy, -dim + zz)
97
-
98
- # Top face
99
-
100
- cube.normal(0, 1, 0)
101
- cube.vertex(-dim + xx, -dim + yy, -dim + zz)
102
- cube.vertex(+dim + xx, -dim + yy, -dim + zz)
103
- cube.vertex(+dim + xx, -dim + yy, +dim + zz)
104
- cube.vertex(-dim + xx, -dim + yy, +dim + zz)
105
-
106
- # Bottom face
107
-
108
- cube.normal(0, -1, 0)
109
- cube.vertex(-dim + xx, +dim + yy, -dim + zz)
110
- cube.vertex(+dim + xx, +dim + yy, -dim + zz)
111
- cube.vertex(+dim + xx, +dim + yy, +dim + zz)
112
- cube.vertex(-dim + xx, +dim + yy, +dim + zz)
113
- cube.end_shape
114
- return cube
115
- end
63
+ dim = sz / 2.0
64
+ cube = create_shape
65
+ cube.begin_shape(QUADS)
66
+ # Front face
67
+ cube.fill(255)
68
+ cube.normal(0, 0, 1)
69
+ cube.vertex(-dim + xx, -dim + yy, -dim + zz)
70
+ cube.vertex(+dim + xx, -dim + yy, -dim + zz)
71
+ cube.vertex(+dim + xx, +dim + yy, -dim + zz)
72
+ cube.vertex(-dim + xx, +dim + yy, -dim + zz)
73
+
74
+ # Back face
75
+
76
+ cube.normal(0, 0, -1)
77
+ cube.vertex(-dim + xx, -dim + yy, +dim + zz)
78
+ cube.vertex(+dim + xx, -dim + yy, +dim + zz)
79
+ cube.vertex(+dim + xx, +dim + yy, +dim + zz)
80
+ cube.vertex(-dim + xx, +dim + yy, +dim + zz)
81
+
82
+ # Left face
83
+
84
+ cube.normal(1, 0, 0)
85
+ cube.vertex(-dim + xx, -dim + yy, -dim + zz)
86
+ cube.vertex(-dim + xx, -dim + yy, +dim + zz)
87
+ cube.vertex(-dim + xx, +dim + yy, +dim + zz)
88
+ cube.vertex(-dim + xx, +dim + yy, -dim + zz)
89
+
90
+ # Right face
91
+
92
+ cube.normal(-1, 0, 0)
93
+ cube.vertex(+dim + xx, -dim + yy, -dim + zz)
94
+ cube.vertex(+dim + xx, -dim + yy, +dim + zz)
95
+ cube.vertex(+dim + xx, +dim + yy, +dim + zz)
96
+ cube.vertex(+dim + xx, +dim + yy, -dim + zz)
97
+
98
+ # Top face
99
+
100
+ cube.normal(0, 1, 0)
101
+ cube.vertex(-dim + xx, -dim + yy, -dim + zz)
102
+ cube.vertex(+dim + xx, -dim + yy, -dim + zz)
103
+ cube.vertex(+dim + xx, -dim + yy, +dim + zz)
104
+ cube.vertex(-dim + xx, -dim + yy, +dim + zz)
105
+
106
+ # Bottom face
107
+
108
+ cube.normal(0, -1, 0)
109
+ cube.vertex(-dim + xx, +dim + yy, -dim + zz)
110
+ cube.vertex(+dim + xx, +dim + yy, -dim + zz)
111
+ cube.vertex(+dim + xx, +dim + yy, +dim + zz)
112
+ cube.vertex(-dim + xx, +dim + yy, +dim + zz)
113
+ cube.end_shape
114
+ return cube
115
+ end
116
116
 
117
117
  def update
118
- theta, x, y, z = arcball.update
119
- rotate(theta, x, y, z)
118
+ theta, x, y, z = arcball.update
119
+ rotate(theta, x, y, z)
120
120
  end
121
121
 
122
122
  def mouse_pressed
123
- arcball.mouse_pressed(mouse_x, mouse_y)
123
+ arcball.mouse_pressed(mouse_x, mouse_y)
124
124
  end
125
125
 
126
126
  def mouse_dragged
127
- arcball.mouse_dragged(mouse_x, mouse_y)
127
+ arcball.mouse_dragged(mouse_x, mouse_y)
128
128
  end
129
129
 
130
130
  def define_lights
131
- ambient_light(50, 50, 50)
132
- point_light(30, 30, 30, 200, 240, 0)
133
- directional_light(50, 50, 50, 1, 0, 0)
134
- spot_light(30, 30, 30, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)
131
+ ambient_light(50, 50, 50)
132
+ point_light(30, 30, 30, 200, 240, 0)
133
+ directional_light(50, 50, 50, 1, 0, 0)
134
+ spot_light(30, 30, 30, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)
135
135
  end
136
136
 
137
- def key_pressed
138
- case(key)
137
+ def key_pressed
138
+ case(key)
139
139
  when 'x'
140
140
  arcball.select_axis(X)
141
141
  when 'y'
@@ -146,5 +146,5 @@ def key_pressed
146
146
  end
147
147
 
148
148
  def key_released
149
- arcball.select_axis(-1)
149
+ arcball.select_axis(-1)
150
150
  end
@@ -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