Class: RQuad::Vector
- Inherits:
-
Object
- Object
- RQuad::Vector
- Defined in:
- lib/rquad/vector.rb
Instance Method Summary collapse
-
#*(scalar) ⇒ Object
Multiply this vector by a ‘scalar`.
-
#+(other) ⇒ Object
Add another Vector ‘other` to this vector.
-
#-(other) ⇒ Object
This vector minus another Vector ‘other`.
-
#/(scalar) ⇒ Object
Divide this vector by a ‘scalar`.
-
#==(other) ⇒ Object
Test if this vector is equal to another Vector ‘other`.
-
#dist_to(other) ⇒ Object
The Euclidean distnce between this and another Vector ‘other`.
-
#initialize(x = nil, y = nil, z = nil) ⇒ Vector
constructor
Initialize a Vector with either another Vector, an Array, or 2-3 numbers.
-
#length ⇒ Object
The length of this vector in 2D or 3D space.
-
#to_s ⇒ Object
Display this vector as a String, either in <x, y> or <x, y, z> notation.
-
#x ⇒ Object
The X component of this vector.
-
#x=(new_x) ⇒ Object
Set the X component of this vector.
-
#y ⇒ Object
The Y component of this vector.
-
#y=(new_y) ⇒ Object
Set the Y component of this vector.
-
#z ⇒ Object
The Z component of this vector.
-
#z=(new_z) ⇒ Object
Set the Z component of this vector.
Constructor Details
#initialize(x = nil, y = nil, z = nil) ⇒ Vector
Initialize a Vector with either another Vector, an Array, or 2-3 numbers.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rquad/vector.rb', line 46 def initialize(x = nil, y = nil, z = nil) if x && x.kind_of?(Vector) @x = x.x @y = x.y @z = x.z elsif x && x.kind_of?(Array) @x = x[0] @y = x[1] @z = x[2] else @x = x.to_f if x @y = y.to_f if y @z = z.to_f if z end end |
Instance Method Details
#*(scalar) ⇒ Object
Multiply this vector by a ‘scalar`.
125 126 127 128 129 130 131 132 133 |
# File 'lib/rquad/vector.rb', line 125 def *(scalar) if z Vector.new(x * scalar, y * scalar, z * scalar) elsif x && y Vector.new(x * scalar, y * scalar) else Vector.new end end |
#+(other) ⇒ Object
Add another Vector ‘other` to this vector.
136 137 138 139 140 141 142 143 144 |
# File 'lib/rquad/vector.rb', line 136 def +(other) if z && other.z Vector.new(x + other.x, y + other.y, z + other.z) elsif x && y && other.x && other.y Vector.new(x + other.x, y + other.y) else Vector.new end end |
#-(other) ⇒ Object
This vector minus another Vector ‘other`.
109 110 111 |
# File 'lib/rquad/vector.rb', line 109 def -(other) self + other * -1 end |
#/(scalar) ⇒ Object
Divide this vector by a ‘scalar`.
114 115 116 117 118 119 120 121 122 |
# File 'lib/rquad/vector.rb', line 114 def /(scalar) if z Vector.new(x / scalar, y / scalar, z / scalar) elsif x && y Vector.new(x / scalar, y / scalar) else Vector.new end end |
#==(other) ⇒ Object
Test if this vector is equal to another Vector ‘other`.
147 148 149 150 151 |
# File 'lib/rquad/vector.rb', line 147 def ==(other) result = (other.x == x && other.y == y && other.z == z) # puts "(#{other.x} == #{x} && #{other.y} == #{y} && #{other.z} == #{z}) = #{result.inspect}" result end |
#dist_to(other) ⇒ Object
The Euclidean distnce between this and another Vector ‘other`.
104 105 106 |
# File 'lib/rquad/vector.rb', line 104 def dist_to(other) (other - self).length end |
#length ⇒ Object
The length of this vector in 2D or 3D space.
93 94 95 96 97 98 99 100 101 |
# File 'lib/rquad/vector.rb', line 93 def length if z Math.sqrt(x * x + y * y + z * z) elsif x && y Math.sqrt(x * x + y * y) else nil end end |
#to_s ⇒ Object
Display this vector as a String, either in <x, y> or <x, y, z> notation.
154 155 156 157 158 159 160 |
# File 'lib/rquad/vector.rb', line 154 def to_s if z "<#{x ? x : 'nil'}, #{y ? y : 'nil'}, #{z}>" else "<#{x ? x : 'nil'}, #{y ? y : 'nil'}>" end end |
#x ⇒ Object
The X component of this vector.
63 64 65 |
# File 'lib/rquad/vector.rb', line 63 def x @x end |
#x=(new_x) ⇒ Object
Set the X component of this vector.
78 79 80 |
# File 'lib/rquad/vector.rb', line 78 def x=(new_x) @x = new_x.to_f if new_x end |
#y ⇒ Object
The Y component of this vector.
68 69 70 |
# File 'lib/rquad/vector.rb', line 68 def y @y end |
#y=(new_y) ⇒ Object
Set the Y component of this vector.
83 84 85 |
# File 'lib/rquad/vector.rb', line 83 def y=(new_y) @y = new_y.to_f if new_y end |
#z ⇒ Object
The Z component of this vector.
73 74 75 |
# File 'lib/rquad/vector.rb', line 73 def z @z end |
#z=(new_z) ⇒ Object
Set the Z component of this vector.
88 89 90 |
# File 'lib/rquad/vector.rb', line 88 def z=(new_z) @z = new_z.to_f if new_z end |