Class: Processing::Vector
- Inherits:
-
Object
- Object
- Processing::Vector
- Includes:
- Comparable
- Defined in:
- lib/processing/vector.rb
Overview
Vector class.
Class Method Summary collapse
-
.add(v1, v2, target = nil) ⇒ Vector
Adds 2 vectors.
-
.angleBetween(v1, v2) ⇒ Numeric
Returns angle between 2 vectors.
-
.cross(v1, v2, target = nil) ⇒ Numeric
Calculates the cross product of 2 vectors.
-
.dist(v1, v2) ⇒ Numeric
Returns the distance of 2 vectors.
-
.div(v1, num, target = nil) ⇒ Vector
Divides a vector by scalar.
-
.dot(v1, v2) ⇒ Numeric
Calculates the dot product of 2 vectors.
-
.fromAngle(angle, target = nil) ⇒ Vector
Returns rotated new vector.
-
.lerp(v1, v2, amount) ⇒ Vector
Returns the interpolated vector between 2 vectors.
-
.mult(v1, num, target = nil) ⇒ Vector
Multiplies a vector by scalar.
-
.random2D(target = nil) ⇒ Vector
Returns a new 2D unit vector with a random direction.
-
.random3D(target = nil) ⇒ Vector
Returns a new 3D unit vector with a random direction.
-
.sub(v1, v2, target = nil) ⇒ Vector
Subtracts 2 vectors.
Instance Method Summary collapse
-
#*(num) ⇒ Vector
Multiplies a vector by scalar.
-
#+(v) ⇒ Vector
Adds a vector.
-
#-(v) ⇒ Vector
Subtracts a vector.
-
#/(num) ⇒ Vector
Divides a vector by scalar.
-
#add(*args) ⇒ Vector
Adds a vector.
-
#array ⇒ Array
Returns x, y, z as an array.
-
#cross(a, *rest) ⇒ Numeric
Calculates the cross product of 2 vectors.
-
#dist(v) ⇒ Numeric
Returns the distance of 2 vectors.
-
#div(num) ⇒ Vector
Divides a vector by scalar.
-
#dot(*args) ⇒ Numeric
Calculates the dot product of 2 vectors.
-
#heading ⇒ Numeric
Returns the angle of rotation for this vector.
-
#initialize(x = 0, y = 0, z = 0, context: nil) ⇒ Vector
constructor
Initialize vector object.
-
#initialize_copy(o) ⇒ Object
Initializer for dup or clone.
-
#lerp(*args, amount) ⇒ Vector
Returns the interpolated vector between 2 vectors.
-
#limit(max) ⇒ Vector
Changes the length of the vector if it’s length is greater than the max value.
-
#mag ⇒ Numeric
Returns the length of the vector.
-
#magSq ⇒ Numeric
Returns squared length of the vector.
-
#mult(num) ⇒ Vector
Multiplies a vector by scalar.
-
#normalize(target = nil) ⇒ Vector
Changes the length of the vector to 1.0.
-
#rotate(angle) ⇒ Vector
Rotate the vector.
-
#set(*args) ⇒ nil
Sets x, y and z.
-
#setMag(target = nil, len) ⇒ Vector
Changes the length of the vector.
-
#sub(*args) ⇒ Vector
Subtracts a vector.
-
#x ⇒ Numeric
Gets x value.
-
#x=(x) ⇒ Numeric
Sets x value.
-
#y ⇒ Numeric
Gets y value.
-
#y=(y) ⇒ Numeric
Sets y value.
-
#z ⇒ Numeric
Gets z value.
-
#z=(z) ⇒ Numeric
Sets z value.
Constructor Details
#new ⇒ Vector #new(x) ⇒ Vector #new(x, y) ⇒ Vector #new(x, y, z) ⇒ Vector #new(v) ⇒ Vector #new(a) ⇒ Vector
Initialize vector object.
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.
267 268 269 270 271 |
# File 'lib/processing/vector.rb', line 267 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.
496 497 498 499 500 501 502 503 504 505 |
# File 'lib/processing/vector.rb', line 496 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.
451 452 453 |
# File 'lib/processing/vector.rb', line 451 def self.cross(v1, v2, target = nil) v1.cross v2, target end |
.dist(v1, v2) ⇒ Numeric
Returns the distance of 2 vectors.
392 393 394 |
# File 'lib/processing/vector.rb', line 392 def self.dist(v1, v2) v1.dist v2 end |
.div(v, num) ⇒ Vector .div(v, num, target) ⇒ Vector
Divides a vector by scalar.
318 319 320 321 322 |
# File 'lib/processing/vector.rb', line 318 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.
420 421 422 |
# File 'lib/processing/vector.rb', line 420 def self.dot(v1, v2) v1.dot v2 end |
.fromAngle(angle, target = nil) ⇒ Vector
Returns rotated new vector.
483 484 485 486 487 |
# File 'lib/processing/vector.rb', line 483 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.
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.
301 302 303 304 305 |
# File 'lib/processing/vector.rb', line 301 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.
513 514 515 516 517 |
# File 'lib/processing/vector.rb', line 513 def self.random2D(target = nil) v = self.fromAngle rand 0.0...(Math::PI * 2) target.set v if target v end |
.random3D(target = nil) ⇒ Vector
Returns a new 3D unit vector with a random direction.
525 526 527 528 529 530 531 532 533 534 |
# File 'lib/processing/vector.rb', line 525 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 |
Instance Method Details
#*(num) ⇒ Vector
Multiplies a vector by scalar.
242 243 244 |
# File 'lib/processing/vector.rb', line 242 def *(num) dup.mult num end |
#+(v) ⇒ Vector
Adds a vector.
222 223 224 |
# File 'lib/processing/vector.rb', line 222 def +(v) dup.add v end |
#-(v) ⇒ Vector
Subtracts a vector.
232 233 234 |
# File 'lib/processing/vector.rb', line 232 def -(v) dup.sub v end |
#/(num) ⇒ Vector
Divides a vector by scalar.
252 253 254 |
# File 'lib/processing/vector.rb', line 252 def /(num) dup.div num end |
#add(v) ⇒ Vector #add(x, y) ⇒ Vector #add(x, y, z) ⇒ Vector
Adds a vector.
171 172 173 174 |
# File 'lib/processing/vector.rb', line 171 def add(*args) @point += toVector__(*args).getInternal__ self end |
#array ⇒ Array
Returns x, y, z as an array
154 155 156 |
# File 'lib/processing/vector.rb', line 154 def array() @point.to_a 3 end |
#cross(v) ⇒ Numeric #cross(x, y) ⇒ Numeric #cross(x, y, z) ⇒ Numeric
Calculates the cross product of 2 vectors.
437 438 439 440 441 442 |
# File 'lib/processing/vector.rb', line 437 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.
381 382 383 |
# File 'lib/processing/vector.rb', line 381 def dist(v) (self - v).mag end |
#div(num) ⇒ Vector
Divides a vector by scalar.
211 212 213 214 |
# File 'lib/processing/vector.rb', line 211 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.
409 410 411 |
# File 'lib/processing/vector.rb', line 409 def dot(*args) Rays::Point::dot getInternal__, toVector__(*args).getInternal__ end |
#heading ⇒ Numeric
Returns the angle of rotation for this vector.
472 473 474 |
# File 'lib/processing/vector.rb', line 472 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 |
#lerp(v, amount) ⇒ Vector #lerp(x, y, amount) ⇒ Vector #lerp(x, y, z, amount) ⇒ Vector
Returns the interpolated vector between 2 vectors.
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.
370 371 372 373 |
# File 'lib/processing/vector.rb', line 370 def limit(max) setMag max if magSq > max ** 2 self end |
#mag ⇒ Numeric
Returns the length of the vector.
328 329 330 |
# File 'lib/processing/vector.rb', line 328 def mag() @point.length end |
#magSq ⇒ Numeric
Returns squared length of the vector.
336 337 338 |
# File 'lib/processing/vector.rb', line 336 def magSq() Rays::Point::dot(@point, @point) end |
#mult(num) ⇒ Vector
Multiplies a vector by scalar.
200 201 202 203 |
# File 'lib/processing/vector.rb', line 200 def mult(num) @point *= num self end |
#normalize(target = nil) ⇒ Vector
Changes the length of the vector to 1.0.
360 361 362 |
# File 'lib/processing/vector.rb', line 360 def normalize(target = nil) (target || self).set @point.normal end |
#rotate(angle) ⇒ Vector
Rotate the vector.
461 462 463 464 465 466 |
# File 'lib/processing/vector.rb', line 461 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.
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.
350 351 352 |
# File 'lib/processing/vector.rb', line 350 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.
189 190 191 192 |
# File 'lib/processing/vector.rb', line 189 def sub(*args) @point -= toVector__(*args).getInternal__ self end |
#x ⇒ Numeric
Gets x value.
72 73 74 |
# File 'lib/processing/vector.rb', line 72 def x() @point.x end |
#x=(x) ⇒ Numeric
Sets x value.
96 97 98 |
# File 'lib/processing/vector.rb', line 96 def x=(x) @point.x = x end |
#y ⇒ Numeric
Gets y value.
80 81 82 |
# File 'lib/processing/vector.rb', line 80 def y() @point.y end |
#y=(y) ⇒ Numeric
Sets y value.
104 105 106 |
# File 'lib/processing/vector.rb', line 104 def y=(y) @point.y = y end |
#z ⇒ Numeric
Gets z value.
88 89 90 |
# File 'lib/processing/vector.rb', line 88 def z() @point.z end |
#z=(z) ⇒ Numeric
Sets z value.
112 113 114 |
# File 'lib/processing/vector.rb', line 112 def z=(z) @point.z = z end |