rubysketch 0.2.7 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,8 +3,9 @@ module RubySketch
3
3
 
4
4
  class Window < Reflex::Window
5
5
 
6
- attr_accessor :setup, :update, :draw, :key, :motion, :resize,
7
- :pointer_down, :pointer_up, :pointer_move, :pointer_drag
6
+ attr_accessor :setup, :update, :draw, :before_draw, :after_draw, :resize,
7
+ :pointer_down, :pointer_up, :pointer_move, :pointer_drag,
8
+ :key, :motion
8
9
 
9
10
  attr_accessor :auto_resize
10
11
 
@@ -21,7 +22,7 @@ module RubySketch
21
22
  end
22
23
 
23
24
  def start (&block)
24
- @canvas_painter.paint do |_|
25
+ draw_canvas do
25
26
  block.call if block
26
27
  on_setup
27
28
  end
@@ -41,14 +42,12 @@ module RubySketch
41
42
  end
42
43
 
43
44
  def on_draw (e)
44
- @canvas_painter.paint do |_|
45
- call_block @draw, e
46
- end
45
+ draw_canvas {call_block @draw, e} if @draw
47
46
  e.painter.image @canvas
48
47
  end
49
48
 
50
49
  def on_key (e)
51
- call_block @key, e
50
+ draw_canvas {call_block @key, e} if @key
52
51
  end
53
52
 
54
53
  def on_pointer (e)
@@ -57,16 +56,16 @@ module RubySketch
57
56
  when :up then @pointer_up
58
57
  when :move then e.drag? ? @pointer_drag : @pointer_move
59
58
  end
60
- call_block block, e if block
59
+ draw_canvas {call_block block, e} if block
61
60
  end
62
61
 
63
62
  def on_motion (e)
64
- call_block @motion, e
63
+ draw_canvas {call_block @motion, e} if @motion
65
64
  end
66
65
 
67
66
  def on_resize (e)
68
67
  reset_canvas e.width, e.height if @auto_resize
69
- call_block @resize, e
68
+ draw_canvas {call_block @resize, e} if @resize
70
69
  end
71
70
 
72
71
  private
@@ -98,6 +97,23 @@ module RubySketch
98
97
  to.font = from.font
99
98
  end
100
99
 
100
+ def draw_canvas (&block)
101
+ begin_draw
102
+ block.call
103
+ ensure
104
+ end_draw
105
+ end
106
+
107
+ def begin_draw ()
108
+ @canvas_painter.__send__ :begin_paint
109
+ @before_draw&.call
110
+ end
111
+
112
+ def end_draw ()
113
+ @after_draw&.call
114
+ @canvas_painter.__send__ :end_paint
115
+ end
116
+
101
117
  def call_block (block, event, *args)
102
118
  @events.push event
103
119
  block.call event, *args if block && !@error
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ %w[../xot ../rucy ../rays ../reflex .]
5
+ .map {|s| File.expand_path "../../#{s}/lib", __FILE__}
6
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
7
+
8
+ require 'test/unit'
9
+ require 'xot/test'
10
+ require 'rubysketch'
11
+
12
+ include Xot::Test
13
+
14
+
15
+ unless $RAYS_NOAUTOINIT
16
+ #def Rays.fin! () end
17
+ end
18
+
19
+
20
+ def assert_equal_vector (v1, v2, delta = 0.000001)
21
+ assert_in_delta v1.x, v2.x, delta
22
+ assert_in_delta v1.y, v2.y, delta
23
+ assert_in_delta v1.z, v2.z, delta
24
+ end
@@ -0,0 +1,397 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require_relative '../helper'
5
+
6
+
7
+ class TestProcessingVector < Test::Unit::TestCase
8
+
9
+ P = RubySketch::Processing
10
+
11
+ V = P::Vector
12
+
13
+ M = Math
14
+
15
+ PI = M::PI
16
+
17
+ def vec (*args)
18
+ V.new *args
19
+ end
20
+
21
+ def point (*args)
22
+ Rays::Point.new *args
23
+ end
24
+
25
+ def test_initialize ()
26
+ assert_equal_vector vec(0, 0, 0), vec()
27
+ assert_equal_vector vec(1, 0, 0), vec(1)
28
+ assert_equal_vector vec(1, 2, 0), vec(1, 2)
29
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3)
30
+
31
+ assert_equal_vector vec(0, 0, 0), vec([])
32
+ assert_equal_vector vec(1, 0, 0), vec([1])
33
+ assert_equal_vector vec(1, 2, 0), vec([1, 2])
34
+ assert_equal_vector vec(1, 2, 3), vec([1, 2, 3])
35
+
36
+ assert_equal_vector vec(1, 2, 3), vec(vec 1, 2, 3)
37
+ assert_equal_vector vec(1, 2, 3), vec(point 1, 2, 3)
38
+ end
39
+
40
+ def test_set ()
41
+ v0 = vec 9, 9, 9
42
+
43
+ v = v0.dup; v.set; assert_equal_vector vec(0, 0, 0), v
44
+ v = v0.dup; v.set 1; assert_equal_vector vec(1, 0, 0), v
45
+ v = v0.dup; v.set 1, 2; assert_equal_vector vec(1, 2, 0), v
46
+ v = v0.dup; v.set 1, 2, 3; assert_equal_vector vec(1, 2, 3), v
47
+
48
+ v = v0.dup; v.set []; assert_equal_vector vec(0, 0, 0), v
49
+ v = v0.dup; v.set [1]; assert_equal_vector vec(1, 0, 0), v
50
+ v = v0.dup; v.set [1, 2]; assert_equal_vector vec(1, 2, 0), v
51
+ v = v0.dup; v.set [1, 2, 3]; assert_equal_vector vec(1, 2, 3), v
52
+
53
+ v = v0.dup; v.set vec(1, 2, 3); assert_equal_vector vec(1, 2, 3), v
54
+ v = v0.dup; v.set point(1, 2, 3); assert_equal_vector vec(1, 2, 3), v
55
+ end
56
+
57
+ def test_dup ()
58
+ v1 = vec 1, 2, 3
59
+ assert_equal_vector vec(1, 2, 3), v1
60
+
61
+ v2 = v1.dup
62
+ assert_equal_vector vec(1, 2, 3), v1
63
+ assert_equal_vector vec(1, 2, 3), v2
64
+
65
+ v1.set 7, 8, 9
66
+ assert_equal_vector vec(7, 8, 9), v1
67
+ assert_equal_vector vec(1, 2, 3), v2
68
+ end
69
+
70
+ def test_copy ()
71
+ v1 = vec 1, 2, 3
72
+ assert_equal_vector vec(1, 2, 3), v1
73
+
74
+ v2 = v1.copy
75
+ assert_equal_vector vec(1, 2, 3), v1
76
+ assert_equal_vector vec(1, 2, 3), v2
77
+
78
+ v1.set 7, 8, 9
79
+ assert_equal_vector vec(7, 8, 9), v1
80
+ assert_equal_vector vec(1, 2, 3), v2
81
+ end
82
+
83
+ def test_xyz ()
84
+ v = vec 1, 2, 3
85
+ assert_equal_vector vec(1, 2, 3), v
86
+ assert_equal [1, 2, 3], [v.x, v.y, v.z]
87
+
88
+ v.x = 7
89
+ assert_equal_vector vec(7, 2, 3), v
90
+
91
+ v.y = 8
92
+ assert_equal_vector vec(7, 8, 3), v
93
+
94
+ v.z = 9
95
+ assert_equal_vector vec(7, 8, 9), v
96
+ end
97
+
98
+ def test_array ()
99
+ assert_equal [1, 2, 3], vec(1, 2, 3).array
100
+ end
101
+
102
+ def test_add ()
103
+ v = vec 1, 2, 3
104
+ v.add 4, 5, 6
105
+ assert_equal_vector vec(5, 7, 9), v
106
+
107
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3).add()
108
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3).add(4)
109
+ assert_equal_vector vec(5, 7, 3), vec(1, 2, 3).add(4, 5)
110
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add(4, 5, 6)
111
+
112
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3).add([])
113
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3).add([4])
114
+ assert_equal_vector vec(5, 7, 3), vec(1, 2, 3).add([4, 5])
115
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add([4, 5, 6])
116
+
117
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add( vec(4, 5, 6))
118
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add(point(4, 5, 6))
119
+ end
120
+
121
+ def test_sub ()
122
+ v = vec 9, 8, 7
123
+ v.sub 1, 2, 3
124
+ assert_equal_vector vec(8, 6, 4), v
125
+
126
+ assert_equal_vector vec(9, 8, 7), vec(9, 8, 7).sub()
127
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7).sub(1)
128
+ assert_equal_vector vec(8, 6, 7), vec(9, 8, 7).sub(1, 2)
129
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub(1, 2, 3)
130
+
131
+ assert_equal_vector vec(9, 8, 7), vec(9, 8, 7).sub([])
132
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7).sub([1])
133
+ assert_equal_vector vec(8, 6, 7), vec(9, 8, 7).sub([1, 2])
134
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub([1, 2, 3])
135
+
136
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub( vec(1, 2, 3))
137
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub(point(1, 2, 3))
138
+ end
139
+
140
+ def test_mult ()
141
+ v = vec 1, 2, 3
142
+ v.mult 2
143
+ assert_equal_vector vec(2, 4, 6), v
144
+ end
145
+
146
+ def test_div ()
147
+ v = vec 2, 4, 6
148
+ v.div 2
149
+ assert_equal_vector vec(1, 2, 3), v
150
+ end
151
+
152
+ def test_op_add ()
153
+ v1 = vec 1, 2, 3
154
+ v2 = vec 4, 5, 6
155
+ assert_equal_vector vec(5, 7, 9), v1 + v2
156
+ assert_equal_vector vec(1, 2, 3), v1
157
+ assert_equal_vector vec(4, 5, 6), v2
158
+
159
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3) + 4
160
+
161
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3) + []
162
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3) + [4]
163
+ assert_equal_vector vec(5, 7, 3), vec(1, 2, 3) + [4, 5]
164
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + [4, 5, 6]
165
+
166
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + vec(4, 5, 6)
167
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + point(4, 5, 6)
168
+ end
169
+
170
+ def test_op_sub ()
171
+ v1 = vec 9, 8, 7
172
+ v2 = vec 1, 2, 3
173
+ assert_equal_vector vec(8, 6, 4), v1 - v2
174
+ assert_equal_vector vec(9, 8, 7), v1
175
+ assert_equal_vector vec(1, 2, 3), v2
176
+
177
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7) - 1
178
+
179
+ assert_equal_vector vec(9, 8, 7), vec(9, 8, 7) - []
180
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7) - [1]
181
+ assert_equal_vector vec(8, 6, 7), vec(9, 8, 7) - [1, 2]
182
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - [1, 2, 3]
183
+
184
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - vec(1, 2, 3)
185
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - point(1, 2, 3)
186
+ end
187
+
188
+ def test_op_mult ()
189
+ v = vec 1, 2, 3
190
+ assert_equal_vector vec(2, 4, 6), v * 2
191
+ assert_equal_vector vec(1, 2, 3), v
192
+ end
193
+
194
+ def test_op_div ()
195
+ v = vec 2, 4, 6
196
+ assert_equal_vector vec(1, 2, 3), v / 2
197
+ assert_equal_vector vec(2, 4, 6), v
198
+ end
199
+
200
+ def test_fun_add ()
201
+ v1 = vec 1, 2, 3
202
+ v2 = vec 4, 5, 6
203
+ result = vec
204
+ assert_equal_vector vec(5, 7, 9), V.add(v1, v2, result)
205
+ assert_equal_vector vec(1, 2, 3), v1
206
+ assert_equal_vector vec(4, 5, 6), v2
207
+ assert_equal_vector vec(5, 7, 9), result
208
+ end
209
+
210
+ def test_fun_sub ()
211
+ v1 = vec 9, 8, 7
212
+ v2 = vec 1, 2, 3
213
+ result = vec
214
+ assert_equal_vector vec(8, 6, 4), V.sub(v1, v2, result)
215
+ assert_equal_vector vec(9, 8, 7), v1
216
+ assert_equal_vector vec(1, 2, 3), v2
217
+ assert_equal_vector vec(8, 6, 4), result
218
+ end
219
+
220
+ def test_fun_mult ()
221
+ v1 = vec 1, 2, 3
222
+ result = vec
223
+ assert_equal_vector vec(2, 4, 6), V.mult(v1, 2, result)
224
+ assert_equal_vector vec(1, 2, 3), v1
225
+ assert_equal_vector vec(2, 4, 6), result
226
+ end
227
+
228
+ def test_fun_div ()
229
+ v1 = vec 2, 4, 6
230
+ result = vec
231
+ assert_equal_vector vec(1, 2, 3), V.div(v1, 2, result)
232
+ assert_equal_vector vec(2, 4, 6), v1
233
+ assert_equal_vector vec(1, 2, 3), result
234
+ end
235
+
236
+ def test_mag ()
237
+ assert_in_delta M.sqrt(5), vec(1, 2) .mag, 0.000001
238
+ assert_in_delta M.sqrt(14), vec(1, 2, 3).mag, 0.000001
239
+ end
240
+
241
+ def test_magSq ()
242
+ assert_equal 5, vec(1, 2) .magSq
243
+ assert_equal 14, vec(1, 2, 3).magSq
244
+ end
245
+
246
+ def test_setMag ()
247
+ v = vec 3, 4, 0
248
+ assert_equal_vector vec(6, 8, 0), v.setMag(10)
249
+ assert_equal_vector vec(6, 8, 0), v
250
+
251
+ v = vec 3, 4, 0
252
+ result = vec
253
+ assert_equal_vector vec(6, 8, 0), v.setMag(result, 10)
254
+ assert_equal_vector vec(3, 4, 0), v
255
+ assert_equal_vector vec(6, 8, 0), result
256
+ end
257
+
258
+ def test_normalize ()
259
+ v = vec 1, 2, 3
260
+ normal = v / v.mag
261
+ assert_equal_vector normal, v.normalize
262
+ assert_equal_vector normal, v
263
+
264
+ v = vec 1, 2, 3
265
+ result = vec
266
+ assert_equal_vector normal, v.normalize(result)
267
+ assert_equal_vector vec(1, 2, 3), v
268
+ assert_equal_vector normal, result
269
+ end
270
+
271
+ def test_limit ()
272
+ v = vec 1, 2, 3
273
+ assert_in_delta 1, v.limit(1).mag, 0.000001
274
+ assert_in_delta 1, v .mag, 0.000001
275
+
276
+ assert_in_delta 1, vec(1, 2, 3).limit(1).mag, 0.000001
277
+ assert_in_delta 2, vec(1, 2, 3).limit(2).mag, 0.000001
278
+ assert_in_delta 3, vec(1, 2, 3).limit(3).mag, 0.000001
279
+ assert_in_delta vec(1, 2, 3).mag, vec(1, 2, 3).limit(4).mag, 0.000001
280
+ end
281
+
282
+ def test_dist ()
283
+ v1 = vec 1, 2, 3
284
+ v2 = vec 4, 5, 6
285
+
286
+ assert_in_delta M.sqrt((4-1)**2 + (5-2)**2 + (6-3)**2), v1.dist(v2), 0.000001
287
+ assert_equal_vector vec(1, 2, 3), v1
288
+ assert_equal_vector vec(4, 5, 6), v2
289
+
290
+ assert_in_delta M.sqrt((4-1)**2 + (5-2)**2 + (6-3)**2), V.dist(v1, v2), 0.000001
291
+ assert_equal_vector vec(1, 2, 3), v1
292
+ assert_equal_vector vec(4, 5, 6), v2
293
+ end
294
+
295
+ def test_dot ()
296
+ v1 = vec 1, 2, 3
297
+ v2 = vec 4, 5, 6
298
+
299
+ assert_equal 1*4 + 2*5 + 3*6, v1.dot(4, 5, 6)
300
+ assert_equal_vector vec(1, 2, 3), v1
301
+
302
+ assert_equal 1*4 + 2*5 + 3*6, v1.dot(v2)
303
+ assert_equal_vector vec(1, 2, 3), v1
304
+ assert_equal_vector vec(4, 5, 6), v2
305
+
306
+ assert_equal 1*4 + 2*5 + 3*6, V.dot(v1, v2)
307
+ assert_equal_vector vec(1, 2, 3), v1
308
+ assert_equal_vector vec(4, 5, 6), v2
309
+ end
310
+
311
+ def test_cross ()
312
+ v1 = vec 1, 0, 0
313
+ v2 = vec 0, 1, 0
314
+
315
+ assert_equal_vector vec(0, 0, 1), v1.cross(0, 1, 0)
316
+ assert_equal_vector vec(1, 0, 0), v1
317
+
318
+ result = vec 1, 2, 3
319
+ assert_equal_vector vec(0, 0, 1), v1.cross(v2, result)
320
+ assert_equal_vector vec(1, 0, 0), v1
321
+ assert_equal_vector vec(0, 1, 0), v2
322
+ assert_equal_vector vec(0, 0, 1), result
323
+
324
+ result = vec 1, 2, 3
325
+ assert_equal_vector vec(0, 0, 1), V.cross(v1, v2, result)
326
+ assert_equal_vector vec(1, 0, 0), v1
327
+ assert_equal_vector vec(0, 1, 0), v2
328
+ assert_equal_vector vec(0, 0, 1), result
329
+ end
330
+
331
+ def test_rotate ()
332
+ angle = PI * 2 * 0.1
333
+ context = Object.new.tap {|o| def o.toAngle__ (a); a * 2 * P::RAD2DEG__; end}
334
+
335
+ v = vec 1, 0, 0
336
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), v.rotate(angle)
337
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), v
338
+
339
+ v = vec 1, 0, 0, context: context
340
+ assert_equal_vector vec(M.cos(angle * 2), M.sin(angle * 2), 0), v.rotate(angle)
341
+ end
342
+
343
+ def test_fromAngle ()
344
+ angle = PI * 2 * 0.1
345
+ result = vec
346
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), V.fromAngle(angle)
347
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), V.fromAngle(angle, result)
348
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), result
349
+ end
350
+
351
+ def test_heading ()
352
+ angle = PI * 1 * 0.1
353
+ assert_in_delta angle, V.fromAngle( angle).heading, 0.000001
354
+ assert_in_delta -angle, V.fromAngle(-angle).heading, 0.000001
355
+ end
356
+
357
+ def test_angleBetween ()
358
+ v1 = V.fromAngle PI * 0.25
359
+ v2 = V.fromAngle PI * 0.75
360
+ assert_in_delta PI / 2, V.angleBetween(v1, v2), 0.000001
361
+ end
362
+
363
+ def test_lerp ()
364
+ assert_equal_vector vec(0.5, 0.5, 0.5), vec(0, 0, 0).lerp(vec(1, 1, 1), 0.5)
365
+ assert_equal_vector vec(0.5, 0.5, 0.5), vec(0, 0, 0).lerp( 1, 1, 1, 0.5)
366
+ assert_equal_vector vec(0.5, 0.5, 0.5), V.lerp(vec(0, 0, 0), vec(1, 1, 1), 0.5)
367
+ end
368
+
369
+ def test_random2D ()
370
+ v1 = V.random2D
371
+ v2 = V.random2D
372
+ assert v1.x != 0
373
+ assert v1.y != 0
374
+ assert_equal 0, v1.z
375
+ assert v2.x != 0
376
+ assert v2.y != 0
377
+ assert_equal 0, v2.z
378
+ assert_not_equal v1, v2
379
+ assert_in_delta 1, v1.mag, 0.000001
380
+ assert_in_delta 1, v2.mag, 0.000001
381
+ end
382
+
383
+ def test_random3D ()
384
+ v1 = V.random3D
385
+ v2 = V.random3D
386
+ assert v1.x != 0
387
+ assert v1.y != 0
388
+ assert v1.z != 0
389
+ assert v2.x != 0
390
+ assert v2.y != 0
391
+ assert v2.z != 0
392
+ assert_not_equal v1, v2
393
+ assert_in_delta 1, v1.mag, 0.000001
394
+ assert_in_delta 1, v2.mag, 0.000001
395
+ end
396
+
397
+ end# TestProcessingVector