rubysketch 0.2.6 → 0.3.3

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