processing 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,559 @@
1
+ module Processing
2
+
3
+
4
+ # Vector class.
5
+ #
6
+ class Vector
7
+
8
+ include Comparable
9
+
10
+ # Initialize vector object.
11
+ #
12
+ # @overload new()
13
+ # @overload new(x)
14
+ # @overload new(x, y)
15
+ # @overload new(x, y, z)
16
+ # @overload new(v)
17
+ # @overload new(a)
18
+ #
19
+ # @param x [Numeric] x of vector
20
+ # @param y [Numeric] y of vector
21
+ # @param z [Numeric] z of vector
22
+ # @param v [Vector] vector object to copy
23
+ # @param a [Array] array like [x, y, z]
24
+ #
25
+ def initialize(x = 0, y = 0, z = 0, context: nil)
26
+ @point = case x
27
+ when Rays::Point then x.dup
28
+ when Vector then x.getInternal__.dup
29
+ when Array then Rays::Point.new x[0] || 0, x[1] || 0, x[2] || 0
30
+ else Rays::Point.new x || 0, y || 0, z || 0
31
+ end
32
+ @context = context || Context.context__
33
+ end
34
+
35
+ # Initializer for dup or clone
36
+ #
37
+ def initialize_copy(o)
38
+ @point = o.getInternal__.dup
39
+ end
40
+
41
+ # Copy vector object
42
+ #
43
+ # @return [Vector] duplicated vector object
44
+ #
45
+ alias copy dup
46
+
47
+ # Sets x, y and z.
48
+ #
49
+ # @overload set(x)
50
+ # @overload set(x, y)
51
+ # @overload set(x, y, z)
52
+ # @overload set(v)
53
+ # @overload set(a)
54
+ #
55
+ # @param x [Numeric] x of vector
56
+ # @param y [Numeric] y of vector
57
+ # @param z [Numeric] z of vector
58
+ # @param v [Vector] vector object to copy
59
+ # @param a [Array] array with x, y, z
60
+ #
61
+ # @return [nil] nil
62
+ #
63
+ def set(*args)
64
+ initialize(*args)
65
+ self
66
+ end
67
+
68
+ # Gets x value.
69
+ #
70
+ # @return [Numeric] x value of vector
71
+ #
72
+ def x()
73
+ @point.x
74
+ end
75
+
76
+ # Gets y value.
77
+ #
78
+ # @return [Numeric] y value of vector
79
+ #
80
+ def y()
81
+ @point.y
82
+ end
83
+
84
+ # Gets z value.
85
+ #
86
+ # @return [Numeric] z value of vector
87
+ #
88
+ def z()
89
+ @point.z
90
+ end
91
+
92
+ # Sets x value.
93
+ #
94
+ # @return [Numeric] x value of vector
95
+ #
96
+ def x=(x)
97
+ @point.x = x
98
+ end
99
+
100
+ # Sets y value.
101
+ #
102
+ # @return [Numeric] y value of vector
103
+ #
104
+ def y=(y)
105
+ @point.y = y
106
+ end
107
+
108
+ # Sets z value.
109
+ #
110
+ # @return [Numeric] z value of vector
111
+ #
112
+ def z=(z)
113
+ @point.z = z
114
+ end
115
+
116
+ # Returns the interpolated vector between 2 vectors.
117
+ #
118
+ # @overload lerp(v, amount)
119
+ # @overload lerp(x, y, amount)
120
+ # @overload lerp(x, y, z, amount)
121
+ #
122
+ # @param v [Vector] vector to interpolate
123
+ # @param x [Numeric] x of vector to interpolate
124
+ # @param y [Numeric] y of vector to interpolate
125
+ # @param z [Numeric] z of vector to interpolate
126
+ # @param amount [Numeric] amount to interpolate
127
+ #
128
+ # @return [Vector] interporated vector
129
+ #
130
+ def lerp(*args, amount)
131
+ v = toVector__(*args)
132
+ self.x = x + (v.x - x) * amount
133
+ self.y = y + (v.y - y) * amount
134
+ self.z = z + (v.z - z) * amount
135
+ self
136
+ end
137
+
138
+ # Returns the interpolated vector between 2 vectors.
139
+ #
140
+ # @param v1 [Vector] vector to interpolate
141
+ # @param v2 [Vector] vector to interpolate
142
+ # @param amount [Numeric] amount to interpolate
143
+ #
144
+ # @return [Vector] interporated vector
145
+ #
146
+ def self.lerp(v1, v2, amount)
147
+ v1.dup.lerp v2, amount
148
+ end
149
+
150
+ # Returns x, y, z as an array
151
+ #
152
+ # @return [Array] array of x, y, z
153
+ #
154
+ def array()
155
+ @point.to_a 3
156
+ end
157
+
158
+ # Adds a vector.
159
+ #
160
+ # @overload add(v)
161
+ # @overload add(x, y)
162
+ # @overload add(x, y, z)
163
+ #
164
+ # @param v [Vector] vector to add
165
+ # @param x [Vector] x of vector to add
166
+ # @param y [Vector] y of vector to add
167
+ # @param z [Vector] z of vector to add
168
+ #
169
+ # @return [Vector] added vector
170
+ #
171
+ def add(*args)
172
+ @point += toVector__(*args).getInternal__
173
+ self
174
+ end
175
+
176
+ # Subtracts a vector.
177
+ #
178
+ # @overload sub(v)
179
+ # @overload sub(x, y)
180
+ # @overload sub(x, y, z)
181
+ #
182
+ # @param v [Vector] vector to subtract
183
+ # @param x [Vector] x of vector to subtract
184
+ # @param y [Vector] y of vector to subtract
185
+ # @param z [Vector] z of vector to subtract
186
+ #
187
+ # @return [Vector] subtracted vector
188
+ #
189
+ def sub(*args)
190
+ @point -= toVector__(*args).getInternal__
191
+ self
192
+ end
193
+
194
+ # Multiplies a vector by scalar.
195
+ #
196
+ # @param num [Numeric] number to multiply the vector
197
+ #
198
+ # @return [Vector] multiplied vector
199
+ #
200
+ def mult(num)
201
+ @point *= num
202
+ self
203
+ end
204
+
205
+ # Divides a vector by scalar.
206
+ #
207
+ # @param num [Numeric] number to divide the vector
208
+ #
209
+ # @return [Vector] divided vector
210
+ #
211
+ def div(num)
212
+ @point /= num
213
+ self
214
+ end
215
+
216
+ # Adds a vector.
217
+ #
218
+ # @param v [Vector] vector to add
219
+ #
220
+ # @return [Vector] added vector
221
+ #
222
+ def +(v)
223
+ dup.add v
224
+ end
225
+
226
+ # Subtracts a vector.
227
+ #
228
+ # @param v [Vector] vector to subtract
229
+ #
230
+ # @return [Vector] subtracted vector
231
+ #
232
+ def -(v)
233
+ dup.sub v
234
+ end
235
+
236
+ # Multiplies a vector by scalar.
237
+ #
238
+ # @param num [Numeric] number to multiply the vector
239
+ #
240
+ # @return [Vector] multiplied vector
241
+ #
242
+ def *(num)
243
+ dup.mult num
244
+ end
245
+
246
+ # Divides a vector by scalar.
247
+ #
248
+ # @param num [Numeric] number to divide the vector
249
+ #
250
+ # @return [Vector] divided vector
251
+ #
252
+ def /(num)
253
+ dup.div num
254
+ end
255
+
256
+ # Adds 2 vectors.
257
+ #
258
+ # @overload add(v1, v2)
259
+ # @overload add(v1, v2, target)
260
+ #
261
+ # @param v1 [Vector] a vector
262
+ # @param v2 [Vector] another vector
263
+ # @param target [Vector] vector to store added vector
264
+ #
265
+ # @return [Vector] added vector
266
+ #
267
+ def self.add(v1, v2, target = nil)
268
+ v = v1 + v2
269
+ target.set v if self === target
270
+ v
271
+ end
272
+
273
+ # Subtracts 2 vectors.
274
+ #
275
+ # @overload sub(v1, v2)
276
+ # @overload sub(v1, v2, target)
277
+ #
278
+ # @param v1 [Vector] a vector
279
+ # @param v2 [Vector] another vector
280
+ # @param target [Vector] vector to store subtracted vector
281
+ #
282
+ # @return [Vector] subtracted vector
283
+ #
284
+ def self.sub(v1, v2, target = nil)
285
+ v = v1 - v2
286
+ target.set v if self === target
287
+ v
288
+ end
289
+
290
+ # Multiplies a vector by scalar.
291
+ #
292
+ # @overload mult(v, num)
293
+ # @overload mult(v, num, target)
294
+ #
295
+ # @param v [Vector] a vector
296
+ # @param num [Numeric] number to multiply the vector
297
+ # @param target [Vector] vector to store multiplied vector
298
+ #
299
+ # @return [Vector] multiplied vector
300
+ #
301
+ def self.mult(v1, num, target = nil)
302
+ v = v1 * num
303
+ target.set v if self === target
304
+ v
305
+ end
306
+
307
+ # Divides a vector by scalar.
308
+ #
309
+ # @overload div(v, num)
310
+ # @overload div(v, num, target)
311
+ #
312
+ # @param v [Vector] a vector
313
+ # @param num [Numeric] number to divide the vector
314
+ # @param target [Vector] vector to store divided vector
315
+ #
316
+ # @return [Vector] divided vector
317
+ #
318
+ def self.div(v1, num, target = nil)
319
+ v = v1 / num
320
+ target.set v if self === target
321
+ v
322
+ end
323
+
324
+ # Returns the length of the vector.
325
+ #
326
+ # @return [Numeric] length
327
+ #
328
+ def mag()
329
+ @point.length
330
+ end
331
+
332
+ # Returns squared length of the vector.
333
+ #
334
+ # @return [Numeric] squared length
335
+ #
336
+ def magSq()
337
+ Rays::Point::dot(@point, @point)
338
+ end
339
+
340
+ # Changes the length of the vector.
341
+ #
342
+ # @overload setMag(len)
343
+ # @overload setMag(target, len)
344
+ #
345
+ # @param len [Numeric] length of new vector
346
+ # @param target [Vector] vector to store new vector
347
+ #
348
+ # @return [Vector] vector with new length
349
+ #
350
+ def setMag(target = nil, len)
351
+ (target || self).set @point.normal * len
352
+ end
353
+
354
+ # Changes the length of the vector to 1.0.
355
+ #
356
+ # @param target [Vector] vector to store the normalized vector
357
+ #
358
+ # @return [Vector] normalized vector
359
+ #
360
+ def normalize(target = nil)
361
+ (target || self).set @point.normal
362
+ end
363
+
364
+ # Changes the length of the vector if it's length is greater than the max value.
365
+ #
366
+ # @param max [Numeric] max length
367
+ #
368
+ # @return [Vector] new vector
369
+ #
370
+ def limit(max)
371
+ setMag max if magSq > max ** 2
372
+ self
373
+ end
374
+
375
+ # Returns the distance of 2 vectors.
376
+ #
377
+ # @param v [Vector] a vector
378
+ #
379
+ # @return [Numeric] the distance
380
+ #
381
+ def dist(v)
382
+ (self - v).mag
383
+ end
384
+
385
+ # Returns the distance of 2 vectors.
386
+ #
387
+ # @param v1 [Vector] a vector
388
+ # @param v2 [Vector] another vector
389
+ #
390
+ # @return [Numeric] the distance
391
+ #
392
+ def self.dist(v1, v2)
393
+ v1.dist v2
394
+ end
395
+
396
+ # Calculates the dot product of 2 vectors.
397
+ #
398
+ # @overload dot(v)
399
+ # @overload dot(x, y)
400
+ # @overload dot(x, y, z)
401
+ #
402
+ # @param v [Vector] a vector
403
+ # @param x [Numeric] x of vector
404
+ # @param y [Numeric] y of vector
405
+ # @param z [Numeric] z of vector
406
+ #
407
+ # @return [Numeric] result of dot product
408
+ #
409
+ def dot(*args)
410
+ Rays::Point::dot getInternal__, toVector__(*args).getInternal__
411
+ end
412
+
413
+ # Calculates the dot product of 2 vectors.
414
+ #
415
+ # @param v1 [Vector] a vector
416
+ # @param v2 [Vector] another vector
417
+ #
418
+ # @return [Numeric] result of dot product
419
+ #
420
+ def self.dot(v1, v2)
421
+ v1.dot v2
422
+ end
423
+
424
+ # Calculates the cross product of 2 vectors.
425
+ #
426
+ # @overload cross(v)
427
+ # @overload cross(x, y)
428
+ # @overload cross(x, y, z)
429
+ #
430
+ # @param v [Vector] a vector
431
+ # @param x [Numeric] x of vector
432
+ # @param y [Numeric] y of vector
433
+ # @param z [Numeric] z of vector
434
+ #
435
+ # @return [Numeric] result of cross product
436
+ #
437
+ def cross(a, *rest)
438
+ target = self.class === rest.last ? rest.pop : nil
439
+ v = self.class.new Rays::Point::cross getInternal__, toVector__(a, *rest).getInternal__
440
+ target.set v if self.class === target
441
+ v
442
+ end
443
+
444
+ # Calculates the cross product of 2 vectors.
445
+ #
446
+ # @param v1 [Vector] a vector
447
+ # @param v2 [Vector] another vector
448
+ #
449
+ # @return [Numeric] result of cross product
450
+ #
451
+ def self.cross(v1, v2, target = nil)
452
+ v1.cross v2, target
453
+ end
454
+
455
+ # Rotate the vector.
456
+ #
457
+ # @param angle [Numeric] the angle of rotation
458
+ #
459
+ # @return [Vector] rotated this object
460
+ #
461
+ def rotate(angle)
462
+ angle = @context ?
463
+ @context.toAngle__(angle) : angle * GraphicsContext::RAD2DEG__
464
+ @point.rotate! angle
465
+ self
466
+ end
467
+
468
+ # Returns the angle of rotation for this vector.
469
+ #
470
+ # @return [Numeric] the angle in radians
471
+ #
472
+ def heading()
473
+ Math.atan2 y, x
474
+ end
475
+
476
+ # Returns rotated new vector.
477
+ #
478
+ # @param angle [Numeric] the angle of rotation
479
+ # @param target [Vector] vector to store new vector
480
+ #
481
+ # @return [Vector] rotated vector
482
+ #
483
+ def self.fromAngle(angle, target = nil)
484
+ v = self.new(1, 0, 0).rotate(angle)
485
+ target.set v if target
486
+ v
487
+ end
488
+
489
+ # Returns angle between 2 vectors.
490
+ #
491
+ # @param v1 [Vector] a vector
492
+ # @param v2 [Vector] another vector
493
+ #
494
+ # @return [Numeric] angle in radians
495
+ #
496
+ def self.angleBetween(v1, v2)
497
+ x1, y1, z1 = v1.array
498
+ x2, y2, z2 = v2.array
499
+ return 0 if (x1 == 0 && y1 == 0 && z1 == 0) || (x2 == 0 && y2 == 0 && z2 == 0)
500
+
501
+ x = dot(v1, v2) / (v1.mag * v2.mag)
502
+ return Math::PI if x <= -1
503
+ return 0 if x >= 1
504
+ return Math.acos x
505
+ end
506
+
507
+ # Returns a new 2D unit vector with a random direction.
508
+ #
509
+ # @param target [Vector] a vector to store the new vector
510
+ #
511
+ # @return [Vector] a random vector
512
+ #
513
+ def self.random2D(target = nil)
514
+ v = self.fromAngle rand 0.0...(Math::PI * 2)
515
+ target.set v if target
516
+ v
517
+ end
518
+
519
+ # Returns a new 3D unit vector with a random direction.
520
+ #
521
+ # @param target [Vector] a vector to store the new vector
522
+ #
523
+ # @return [Vector] a random vector
524
+ #
525
+ def self.random3D(target = nil)
526
+ angle = rand 0.0...(Math::PI * 2)
527
+ z = rand(-1.0..1.0)
528
+ z2 = z ** 2
529
+ x = Math.sqrt(1.0 - z2) * Math.cos(angle)
530
+ y = Math.sqrt(1.0 - z2) * Math.sin(angle)
531
+ v = self.new x, y, z
532
+ target.set v if target
533
+ v
534
+ end
535
+
536
+ # @private
537
+ def inspect()
538
+ "<##{self.class.name} #{x}, #{y}, #{z}>"
539
+ end
540
+
541
+ # @private
542
+ def <=>(o)
543
+ @point <=> o.getInternal__
544
+ end
545
+
546
+ # @private
547
+ def getInternal__()
548
+ @point
549
+ end
550
+
551
+ # @private
552
+ private def toVector__(*args)
553
+ self.class === args.first ? args.first : self.class.new(*args)
554
+ end
555
+
556
+ end# Vector
557
+
558
+
559
+ end# Processing
data/lib/processing.rb CHANGED
@@ -1,11 +1,30 @@
1
- require 'set'
2
- require 'digest/sha1'
3
- require 'pathname'
4
- require 'tmpdir'
5
- require 'open-uri'
6
-
7
- require 'reflex'
8
- require 'processing/module'
9
- require 'processing/app'
10
- require 'processing/window'
11
- require 'processing/processing'
1
+ require 'processing/all'
2
+
3
+
4
+ module Processing
5
+ WINDOW = Processing::Window.new {start}
6
+ CONTEXT = Processing::Context.new WINDOW
7
+
8
+ refine Object do
9
+ (CONTEXT.methods - Object.instance_methods).each do |method|
10
+ define_method method do |*args, **kwargs, &block|
11
+ CONTEXT.__send__ method, *args, **kwargs, &block
12
+ end
13
+ end
14
+ end
15
+ end# Processing
16
+
17
+
18
+ begin
19
+ w, c = Processing::WINDOW, Processing::CONTEXT
20
+
21
+ c.class.constants.each do |const|
22
+ self.class.const_set const, c.class.const_get(const)
23
+ end
24
+
25
+ w.__send__ :begin_draw
26
+ at_exit do
27
+ w.__send__ :end_draw
28
+ Processing::App.new {w.show}.start if c.hasDrawBlock__ && !$!
29
+ end
30
+ end
@@ -1 +1 @@
1
- require 'processing/include'
1
+ require 'processing'
data/lib/rubysketch.rb CHANGED
@@ -1 +1 @@
1
- require 'processing'
1
+ require 'processing/all'
data/processing.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
 
19
19
  s.name = name
20
20
  s.summary = 'Processing compatible Creative Coding Framework.'
21
- s.description = 'Creative Coding Framework has API compatible to Processing API or p5.js.'
21
+ s.description = 'Creative Coding Framework has API compatible to Processing or p5.js.'
22
22
  s.version = mod.version
23
23
 
24
24
  s.authors = %w[xordog]
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.platform = Gem::Platform::RUBY
29
29
  s.required_ruby_version = '>= 2.7.0'
30
30
 
31
- s.add_runtime_dependency 'reflexion', '~> 0.1.29'
31
+ s.add_runtime_dependency 'reflexion', '~> 0.1.30'
32
32
 
33
33
  s.add_development_dependency 'yard'
34
34
 
@@ -34,7 +34,7 @@ ReflexViewController_show (UIViewController*, ReflexViewController*)
34
34
  + (void) start: (NSString*) path
35
35
  {
36
36
  [CRuby evaluate:[NSString stringWithFormat:@
37
- "raise 'already started' unless require 'processing/include'\n"
37
+ "raise 'already started' unless require 'processing'\n"
38
38
  "load '%@'\n"
39
39
  "PROCESSING_WINDOW.__send__ :end_draw\n"
40
40
  "PROCESSING_WINDOW.show",
data/test/helper.rb CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
 
4
4
  %w[../xot ../rucy ../rays ../reflex .]
5
- .map {|s| File.expand_path "../../#{s}/lib", __FILE__}
5
+ .map {|s| File.expand_path "../#{s}/lib", __dir__}
6
6
  .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
7
7
 
8
8
  require 'test/unit'
9
9
  require 'xot/test'
10
- require 'processing'
10
+ require 'processing/all'
11
11
 
12
12
  include Xot::Test
13
13
 
@@ -17,3 +17,9 @@ def assert_equal_vector(v1, v2, delta = 0.000001)
17
17
  assert_in_delta v1.y, v2.y, delta
18
18
  assert_in_delta v1.z, v2.z, delta
19
19
  end
20
+
21
+ def graphics(width = 10, height = 10, &block)
22
+ Processing::Graphics.new(width, height).tap do |g|
23
+ g.beginDraw {block.call g, g.getInternal__} if block
24
+ end
25
+ end
@@ -4,9 +4,9 @@
4
4
  require_relative 'helper'
5
5
 
6
6
 
7
- class TestProcessingGraphics < Test::Unit::TestCase
7
+ class TestGraphics < Test::Unit::TestCase
8
8
 
9
- P = Processing::Processing
9
+ P = Processing
10
10
 
11
11
  def graphics(w = 10, h = 10)
12
12
  P::Graphics.new w, h
@@ -18,4 +18,4 @@ class TestProcessingGraphics < Test::Unit::TestCase
18
18
  assert_raise {g.beginDraw}
19
19
  end
20
20
 
21
- end# TestProcessingGraphics
21
+ end# TestGraphics
@@ -4,14 +4,14 @@
4
4
  require_relative 'helper'
5
5
 
6
6
 
7
- class TestProcessingShader < Test::Unit::TestCase
7
+ class TestShader < Test::Unit::TestCase
8
8
 
9
9
  def color(*args)
10
10
  Rays::Color.new(*args)
11
11
  end
12
12
 
13
13
  def shader(vs = vshader, fs = fshader)
14
- Processing::Processing::Shader.new vs, fs
14
+ Processing::Shader.new vs, fs
15
15
  end
16
16
 
17
17
  def vshader()
@@ -44,4 +44,4 @@ class TestProcessingShader < Test::Unit::TestCase
44
44
  end
45
45
  end
46
46
 
47
- end# TestProcessingShader
47
+ end# TestShader
@@ -4,9 +4,9 @@
4
4
  require_relative 'helper'
5
5
 
6
6
 
7
- class TestProcessingUtility < Test::Unit::TestCase
7
+ class TestUtility < Test::Unit::TestCase
8
8
 
9
- P = Processing::Processing
9
+ P = Processing
10
10
 
11
11
  include P::GraphicsContext
12
12
 
@@ -38,4 +38,4 @@ class TestProcessingUtility < Test::Unit::TestCase
38
38
  assert_equal P::Shader, createShader(nil, fs).class
39
39
  end
40
40
 
41
- end# TestProcessingUtility
41
+ end# TestUtility