processing 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,394 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require_relative 'helper'
5
+
6
+
7
+ class TestProcessingVector < Test::Unit::TestCase
8
+
9
+ P = Processing::Processing
10
+ V = P::Vector
11
+ M = Math
12
+ PI = M::PI
13
+
14
+ def vec(*args, **kwargs)
15
+ V.new(*args, **kwargs)
16
+ end
17
+
18
+ def point(*args, **kwargs)
19
+ Rays::Point.new(*args, **kwargs)
20
+ end
21
+
22
+ def test_initialize()
23
+ assert_equal_vector vec(0, 0, 0), vec()
24
+ assert_equal_vector vec(1, 0, 0), vec(1)
25
+ assert_equal_vector vec(1, 2, 0), vec(1, 2)
26
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3)
27
+
28
+ assert_equal_vector vec(0, 0, 0), vec([])
29
+ assert_equal_vector vec(1, 0, 0), vec([1])
30
+ assert_equal_vector vec(1, 2, 0), vec([1, 2])
31
+ assert_equal_vector vec(1, 2, 3), vec([1, 2, 3])
32
+
33
+ assert_equal_vector vec(1, 2, 3), vec(vec 1, 2, 3)
34
+ assert_equal_vector vec(1, 2, 3), vec(point 1, 2, 3)
35
+ end
36
+
37
+ def test_set()
38
+ v0 = vec 9, 9, 9
39
+
40
+ v = v0.dup; v.set; assert_equal_vector vec(0, 0, 0), v
41
+ v = v0.dup; v.set 1; assert_equal_vector vec(1, 0, 0), v
42
+ v = v0.dup; v.set 1, 2; assert_equal_vector vec(1, 2, 0), v
43
+ v = v0.dup; v.set 1, 2, 3; assert_equal_vector vec(1, 2, 3), v
44
+
45
+ v = v0.dup; v.set []; assert_equal_vector vec(0, 0, 0), v
46
+ v = v0.dup; v.set [1]; assert_equal_vector vec(1, 0, 0), v
47
+ v = v0.dup; v.set [1, 2]; assert_equal_vector vec(1, 2, 0), v
48
+ v = v0.dup; v.set [1, 2, 3]; assert_equal_vector vec(1, 2, 3), v
49
+
50
+ v = v0.dup; v.set vec(1, 2, 3); assert_equal_vector vec(1, 2, 3), v
51
+ v = v0.dup; v.set point(1, 2, 3); assert_equal_vector vec(1, 2, 3), v
52
+ end
53
+
54
+ def test_dup()
55
+ v1 = vec 1, 2, 3
56
+ assert_equal_vector vec(1, 2, 3), v1
57
+
58
+ v2 = v1.dup
59
+ assert_equal_vector vec(1, 2, 3), v1
60
+ assert_equal_vector vec(1, 2, 3), v2
61
+
62
+ v1.set 7, 8, 9
63
+ assert_equal_vector vec(7, 8, 9), v1
64
+ assert_equal_vector vec(1, 2, 3), v2
65
+ end
66
+
67
+ def test_copy()
68
+ v1 = vec 1, 2, 3
69
+ assert_equal_vector vec(1, 2, 3), v1
70
+
71
+ v2 = v1.copy
72
+ assert_equal_vector vec(1, 2, 3), v1
73
+ assert_equal_vector vec(1, 2, 3), v2
74
+
75
+ v1.set 7, 8, 9
76
+ assert_equal_vector vec(7, 8, 9), v1
77
+ assert_equal_vector vec(1, 2, 3), v2
78
+ end
79
+
80
+ def test_xyz()
81
+ v = vec 1, 2, 3
82
+ assert_equal_vector vec(1, 2, 3), v
83
+ assert_equal [1, 2, 3], [v.x, v.y, v.z]
84
+
85
+ v.x = 7
86
+ assert_equal_vector vec(7, 2, 3), v
87
+
88
+ v.y = 8
89
+ assert_equal_vector vec(7, 8, 3), v
90
+
91
+ v.z = 9
92
+ assert_equal_vector vec(7, 8, 9), v
93
+ end
94
+
95
+ def test_array()
96
+ assert_equal [1, 2, 3], vec(1, 2, 3).array
97
+ end
98
+
99
+ def test_add()
100
+ v = vec 1, 2, 3
101
+ v.add 4, 5, 6
102
+ assert_equal_vector vec(5, 7, 9), v
103
+
104
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3).add()
105
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3).add(4)
106
+ assert_equal_vector vec(5, 7, 3), vec(1, 2, 3).add(4, 5)
107
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add(4, 5, 6)
108
+
109
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3).add([])
110
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3).add([4])
111
+ assert_equal_vector vec(5, 7, 3), vec(1, 2, 3).add([4, 5])
112
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add([4, 5, 6])
113
+
114
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add( vec(4, 5, 6))
115
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add(point(4, 5, 6))
116
+ end
117
+
118
+ def test_sub()
119
+ v = vec 9, 8, 7
120
+ v.sub 1, 2, 3
121
+ assert_equal_vector vec(8, 6, 4), v
122
+
123
+ assert_equal_vector vec(9, 8, 7), vec(9, 8, 7).sub()
124
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7).sub(1)
125
+ assert_equal_vector vec(8, 6, 7), vec(9, 8, 7).sub(1, 2)
126
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub(1, 2, 3)
127
+
128
+ assert_equal_vector vec(9, 8, 7), vec(9, 8, 7).sub([])
129
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7).sub([1])
130
+ assert_equal_vector vec(8, 6, 7), vec(9, 8, 7).sub([1, 2])
131
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub([1, 2, 3])
132
+
133
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub( vec(1, 2, 3))
134
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub(point(1, 2, 3))
135
+ end
136
+
137
+ def test_mult()
138
+ v = vec 1, 2, 3
139
+ v.mult 2
140
+ assert_equal_vector vec(2, 4, 6), v
141
+ end
142
+
143
+ def test_div()
144
+ v = vec 2, 4, 6
145
+ v.div 2
146
+ assert_equal_vector vec(1, 2, 3), v
147
+ end
148
+
149
+ def test_op_add()
150
+ v1 = vec 1, 2, 3
151
+ v2 = vec 4, 5, 6
152
+ assert_equal_vector vec(5, 7, 9), v1 + v2
153
+ assert_equal_vector vec(1, 2, 3), v1
154
+ assert_equal_vector vec(4, 5, 6), v2
155
+
156
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3) + 4
157
+
158
+ assert_equal_vector vec(1, 2, 3), vec(1, 2, 3) + []
159
+ assert_equal_vector vec(5, 2, 3), vec(1, 2, 3) + [4]
160
+ assert_equal_vector vec(5, 7, 3), vec(1, 2, 3) + [4, 5]
161
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + [4, 5, 6]
162
+
163
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + vec(4, 5, 6)
164
+ assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + point(4, 5, 6)
165
+ end
166
+
167
+ def test_op_sub()
168
+ v1 = vec 9, 8, 7
169
+ v2 = vec 1, 2, 3
170
+ assert_equal_vector vec(8, 6, 4), v1 - v2
171
+ assert_equal_vector vec(9, 8, 7), v1
172
+ assert_equal_vector vec(1, 2, 3), v2
173
+
174
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7) - 1
175
+
176
+ assert_equal_vector vec(9, 8, 7), vec(9, 8, 7) - []
177
+ assert_equal_vector vec(8, 8, 7), vec(9, 8, 7) - [1]
178
+ assert_equal_vector vec(8, 6, 7), vec(9, 8, 7) - [1, 2]
179
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - [1, 2, 3]
180
+
181
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - vec(1, 2, 3)
182
+ assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - point(1, 2, 3)
183
+ end
184
+
185
+ def test_op_mult()
186
+ v = vec 1, 2, 3
187
+ assert_equal_vector vec(2, 4, 6), v * 2
188
+ assert_equal_vector vec(1, 2, 3), v
189
+ end
190
+
191
+ def test_op_div()
192
+ v = vec 2, 4, 6
193
+ assert_equal_vector vec(1, 2, 3), v / 2
194
+ assert_equal_vector vec(2, 4, 6), v
195
+ end
196
+
197
+ def test_fun_add()
198
+ v1 = vec 1, 2, 3
199
+ v2 = vec 4, 5, 6
200
+ result = vec
201
+ assert_equal_vector vec(5, 7, 9), V.add(v1, v2, result)
202
+ assert_equal_vector vec(1, 2, 3), v1
203
+ assert_equal_vector vec(4, 5, 6), v2
204
+ assert_equal_vector vec(5, 7, 9), result
205
+ end
206
+
207
+ def test_fun_sub()
208
+ v1 = vec 9, 8, 7
209
+ v2 = vec 1, 2, 3
210
+ result = vec
211
+ assert_equal_vector vec(8, 6, 4), V.sub(v1, v2, result)
212
+ assert_equal_vector vec(9, 8, 7), v1
213
+ assert_equal_vector vec(1, 2, 3), v2
214
+ assert_equal_vector vec(8, 6, 4), result
215
+ end
216
+
217
+ def test_fun_mult()
218
+ v1 = vec 1, 2, 3
219
+ result = vec
220
+ assert_equal_vector vec(2, 4, 6), V.mult(v1, 2, result)
221
+ assert_equal_vector vec(1, 2, 3), v1
222
+ assert_equal_vector vec(2, 4, 6), result
223
+ end
224
+
225
+ def test_fun_div()
226
+ v1 = vec 2, 4, 6
227
+ result = vec
228
+ assert_equal_vector vec(1, 2, 3), V.div(v1, 2, result)
229
+ assert_equal_vector vec(2, 4, 6), v1
230
+ assert_equal_vector vec(1, 2, 3), result
231
+ end
232
+
233
+ def test_mag()
234
+ assert_in_delta M.sqrt(5), vec(1, 2) .mag, 0.000001
235
+ assert_in_delta M.sqrt(14), vec(1, 2, 3).mag, 0.000001
236
+ end
237
+
238
+ def test_magSq()
239
+ assert_equal 5, vec(1, 2) .magSq
240
+ assert_equal 14, vec(1, 2, 3).magSq
241
+ end
242
+
243
+ def test_setMag()
244
+ v = vec 3, 4, 0
245
+ assert_equal_vector vec(6, 8, 0), v.setMag(10)
246
+ assert_equal_vector vec(6, 8, 0), v
247
+
248
+ v = vec 3, 4, 0
249
+ result = vec
250
+ assert_equal_vector vec(6, 8, 0), v.setMag(result, 10)
251
+ assert_equal_vector vec(3, 4, 0), v
252
+ assert_equal_vector vec(6, 8, 0), result
253
+ end
254
+
255
+ def test_normalize()
256
+ v = vec 1, 2, 3
257
+ normal = v / v.mag
258
+ assert_equal_vector normal, v.normalize
259
+ assert_equal_vector normal, v
260
+
261
+ v = vec 1, 2, 3
262
+ result = vec
263
+ assert_equal_vector normal, v.normalize(result)
264
+ assert_equal_vector vec(1, 2, 3), v
265
+ assert_equal_vector normal, result
266
+ end
267
+
268
+ def test_limit()
269
+ v = vec 1, 2, 3
270
+ assert_in_delta 1, v.limit(1).mag, 0.000001
271
+ assert_in_delta 1, v .mag, 0.000001
272
+
273
+ assert_in_delta 1, vec(1, 2, 3).limit(1).mag, 0.000001
274
+ assert_in_delta 2, vec(1, 2, 3).limit(2).mag, 0.000001
275
+ assert_in_delta 3, vec(1, 2, 3).limit(3).mag, 0.000001
276
+ assert_in_delta vec(1, 2, 3).mag, vec(1, 2, 3).limit(4).mag, 0.000001
277
+ end
278
+
279
+ def test_dist()
280
+ v1 = vec 1, 2, 3
281
+ v2 = vec 4, 5, 6
282
+
283
+ assert_in_delta M.sqrt((4-1)**2 + (5-2)**2 + (6-3)**2), v1.dist(v2), 0.000001
284
+ assert_equal_vector vec(1, 2, 3), v1
285
+ assert_equal_vector vec(4, 5, 6), v2
286
+
287
+ assert_in_delta M.sqrt((4-1)**2 + (5-2)**2 + (6-3)**2), V.dist(v1, v2), 0.000001
288
+ assert_equal_vector vec(1, 2, 3), v1
289
+ assert_equal_vector vec(4, 5, 6), v2
290
+ end
291
+
292
+ def test_dot()
293
+ v1 = vec 1, 2, 3
294
+ v2 = vec 4, 5, 6
295
+
296
+ assert_equal 1*4 + 2*5 + 3*6, v1.dot(4, 5, 6)
297
+ assert_equal_vector vec(1, 2, 3), v1
298
+
299
+ assert_equal 1*4 + 2*5 + 3*6, v1.dot(v2)
300
+ assert_equal_vector vec(1, 2, 3), v1
301
+ assert_equal_vector vec(4, 5, 6), v2
302
+
303
+ assert_equal 1*4 + 2*5 + 3*6, V.dot(v1, v2)
304
+ assert_equal_vector vec(1, 2, 3), v1
305
+ assert_equal_vector vec(4, 5, 6), v2
306
+ end
307
+
308
+ def test_cross()
309
+ v1 = vec 1, 0, 0
310
+ v2 = vec 0, 1, 0
311
+
312
+ assert_equal_vector vec(0, 0, 1), v1.cross(0, 1, 0)
313
+ assert_equal_vector vec(1, 0, 0), v1
314
+
315
+ result = vec 1, 2, 3
316
+ assert_equal_vector vec(0, 0, 1), v1.cross(v2, result)
317
+ assert_equal_vector vec(1, 0, 0), v1
318
+ assert_equal_vector vec(0, 1, 0), v2
319
+ assert_equal_vector vec(0, 0, 1), result
320
+
321
+ result = vec 1, 2, 3
322
+ assert_equal_vector vec(0, 0, 1), V.cross(v1, v2, result)
323
+ assert_equal_vector vec(1, 0, 0), v1
324
+ assert_equal_vector vec(0, 1, 0), v2
325
+ assert_equal_vector vec(0, 0, 1), result
326
+ end
327
+
328
+ def test_rotate()
329
+ angle = PI * 2 * 0.1
330
+ context = Object.new.tap {|o| def o.toAngle__(a); a * 2 * P::RAD2DEG__; end}
331
+
332
+ v = vec 1, 0, 0
333
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), v.rotate(angle)
334
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), v
335
+
336
+ v = vec 1, 0, 0, context: context
337
+ assert_equal_vector vec(M.cos(angle * 2), M.sin(angle * 2), 0), v.rotate(angle)
338
+ end
339
+
340
+ def test_fromAngle()
341
+ angle = PI * 2 * 0.1
342
+ result = vec
343
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), V.fromAngle(angle)
344
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), V.fromAngle(angle, result)
345
+ assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), result
346
+ end
347
+
348
+ def test_heading()
349
+ angle = PI * 1 * 0.1
350
+ assert_in_delta( angle, V.fromAngle( angle).heading, 0.000001)
351
+ assert_in_delta(-angle, V.fromAngle(-angle).heading, 0.000001)
352
+ end
353
+
354
+ def test_angleBetween()
355
+ v1 = V.fromAngle PI * 0.25
356
+ v2 = V.fromAngle PI * 0.75
357
+ assert_in_delta PI / 2, V.angleBetween(v1, v2), 0.000001
358
+ end
359
+
360
+ def test_lerp()
361
+ assert_equal_vector vec(0.5, 0.5, 0.5), vec(0, 0, 0).lerp(vec(1, 1, 1), 0.5)
362
+ assert_equal_vector vec(0.5, 0.5, 0.5), vec(0, 0, 0).lerp( 1, 1, 1, 0.5)
363
+ assert_equal_vector vec(0.5, 0.5, 0.5), V.lerp(vec(0, 0, 0), vec(1, 1, 1), 0.5)
364
+ end
365
+
366
+ def test_random2D()
367
+ v1 = V.random2D
368
+ v2 = V.random2D
369
+ assert v1.x != 0
370
+ assert v1.y != 0
371
+ assert_equal 0, v1.z
372
+ assert v2.x != 0
373
+ assert v2.y != 0
374
+ assert_equal 0, v2.z
375
+ assert_not_equal v1, v2
376
+ assert_in_delta 1, v1.mag, 0.000001
377
+ assert_in_delta 1, v2.mag, 0.000001
378
+ end
379
+
380
+ def test_random3D()
381
+ v1 = V.random3D
382
+ v2 = V.random3D
383
+ assert v1.x != 0
384
+ assert v1.y != 0
385
+ assert v1.z != 0
386
+ assert v2.x != 0
387
+ assert v2.y != 0
388
+ assert v2.z != 0
389
+ assert_not_equal v1, v2
390
+ assert_in_delta 1, v1.mag, 0.000001
391
+ assert_in_delta 1, v2.mag, 0.000001
392
+ end
393
+
394
+ end# TestProcessingVector
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: processing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - xordog
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: reflexion
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.29
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.29
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Creative Coding Framework has API compatible to Processing API or p5.js.
42
+ email: xordog@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".github/workflows/release.yml"
48
+ - ".github/workflows/test.yml"
49
+ - ".gitignore"
50
+ - ".yardopts"
51
+ - ChangeLog.md
52
+ - Gemfile
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - RubyProcessing.podspec
57
+ - VERSION
58
+ - examples/breakout.rb
59
+ - examples/camera.rb
60
+ - examples/clock.rb
61
+ - examples/delay_camera.rb
62
+ - examples/hello.rb
63
+ - examples/image.rb
64
+ - examples/shapes.rb
65
+ - lib/processing.rb
66
+ - lib/processing/app.rb
67
+ - lib/processing/include.rb
68
+ - lib/processing/module.rb
69
+ - lib/processing/processing.rb
70
+ - lib/processing/window.rb
71
+ - lib/rubysketch-processing.rb
72
+ - lib/rubysketch.rb
73
+ - processing.gemspec
74
+ - src/RubyProcessing.h
75
+ - src/RubyProcessing.mm
76
+ - test/helper.rb
77
+ - test/processing/helper.rb
78
+ - test/processing/test_graphics.rb
79
+ - test/processing/test_shader.rb
80
+ - test/processing/test_utility.rb
81
+ - test/processing/test_vector.rb
82
+ homepage: https://github.com/xord/processing
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.7.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.2.33
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Processing compatible Creative Coding Framework.
104
+ test_files:
105
+ - test/helper.rb
106
+ - test/processing/helper.rb
107
+ - test/processing/test_graphics.rb
108
+ - test/processing/test_shader.rb
109
+ - test/processing/test_utility.rb
110
+ - test/processing/test_vector.rb