Class: Processing::Vector

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/processing/vector.rb

Overview

Vector class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#newVector #new(x) ⇒ Vector #new(x, y) ⇒ Vector #new(x, y, z) ⇒ Vector #new(v) ⇒ Vector #new(a) ⇒ Vector

Initialize vector object.

Parameters:

  • x (Numeric) (defaults to: 0)

    x of vector

  • y (Numeric) (defaults to: 0)

    y of vector

  • z (Numeric) (defaults to: 0)

    z of vector

  • v (Vector)

    vector object to copy

  • a (Array)

    array like [x, y, z]



25
26
27
28
29
30
31
32
33
# File 'lib/processing/vector.rb', line 25

def initialize(x = 0, y = 0, z = 0, context: nil)
  @point = case x
    when Rays::Point then x.dup
    when Vector      then x.getInternal__.dup
    when Array       then Rays::Point.new x[0] || 0, x[1] || 0, x[2] || 0
    else                  Rays::Point.new x    || 0, y    || 0, z    || 0
    end
  @context = context || Context.context__
end

Class Method Details

.add(v1, v2) ⇒ Vector .add(v1, v2, target) ⇒ Vector

Adds 2 vectors.

Parameters:

  • v1 (Vector)

    a vector

  • v2 (Vector)

    another vector

  • target (Vector) (defaults to: nil)

    vector to store added vector

Returns:



271
272
273
274
275
# File 'lib/processing/vector.rb', line 271

def self.add(v1, v2, target = nil)
  v = v1 + v2
  target.set v if self === target
  v
end

.angleBetween(v1, v2) ⇒ Numeric

Returns angle between 2 vectors.

Parameters:

Returns:

  • (Numeric)

    angle in radians



500
501
502
503
504
505
506
507
508
509
# File 'lib/processing/vector.rb', line 500

def self.angleBetween(v1, v2)
  x1, y1, z1 = v1.array
  x2, y2, z2 = v2.array
  return 0 if (x1 == 0 && y1 == 0 && z1 == 0) || (x2 == 0 && y2 == 0 && z2 == 0)

  x = dot(v1, v2) / (v1.mag * v2.mag)
  return Math::PI if x <= -1
  return 0        if x >= 1
  return Math.acos x
end

.cross(v1, v2, target = nil) ⇒ Numeric

Calculates the cross product of 2 vectors.

Parameters:

Returns:

  • (Numeric)

    result of cross product



455
456
457
# File 'lib/processing/vector.rb', line 455

def self.cross(v1, v2, target = nil)
  v1.cross v2, target
end

.dist(v1, v2) ⇒ Numeric

Returns the distance of 2 vectors.

Parameters:

Returns:

  • (Numeric)

    the distance



396
397
398
# File 'lib/processing/vector.rb', line 396

def self.dist(v1, v2)
  v1.dist v2
end

.div(v, num) ⇒ Vector .div(v, num, target) ⇒ Vector

Divides a vector by scalar.

Parameters:

  • v (Vector)

    a vector

  • num (Numeric)

    number to divide the vector

  • target (Vector) (defaults to: nil)

    vector to store divided vector

Returns:



322
323
324
325
326
# File 'lib/processing/vector.rb', line 322

def self.div(v1, num, target = nil)
  v = v1 / num
  target.set v if self === target
  v
end

.dot(v1, v2) ⇒ Numeric

Calculates the dot product of 2 vectors.

Parameters:

Returns:

  • (Numeric)

    result of dot product



424
425
426
# File 'lib/processing/vector.rb', line 424

def self.dot(v1, v2)
  v1.dot v2
end

.fromAngle(angle, target = nil) ⇒ Vector

Returns rotated new vector.

Parameters:

  • angle (Numeric)

    the angle of rotation

  • target (Vector) (defaults to: nil)

    vector to store new vector

Returns:



487
488
489
490
491
# File 'lib/processing/vector.rb', line 487

def self.fromAngle(angle, target = nil)
  v = self.new(1, 0, 0).rotate(angle)
  target.set v if target
  v
end

.lerp(v1, v2, amount) ⇒ Vector

Returns the interpolated vector between 2 vectors.

Parameters:

  • v1 (Vector)

    vector to interpolate

  • v2 (Vector)

    vector to interpolate

  • amount (Numeric)

    amount to interpolate

Returns:

  • (Vector)

    interporated vector



146
147
148
# File 'lib/processing/vector.rb', line 146

def self.lerp(v1, v2, amount)
  v1.dup.lerp v2, amount
end

.mult(v, num) ⇒ Vector .mult(v, num, target) ⇒ Vector

Multiplies a vector by scalar.

Parameters:

  • v (Vector)

    a vector

  • num (Numeric)

    number to multiply the vector

  • target (Vector) (defaults to: nil)

    vector to store multiplied vector

Returns:

  • (Vector)

    multiplied vector



305
306
307
308
309
# File 'lib/processing/vector.rb', line 305

def self.mult(v1, num, target = nil)
  v = v1 * num
  target.set v if self === target
  v
end

.random2D(target = nil) ⇒ Vector

Returns a new 2D unit vector with a random direction.

Parameters:

  • target (Vector) (defaults to: nil)

    a vector to store the new vector

Returns:

  • (Vector)

    a random vector



517
518
519
520
521
522
# File 'lib/processing/vector.rb', line 517

def self.random2D(target = nil)
  v = self.new(1, 0, 0)
  v.getInternal__.rotate! rand 0.0...360.0
  target.set v if target
  v
end

.random3D(target = nil) ⇒ Vector

Returns a new 3D unit vector with a random direction.

Parameters:

  • target (Vector) (defaults to: nil)

    a vector to store the new vector

Returns:

  • (Vector)

    a random vector



530
531
532
533
534
535
536
537
538
539
# File 'lib/processing/vector.rb', line 530

def self.random3D(target = nil)
  angle = rand 0.0...(Math::PI * 2)
  z     = rand(-1.0..1.0)
  z2    = z ** 2
  x     = Math.sqrt(1.0 - z2) * Math.cos(angle)
  y     = Math.sqrt(1.0 - z2) * Math.sin(angle)
  v     = self.new x, y, z
  target.set v if target
  v
end

.sub(v1, v2) ⇒ Vector .sub(v1, v2, target) ⇒ Vector

Subtracts 2 vectors.

Parameters:

  • v1 (Vector)

    a vector

  • v2 (Vector)

    another vector

  • target (Vector) (defaults to: nil)

    vector to store subtracted vector

Returns:

  • (Vector)

    subtracted vector



288
289
290
291
292
# File 'lib/processing/vector.rb', line 288

def self.sub(v1, v2, target = nil)
  v = v1 - v2
  target.set v if self === target
  v
end

Instance Method Details

#*(num) ⇒ Vector

Multiplies a vector by scalar.

Parameters:

  • num (Numeric)

    number to multiply the vector

Returns:

  • (Vector)

    multiplied vector



246
247
248
# File 'lib/processing/vector.rb', line 246

def *(num)
  dup.mult num
end

#+(v) ⇒ Vector

Adds a vector.

Parameters:

  • v (Vector)

    vector to add

Returns:



226
227
228
# File 'lib/processing/vector.rb', line 226

def +(v)
  dup.add v
end

#-(v) ⇒ Vector

Subtracts a vector.

Parameters:

  • v (Vector)

    vector to subtract

Returns:

  • (Vector)

    subtracted vector



236
237
238
# File 'lib/processing/vector.rb', line 236

def -(v)
  dup.sub v
end

#/(num) ⇒ Vector

Divides a vector by scalar.

Parameters:

  • num (Numeric)

    number to divide the vector

Returns:



256
257
258
# File 'lib/processing/vector.rb', line 256

def /(num)
  dup.div num
end

#add(v) ⇒ Vector #add(x, y) ⇒ Vector #add(x, y, z) ⇒ Vector

Adds a vector.

Parameters:

  • v (Vector)

    vector to add

  • x (Vector)

    x of vector to add

  • y (Vector)

    y of vector to add

  • z (Vector)

    z of vector to add

Returns:



175
176
177
178
# File 'lib/processing/vector.rb', line 175

def add(*args)
  @point += toVector__(*args).getInternal__
  self
end

#array(n = 3) ⇒ Array Also known as: to_a

Returns x, y, z as an array

Parameters:

  • n (Numeric) (defaults to: 3)

    number of dimensions

Returns:

  • (Array)

    array of x, y, z



156
157
158
# File 'lib/processing/vector.rb', line 156

def array(n = 3)
  @point.to_a n
end

#cross(v) ⇒ Numeric #cross(x, y) ⇒ Numeric #cross(x, y, z) ⇒ Numeric

Calculates the cross product of 2 vectors.

Parameters:

  • v (Vector)

    a vector

  • x (Numeric)

    x of vector

  • y (Numeric)

    y of vector

  • z (Numeric)

    z of vector

Returns:

  • (Numeric)

    result of cross product



441
442
443
444
445
446
# File 'lib/processing/vector.rb', line 441

def cross(a, *rest)
  target = self.class === rest.last ? rest.pop : nil
  v = self.class.new Rays::Point::cross getInternal__, toVector__(a, *rest).getInternal__
  target.set v if self.class === target
  v
end

#dist(v) ⇒ Numeric

Returns the distance of 2 vectors.

Parameters:

Returns:

  • (Numeric)

    the distance



385
386
387
# File 'lib/processing/vector.rb', line 385

def dist(v)
  (self - v).mag
end

#div(num) ⇒ Vector

Divides a vector by scalar.

Parameters:

  • num (Numeric)

    number to divide the vector

Returns:



215
216
217
218
# File 'lib/processing/vector.rb', line 215

def div(num)
  @point /= num
  self
end

#dot(v) ⇒ Numeric #dot(x, y) ⇒ Numeric #dot(x, y, z) ⇒ Numeric

Calculates the dot product of 2 vectors.

Parameters:

  • v (Vector)

    a vector

  • x (Numeric)

    x of vector

  • y (Numeric)

    y of vector

  • z (Numeric)

    z of vector

Returns:

  • (Numeric)

    result of dot product



413
414
415
# File 'lib/processing/vector.rb', line 413

def dot(*args)
  Rays::Point::dot getInternal__, toVector__(*args).getInternal__
end

#headingNumeric

Returns the angle of rotation for this vector.

Returns:

  • (Numeric)

    the angle in radians



476
477
478
# File 'lib/processing/vector.rb', line 476

def heading()
  Math.atan2 y, x
end

#initialize_copy(o) ⇒ Object

Initializer for dup or clone



37
38
39
# File 'lib/processing/vector.rb', line 37

def initialize_copy(o)
  @point = o.getInternal__.dup
end

#inspectString

Returns a string containing a human-readable representation of object.

Returns:

  • (String)

    inspected text



545
546
547
# File 'lib/processing/vector.rb', line 545

def inspect()
  "#<#{self.class.name}: #{x}, #{y}, #{z}>"
end

#lerp(v, amount) ⇒ Vector #lerp(x, y, amount) ⇒ Vector #lerp(x, y, z, amount) ⇒ Vector

Returns the interpolated vector between 2 vectors.

Parameters:

  • v (Vector)

    vector to interpolate

  • x (Numeric)

    x of vector to interpolate

  • y (Numeric)

    y of vector to interpolate

  • z (Numeric)

    z of vector to interpolate

  • amount (Numeric)

    amount to interpolate

Returns:

  • (Vector)

    interporated vector



130
131
132
133
134
135
136
# File 'lib/processing/vector.rb', line 130

def lerp(*args, amount)
  v      = toVector__(*args)
  self.x = x + (v.x - x) * amount
  self.y = y + (v.y - y) * amount
  self.z = z + (v.z - z) * amount
  self
end

#limit(max) ⇒ Vector

Changes the length of the vector if it’s length is greater than the max value.

Parameters:

  • max (Numeric)

    max length

Returns:



374
375
376
377
# File 'lib/processing/vector.rb', line 374

def limit(max)
  setMag max if magSq > max ** 2
  self
end

#magNumeric

Returns the length of the vector.

Returns:

  • (Numeric)

    length



332
333
334
# File 'lib/processing/vector.rb', line 332

def mag()
  @point.length
end

#magSqNumeric

Returns squared length of the vector.

Returns:

  • (Numeric)

    squared length



340
341
342
# File 'lib/processing/vector.rb', line 340

def magSq()
  Rays::Point::dot(@point, @point)
end

#mult(num) ⇒ Vector

Multiplies a vector by scalar.

Parameters:

  • num (Numeric)

    number to multiply the vector

Returns:

  • (Vector)

    multiplied vector



204
205
206
207
# File 'lib/processing/vector.rb', line 204

def mult(num)
  @point *= num
  self
end

#normalize(target = nil) ⇒ Vector

Changes the length of the vector to 1.0.

Parameters:

  • target (Vector) (defaults to: nil)

    vector to store the normalized vector

Returns:

  • (Vector)

    normalized vector



364
365
366
# File 'lib/processing/vector.rb', line 364

def normalize(target = nil)
  (target || self).set @point.normal
end

#rotate(angle) ⇒ Vector

Rotate the vector.

Parameters:

  • angle (Numeric)

    the angle of rotation

Returns:

  • (Vector)

    rotated this object



465
466
467
468
469
470
# File 'lib/processing/vector.rb', line 465

def rotate(angle)
  angle = @context ?
    @context.toAngle__(angle) : angle * GraphicsContext::RAD2DEG__
  @point.rotate! angle
  self
end

#set(x) ⇒ nil #set(x, y) ⇒ nil #set(x, y, z) ⇒ nil #set(v) ⇒ nil #set(a) ⇒ nil

Sets x, y and z.

Parameters:

  • x (Numeric)

    x of vector

  • y (Numeric)

    y of vector

  • z (Numeric)

    z of vector

  • v (Vector)

    vector object to copy

  • a (Array)

    array with x, y, z

Returns:

  • (nil)

    nil



63
64
65
66
# File 'lib/processing/vector.rb', line 63

def set(*args)
  initialize(*args)
  self
end

#setMag(len) ⇒ Vector #setMag(target, len) ⇒ Vector

Changes the length of the vector.

Parameters:

  • len (Numeric)

    length of new vector

  • target (Vector) (defaults to: nil)

    vector to store new vector

Returns:

  • (Vector)

    vector with new length



354
355
356
# File 'lib/processing/vector.rb', line 354

def setMag(target = nil, len)
  (target || self).set @point.normal * len
end

#sub(v) ⇒ Vector #sub(x, y) ⇒ Vector #sub(x, y, z) ⇒ Vector

Subtracts a vector.

Parameters:

  • v (Vector)

    vector to subtract

  • x (Vector)

    x of vector to subtract

  • y (Vector)

    y of vector to subtract

  • z (Vector)

    z of vector to subtract

Returns:

  • (Vector)

    subtracted vector



193
194
195
196
# File 'lib/processing/vector.rb', line 193

def sub(*args)
  @point -= toVector__(*args).getInternal__
  self
end

#xNumeric

Gets x value.

Returns:

  • (Numeric)

    x value of vector



72
73
74
# File 'lib/processing/vector.rb', line 72

def x()
  @point.x
end

#x=(x) ⇒ Numeric

Sets x value.

Returns:

  • (Numeric)

    x value of vector



96
97
98
# File 'lib/processing/vector.rb', line 96

def x=(x)
  @point.x = x
end

#yNumeric

Gets y value.

Returns:

  • (Numeric)

    y value of vector



80
81
82
# File 'lib/processing/vector.rb', line 80

def y()
  @point.y
end

#y=(y) ⇒ Numeric

Sets y value.

Returns:

  • (Numeric)

    y value of vector



104
105
106
# File 'lib/processing/vector.rb', line 104

def y=(y)
  @point.y = y
end

#zNumeric

Gets z value.

Returns:

  • (Numeric)

    z value of vector



88
89
90
# File 'lib/processing/vector.rb', line 88

def z()
  @point.z
end

#z=(z) ⇒ Numeric

Sets z value.

Returns:

  • (Numeric)

    z value of vector



112
113
114
# File 'lib/processing/vector.rb', line 112

def z=(z)
  @point.z = z
end