rubysketch 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83aecf951db1320693714768446ccc95aa0a87f23012e02fff31b032c88be3c6
4
- data.tar.gz: 6d1f67cbe65c15468cb606666acceacf19f56bf8c83dd1abae2a1b21d7daf94b
3
+ metadata.gz: 90fb6bf8a945c62f9ac122dc4df7515aa2ebf271cb100836c76cb8cc1d4f7c50
4
+ data.tar.gz: 53e61d592dbc5090d98e3c6bb015d406f018683767997c3f2f4d53462a9f27b4
5
5
  SHA512:
6
- metadata.gz: 9946ba3029ce56a90042daa9be9e6c2f74f5d7ef7d318b5263b10c03ca35c26f5f7e67c61e6f78bd90f4224c267f732dcb937d86030adbe2b0ac51350a225f6e
7
- data.tar.gz: 627eb17aa1e83da474f9ab9457b449b278ecf95d84956f98b82d3185c6bc87fa9b5788ad65c1144f80364171b14a4f7322d86a42066925ccbca8f2e19ff46cc3
6
+ metadata.gz: deb1f851efbcfd3feff20a7495ad55c9ef3a2d1e9ee416d6b87de89069197cc397a64303c69929f212cb0dda658f739a6ca1f1e83bea3050afa219b889509a9d
7
+ data.tar.gz: ba13f0ce8a03a0b86f1ad20078e0b473d2e6058a446a3059c21f8c9e5ccabcaa72f065bcbfee13bb7b105960f0e658b1ee3e609c44c1429f1d4a9851ce122eab
@@ -1,6 +1,11 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.3] - 2020-08-01
5
+
6
+ - add Vector class
7
+
8
+
4
9
  ## [0.3.2] - 2020-07-22
5
10
 
6
11
  - text() draws to the baseline by default
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ MODULES = [Xot, Rucy, Rays, Reflex, RubySketch]
18
18
 
19
19
  ENV['RDOC'] = 'yardoc --no-private'
20
20
 
21
+ test_ruby_extension
21
22
  generate_documents
22
23
  build_ruby_gem
23
24
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -6,13 +6,567 @@ module RubySketch
6
6
  module Processing
7
7
 
8
8
 
9
+ # Vector class.
10
+ #
11
+ class Vector
12
+
13
+ include Comparable
14
+
15
+ # Initialize vector object.
16
+ #
17
+ # @overload new()
18
+ # @overload new(x)
19
+ # @overload new(x, y)
20
+ # @overload new(x, y, z)
21
+ # @overload new(v)
22
+ # @overload new(a)
23
+ #
24
+ # @param x [Numeric] x of vector
25
+ # @param y [Numeric] y of vector
26
+ # @param z [Numeric] z of vector
27
+ # @param v [Vector] vector object to copy
28
+ # @param a [Array] array like [x, y, z]
29
+ #
30
+ def initialize (x = 0, y = 0, z = 0, context: context)
31
+ @point = case x
32
+ when Rays::Point then x.dup
33
+ when Vector then x.getInternal__.dup
34
+ when Array then Rays::Point.new x[0] || 0, x[1] || 0, x[2] || 0
35
+ else Rays::Point.new x || 0, y || 0, z || 0
36
+ end
37
+ @context = context
38
+ end
39
+
40
+ # Initializer for dup or clone
41
+ #
42
+ def initialize_copy (o)
43
+ @point = o.getInternal__.dup
44
+ end
45
+
46
+ # Copy vector object
47
+ #
48
+ # @return [Vector] duplicated vector object
49
+ #
50
+ alias copy dup
51
+
52
+ # Sets x, y and z.
53
+ #
54
+ # @overload set(x)
55
+ # @overload set(x, y)
56
+ # @overload set(x, y, z)
57
+ # @overload set(v)
58
+ # @overload set(a)
59
+ #
60
+ # @param x [Numeric] x of vector
61
+ # @param y [Numeric] y of vector
62
+ # @param z [Numeric] z of vector
63
+ # @param v [Vector] vector object to copy
64
+ # @param a [Array] array with x, y, z
65
+ #
66
+ # @return [nil] nil
67
+ #
68
+ def set (*args)
69
+ initialize *args
70
+ self
71
+ end
72
+
73
+ # Gets x value.
74
+ #
75
+ # @return [Numeric] x value of vector
76
+ #
77
+ def x ()
78
+ @point.x
79
+ end
80
+
81
+ # Gets y value.
82
+ #
83
+ # @return [Numeric] y value of vector
84
+ #
85
+ def y ()
86
+ @point.y
87
+ end
88
+
89
+ # Gets z value.
90
+ #
91
+ # @return [Numeric] z value of vector
92
+ #
93
+ def z ()
94
+ @point.z
95
+ end
96
+
97
+ # Sets x value.
98
+ #
99
+ # @return [Numeric] x value of vector
100
+ #
101
+ def x= (x)
102
+ @point.x = x
103
+ end
104
+
105
+ # Sets y value.
106
+ #
107
+ # @return [Numeric] y value of vector
108
+ #
109
+ def y= (y)
110
+ @point.y = y
111
+ end
112
+
113
+ # Sets z value.
114
+ #
115
+ # @return [Numeric] z value of vector
116
+ #
117
+ def z= (z)
118
+ @point.z = z
119
+ end
120
+
121
+ # Returns the interpolated vector between 2 vectors.
122
+ #
123
+ # @overload lerp(v, amount)
124
+ # @overload lerp(x, y, amount)
125
+ # @overload lerp(x, y, z, amount)
126
+ #
127
+ # @param v [Vector] vector to interpolate
128
+ # @param x [Numeric] x of vector to interpolate
129
+ # @param y [Numeric] y of vector to interpolate
130
+ # @param z [Numeric] z of vector to interpolate
131
+ # @param amount [Numeric] amount to interpolate
132
+ #
133
+ # @return [Vector] interporated vector
134
+ #
135
+ def lerp (*args, amount)
136
+ v = toVector__ *args
137
+ self.x = x + (v.x - x) * amount
138
+ self.y = y + (v.y - y) * amount
139
+ self.z = z + (v.z - z) * amount
140
+ self
141
+ end
142
+
143
+ # Returns the interpolated vector between 2 vectors.
144
+ #
145
+ # @param v1 [Vector] vector to interpolate
146
+ # @param v2 [Vector] vector to interpolate
147
+ # @param amount [Numeric] amount to interpolate
148
+ #
149
+ # @return [Vector] interporated vector
150
+ #
151
+ def self.lerp (v1, v2, amount)
152
+ v1.dup.lerp v2, amount
153
+ end
154
+
155
+ # Returns x, y, z as an array
156
+ #
157
+ # @return [Array] array of x, y, z
158
+ #
159
+ def array ()
160
+ @point.to_a 3
161
+ end
162
+
163
+ # Adds a vector.
164
+ #
165
+ # @overload add(v)
166
+ # @overload add(x, y)
167
+ # @overload add(x, y, z)
168
+ #
169
+ # @param v [Vector] vector to add
170
+ # @param x [Vector] x of vector to add
171
+ # @param y [Vector] y of vector to add
172
+ # @param z [Vector] z of vector to add
173
+ #
174
+ # @return [Vector] added vector
175
+ #
176
+ def add (*args)
177
+ @point += toVector__(*args).getInternal__
178
+ self
179
+ end
180
+
181
+ # Subtracts a vector.
182
+ #
183
+ # @overload sub(v)
184
+ # @overload sub(x, y)
185
+ # @overload sub(x, y, z)
186
+ #
187
+ # @param v [Vector] vector to subtract
188
+ # @param x [Vector] x of vector to subtract
189
+ # @param y [Vector] y of vector to subtract
190
+ # @param z [Vector] z of vector to subtract
191
+ #
192
+ # @return [Vector] subtracted vector
193
+ #
194
+ def sub (*args)
195
+ @point -= toVector__(*args).getInternal__
196
+ self
197
+ end
198
+
199
+ # Multiplies a vector by scalar.
200
+ #
201
+ # @param num [Numeric] number to multiply the vector
202
+ #
203
+ # @return [Vector] multiplied vector
204
+ #
205
+ def mult (num)
206
+ @point *= num
207
+ self
208
+ end
209
+
210
+ # Divides a vector by scalar.
211
+ #
212
+ # @param num [Numeric] number to divide the vector
213
+ #
214
+ # @return [Vector] divided vector
215
+ #
216
+ def div (num)
217
+ @point /= num
218
+ self
219
+ end
220
+
221
+ # Adds a vector.
222
+ #
223
+ # @param v [Vector] vector to add
224
+ #
225
+ # @return [Vector] added vector
226
+ #
227
+ def + (v)
228
+ dup.add v
229
+ end
230
+
231
+ # Subtracts a vector.
232
+ #
233
+ # @param v [Vector] vector to subtract
234
+ #
235
+ # @return [Vector] subtracted vector
236
+ #
237
+ def - (v)
238
+ dup.sub v
239
+ end
240
+
241
+ # Multiplies a vector by scalar.
242
+ #
243
+ # @param num [Numeric] number to multiply the vector
244
+ #
245
+ # @return [Vector] multiplied vector
246
+ #
247
+ def * (num)
248
+ dup.mult num
249
+ end
250
+
251
+ # Divides a vector by scalar.
252
+ #
253
+ # @param num [Numeric] number to divide the vector
254
+ #
255
+ # @return [Vector] divided vector
256
+ #
257
+ def / (num)
258
+ dup.div num
259
+ end
260
+
261
+ # Adds 2 vectors.
262
+ #
263
+ # @overload add(v1, v2)
264
+ # @overload add(v1, v2, target)
265
+ #
266
+ # @param v1 [Vector] a vector
267
+ # @param v2 [Vector] another vector
268
+ # @param target [Vector] vector to store added vector
269
+ #
270
+ # @return [Vector] added vector
271
+ #
272
+ def self.add (v1, v2, target = nil)
273
+ v = v1 + v2
274
+ target.set v if self === target
275
+ v
276
+ end
277
+
278
+ # Subtracts 2 vectors.
279
+ #
280
+ # @overload sub(v1, v2)
281
+ # @overload sub(v1, v2, target)
282
+ #
283
+ # @param v1 [Vector] a vector
284
+ # @param v2 [Vector] another vector
285
+ # @param target [Vector] vector to store subtracted vector
286
+ #
287
+ # @return [Vector] subtracted vector
288
+ #
289
+ def self.sub (v1, v2, target = nil)
290
+ v = v1 - v2
291
+ target.set v if self === target
292
+ v
293
+ end
294
+
295
+ # Multiplies a vector by scalar.
296
+ #
297
+ # @overload mult(v, num)
298
+ # @overload mult(v, num, target)
299
+ #
300
+ # @param v [Vector] a vector
301
+ # @param num [Numeric] number to multiply the vector
302
+ # @param target [Vector] vector to store multiplied vector
303
+ #
304
+ # @return [Vector] multiplied vector
305
+ #
306
+ def self.mult (v1, num, target = nil)
307
+ v = v1 * num
308
+ target.set v if self === target
309
+ v
310
+ end
311
+
312
+ # Divides a vector by scalar.
313
+ #
314
+ # @overload div(v, num)
315
+ # @overload div(v, num, target)
316
+ #
317
+ # @param v [Vector] a vector
318
+ # @param num [Numeric] number to divide the vector
319
+ # @param target [Vector] vector to store divided vector
320
+ #
321
+ # @return [Vector] divided vector
322
+ #
323
+ def self.div (v1, num, target = nil)
324
+ v = v1 / num
325
+ target.set v if self === target
326
+ v
327
+ end
328
+
329
+ # Returns the length of the vector.
330
+ #
331
+ # @return [Numeric] length
332
+ #
333
+ def mag ()
334
+ @point.length
335
+ end
336
+
337
+ # Returns squared length of the vector.
338
+ #
339
+ # @return [Numeric] squared length
340
+ #
341
+ def magSq ()
342
+ Rays::Point::dot(@point, @point)
343
+ end
344
+
345
+ # Changes the length of the vector.
346
+ #
347
+ # @overload setMag(len)
348
+ # @overload setMag(target, len)
349
+ #
350
+ # @param len [Numeric] length of new vector
351
+ # @param target [Vector] vector to store new vector
352
+ #
353
+ # @return [Vector] vector with new length
354
+ #
355
+ def setMag (target = nil, len)
356
+ (target || self).set @point.normal * len
357
+ end
358
+
359
+ # Changes the length of the vector to 1.0.
360
+ #
361
+ # @param target [Vector] vector to store the normalized vector
362
+ #
363
+ # @return [Vector] normalized vector
364
+ #
365
+ def normalize (target = nil)
366
+ (target || self).set @point.normal
367
+ end
368
+
369
+ # Changes the length of the vector if it's length is greater than the max value.
370
+ #
371
+ # @param max [Numeric] max length
372
+ #
373
+ # @return [Vector] new vector
374
+ #
375
+ def limit (max)
376
+ setMag max if magSq > max ** 2
377
+ self
378
+ end
379
+
380
+ # Returns the distance of 2 vectors.
381
+ #
382
+ # @param v [Vector] a vector
383
+ #
384
+ # @return [Numeric] the distance
385
+ #
386
+ def dist (v)
387
+ (self - v).mag
388
+ end
389
+
390
+ # Returns the distance of 2 vectors.
391
+ #
392
+ # @param v1 [Vector] a vector
393
+ # @param v2 [Vector] another vector
394
+ #
395
+ # @return [Numeric] the distance
396
+ #
397
+ def self.dist (v1, v2)
398
+ v1.dist v2
399
+ end
400
+
401
+ # Calculates the dot product of 2 vectors.
402
+ #
403
+ # @overload dot(v)
404
+ # @overload dot(x, y)
405
+ # @overload dot(x, y, z)
406
+ #
407
+ # @param v [Vector] a vector
408
+ # @param x [Numeric] x of vector
409
+ # @param y [Numeric] y of vector
410
+ # @param z [Numeric] z of vector
411
+ #
412
+ # @return [Numeric] result of dot product
413
+ #
414
+ def dot (*args)
415
+ Rays::Point::dot getInternal__, toVector__(*args).getInternal__
416
+ end
417
+
418
+ # Calculates the dot product of 2 vectors.
419
+ #
420
+ # @param v1 [Vector] a vector
421
+ # @param v2 [Vector] another vector
422
+ #
423
+ # @return [Numeric] result of dot product
424
+ #
425
+ def self.dot (v1, v2)
426
+ v1.dot v2
427
+ end
428
+
429
+ # Calculates the cross product of 2 vectors.
430
+ #
431
+ # @overload cross(v)
432
+ # @overload cross(x, y)
433
+ # @overload cross(x, y, z)
434
+ #
435
+ # @param v [Vector] a vector
436
+ # @param x [Numeric] x of vector
437
+ # @param y [Numeric] y of vector
438
+ # @param z [Numeric] z of vector
439
+ #
440
+ # @return [Numeric] result of cross product
441
+ #
442
+ def cross (a, *rest)
443
+ target = self.class === rest.last ? rest.pop : nil
444
+ v = self.class.new Rays::Point::cross getInternal__, toVector__(a, *rest).getInternal__
445
+ target.set v if self.class === target
446
+ v
447
+ end
448
+
449
+ # Calculates the cross product of 2 vectors.
450
+ #
451
+ # @param v1 [Vector] a vector
452
+ # @param v2 [Vector] another vector
453
+ #
454
+ # @return [Numeric] result of cross product
455
+ #
456
+ def self.cross (v1, v2, target = nil)
457
+ v1.cross v2, target
458
+ end
459
+
460
+ # Rotate the vector.
461
+ #
462
+ # @param angle [Numeric] the angle of rotation
463
+ #
464
+ # @return [Vector] rotated this object
465
+ #
466
+ def rotate (angle)
467
+ angle = @context ? @context.toAngle__(angle) : angle * Utility::RAD2DEG__
468
+ @point.rotate! angle
469
+ self
470
+ end
471
+
472
+ # Returns the angle of rotation for this vector.
473
+ #
474
+ # @return [Numeric] the angle in radians
475
+ #
476
+ def heading ()
477
+ Math.atan2 y, x
478
+ end
479
+
480
+ # Returns rotated new vector.
481
+ #
482
+ # @param angle [Numeric] the angle of rotation
483
+ # @param target [Vector] vector to store new vector
484
+ #
485
+ # @return [Vector] rotated vector
486
+ #
487
+ def self.fromAngle (angle, target = nil)
488
+ v = self.new(1, 0, 0).rotate(angle)
489
+ target.set v if target
490
+ v
491
+ end
492
+
493
+ # Returns angle between 2 vectors.
494
+ #
495
+ # @param v1 [Vector] a vector
496
+ # @param v2 [Vector] another vector
497
+ #
498
+ # @return [Numeric] angle in radians
499
+ #
500
+ def self.angleBetween (v1, v2)
501
+ x1, y1, z1 = v1.array
502
+ x2, y2, z2 = v2.array
503
+ return 0 if (x1 == 0 && y1 == 0 && z1 == 0) || (x2 == 0 && y2 == 0 && z2 == 0)
504
+
505
+ x = dot(v1, v2) / (v1.mag * v2.mag)
506
+ return Math::PI if x <= -1
507
+ return 0 if x >= 1
508
+ return Math.acos x
509
+ end
510
+
511
+ # Returns a new 2D unit vector with a random direction.
512
+ #
513
+ # @param target [Vector] a vector to store the new vector
514
+ #
515
+ # @return [Vector] a random vector
516
+ #
517
+ def self.random2D (target = nil)
518
+ v = self.fromAngle rand 0.0...(Math::PI * 2)
519
+ target.set v if target
520
+ v
521
+ end
522
+
523
+ # Returns a new 3D unit vector with a random direction.
524
+ #
525
+ # @param target [Vector] a vector to store the new vector
526
+ #
527
+ # @return [Vector] a random vector
528
+ #
529
+ def self.random3D (target = nil)
530
+ angle = rand 0.0...(Math::PI * 2)
531
+ z = rand -1.0..1.0
532
+ z2 = z ** 2
533
+ x = Math.sqrt(1.0 - z2) * Math.cos(angle)
534
+ y = Math.sqrt(1.0 - z2) * Math.sin(angle)
535
+ v = self.new x, y, z
536
+ target.set v if target
537
+ v
538
+ end
539
+
540
+ # @private
541
+ def inspect ()
542
+ "<##{self.class.name} #{x}, #{y}, #{z}>"
543
+ end
544
+
545
+ # @private
546
+ def <=> (o)
547
+ @point <=> o.getInternal__
548
+ end
549
+
550
+ # @private
551
+ protected def getInternal__ ()
552
+ @point
553
+ end
554
+
555
+ # @private
556
+ private def toVector__ (*args)
557
+ self.class === args.first ? args.first : self.class.new(*args)
558
+ end
559
+
560
+ end# Vector
561
+
562
+
9
563
  # Image object.
10
564
  #
11
565
  class Image
12
566
 
13
567
  # @private
14
568
  def initialize (image)
15
- @image__ = image
569
+ @image = image
16
570
  end
17
571
 
18
572
  # Gets width of image.
@@ -20,7 +574,7 @@ module RubySketch
20
574
  # @return [Numeric] width of image
21
575
  #
22
576
  def width ()
23
- @image__.width
577
+ @image.width
24
578
  end
25
579
 
26
580
  # Gets height of image.
@@ -28,7 +582,7 @@ module RubySketch
28
582
  # @return [Numeric] height of image
29
583
  #
30
584
  def height ()
31
- @image__.height
585
+ @image.height
32
586
  end
33
587
 
34
588
  # Resizes image.
@@ -39,8 +593,8 @@ module RubySketch
39
593
  # @return [nil] nil
40
594
  #
41
595
  def resize (width, height)
42
- @image__ = Rays::Image.new(width, height).paint do |painter|
43
- painter.image @image__, 0, 0, width, height
596
+ @image = Rays::Image.new(width, height).paint do |painter|
597
+ painter.image @image, 0, 0, width, height
44
598
  end
45
599
  nil
46
600
  end
@@ -64,7 +618,7 @@ module RubySketch
64
618
  #
65
619
  def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
66
620
  img ||= self
67
- @image__.paint do |painter|
621
+ @image.paint do |painter|
68
622
  painter.image img.getInternal__, sx, sy, sw, sh, dx, dy, dw, dh
69
623
  end
70
624
  end
@@ -74,12 +628,12 @@ module RubySketch
74
628
  # @param filename [String] file name to save image
75
629
  #
76
630
  def save (filename)
77
- @image__.save filename
631
+ @image.save filename
78
632
  end
79
633
 
80
634
  # @private
81
635
  def getInternal__ ()
82
- @image__
636
+ @image
83
637
  end
84
638
 
85
639
  end# Image
@@ -369,7 +923,7 @@ module RubySketch
369
923
  end
370
924
 
371
925
  # @private
372
- private def toAngle__ (angle)
926
+ protected def toAngle__ (angle)
373
927
  angle * @angleScale__
374
928
  end
375
929
 
@@ -1025,12 +1579,12 @@ module RubySketch
1025
1579
  end
1026
1580
 
1027
1581
  # @private
1028
- def getInternal__ ()
1582
+ private def getInternal__ ()
1029
1583
  @image__
1030
1584
  end
1031
1585
 
1032
1586
  # @private
1033
- def assertDrawing__ ()
1587
+ private def assertDrawing__ ()
1034
1588
  raise "call beginDraw() before drawing" unless @drawing
1035
1589
  end
1036
1590
 
@@ -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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-22 00:00:00.000000000 Z
11
+ date: 2020-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -120,6 +120,7 @@ files:
120
120
  - lib/rubysketch/processing.rb
121
121
  - lib/rubysketch/window.rb
122
122
  - rubysketch.gemspec
123
+ - test/processing/test_vector.rb
123
124
  homepage: https://github.com/xord/rubysketch
124
125
  licenses: []
125
126
  metadata: {}
@@ -142,4 +143,5 @@ rubygems_version: 3.0.3
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: Processing like Creative Coding Framework.
145
- test_files: []
146
+ test_files:
147
+ - test/processing/test_vector.rb