Class: Geo3d::Vector
- Inherits:
-
Object
- Object
- Geo3d::Vector
- Defined in:
- lib/vector.rb
Instance Attribute Summary collapse
-
#w ⇒ Object
(also: #d)
Returns the value of attribute w.
-
#x ⇒ Object
(also: #a)
Returns the value of attribute x.
-
#y ⇒ Object
(also: #b)
Returns the value of attribute y.
-
#z ⇒ Object
(also: #c)
Returns the value of attribute z.
Instance Method Summary collapse
- #!=(vec) ⇒ Object
- #*(scalar) ⇒ Object
- #+(vec) ⇒ Object
- #+@ ⇒ Object
- #-(vec) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #==(vec) ⇒ Object
- #cross(vec) ⇒ Object
- #dot(vec) ⇒ Object
-
#initialize(*args) ⇒ Vector
constructor
A new instance of Vector.
- #length ⇒ Object
- #length_squared ⇒ Object
- #lerp(vec, s) ⇒ Object
- #normalize ⇒ Object
- #normalize! ⇒ Object
- #one_w ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #zero_w ⇒ Object
Constructor Details
#initialize(*args) ⇒ Vector
Returns a new instance of Vector.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/vector.rb', line 9 def initialize *args @x = 0.0 @y = 0.0 @z = 0.0 @w = 0.0 @x = args[0].to_f if args.size > 0 @y = args[1].to_f if args.size > 1 @z = args[2].to_f if args.size > 2 @w = args[3].to_f if args.size > 3 end |
Instance Attribute Details
#w ⇒ Object Also known as: d
Returns the value of attribute w.
3 4 5 |
# File 'lib/vector.rb', line 3 def w @w end |
#x ⇒ Object Also known as: a
Returns the value of attribute x.
3 4 5 |
# File 'lib/vector.rb', line 3 def x @x end |
#y ⇒ Object Also known as: b
Returns the value of attribute y.
3 4 5 |
# File 'lib/vector.rb', line 3 def y @y end |
#z ⇒ Object Also known as: c
Returns the value of attribute z.
3 4 5 |
# File 'lib/vector.rb', line 3 def z @z end |
Instance Method Details
#!=(vec) ⇒ Object
64 65 66 |
# File 'lib/vector.rb', line 64 def != vec !(self == vec) end |
#*(scalar) ⇒ Object
52 53 54 |
# File 'lib/vector.rb', line 52 def * scalar self.class.new x * scalar, y * scalar, z * scalar, w * scalar end |
#+(vec) ⇒ Object
44 45 46 |
# File 'lib/vector.rb', line 44 def + vec self.class.new x + vec.x, y + vec.y, z + vec.z, w + vec.w end |
#+@ ⇒ Object
36 37 38 |
# File 'lib/vector.rb', line 36 def +@ self * 1 end |
#-(vec) ⇒ Object
48 49 50 |
# File 'lib/vector.rb', line 48 def - vec self.class.new x - vec.x, y - vec.y, z - vec.z, w - vec.w end |
#-@ ⇒ Object
40 41 42 |
# File 'lib/vector.rb', line 40 def -@ self * -1 end |
#/(scalar) ⇒ Object
56 57 58 |
# File 'lib/vector.rb', line 56 def / scalar self.class.new x / scalar, y / scalar, z / scalar, w / scalar end |
#==(vec) ⇒ Object
60 61 62 |
# File 'lib/vector.rb', line 60 def == vec Geo3d::Utils.float_cmp(x, vec.x) && Geo3d::Utils.float_cmp(y, vec.y) && Geo3d::Utils.float_cmp(z, vec.z) && Geo3d::Utils.float_cmp(w, vec.w) end |
#cross(vec) ⇒ Object
68 69 70 |
# File 'lib/vector.rb', line 68 def cross vec self.class.new y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x end |
#dot(vec) ⇒ Object
72 73 74 |
# File 'lib/vector.rb', line 72 def dot vec x * vec.x + y * vec.y + z * vec.z + w * vec.w end |
#length ⇒ Object
92 93 94 |
# File 'lib/vector.rb', line 92 def length Math.sqrt length_squared end |
#length_squared ⇒ Object
96 97 98 |
# File 'lib/vector.rb', line 96 def length_squared dot self end |
#lerp(vec, s) ⇒ Object
100 101 102 |
# File 'lib/vector.rb', line 100 def lerp vec, s self + ( vec - self )*s; end |
#normalize ⇒ Object
86 87 88 89 90 |
# File 'lib/vector.rb', line 86 def normalize v = self.class.new x, y, z, w v.normalize! v end |
#normalize! ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/vector.rb', line 76 def normalize! len = length if length > 0 @x /= len @y /= len @z /= len @w /= len end end |
#one_w ⇒ Object
24 25 26 |
# File 'lib/vector.rb', line 24 def one_w self.class.new x, y, z, 1 end |
#to_a ⇒ Object
32 33 34 |
# File 'lib/vector.rb', line 32 def to_a [x, y, z, w] end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/vector.rb', line 28 def to_s to_a.compact.join ' ' end |
#zero_w ⇒ Object
20 21 22 |
# File 'lib/vector.rb', line 20 def zero_w self.class.new x, y, z, 0 end |