Class: Orbit::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/orbit/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, y = nil, z = nil, w = nil) ⇒ Vector

Returns a new instance of Vector.



9
10
11
12
13
14
# File 'lib/orbit/vector.rb', line 9

def initialize( x = nil, y = nil, z = nil, w = nil )
  @m_x = x
  @m_y = y
  @m_z = z
  @m_w = w
end

Instance Attribute Details

#m_wObject

Returns the value of attribute m_w.



7
8
9
# File 'lib/orbit/vector.rb', line 7

def m_w
  @m_w
end

#m_xObject

Returns the value of attribute m_x.



4
5
6
# File 'lib/orbit/vector.rb', line 4

def m_x
  @m_x
end

#m_yObject

Returns the value of attribute m_y.



5
6
7
# File 'lib/orbit/vector.rb', line 5

def m_y
  @m_y
end

#m_zObject

Returns the value of attribute m_z.



6
7
8
# File 'lib/orbit/vector.rb', line 6

def m_z
  @m_z
end

Instance Method Details

#angle(vec) ⇒ Object

##################################/ Calculate the angle between this vector and another



38
39
40
# File 'lib/orbit/vector.rb', line 38

def angle(vec)
  return Math.acos(dot(vec) / (magnitude() * vec.magnitude()))
end

#dot(vec) ⇒ Object

##################################/ Return the dot product



50
51
52
# File 'lib/orbit/vector.rb', line 50

def dot(vec)
  return (@m_x * vec.m_x) + (@m_y * vec.m_y) + (@m_z * vec.m_z)
end

#magnitudeObject

##################################/



43
44
45
# File 'lib/orbit/vector.rb', line 43

def magnitude
  return Math.sqrt((@m_x * @m_x) + (@m_y * @m_y) + (@m_z * @m_z))
end

#mul(factor) ⇒ Object

##################################/ Multiply each component in the vector by ‘factor’.



18
19
20
21
22
23
24
25
# File 'lib/orbit/vector.rb', line 18

def mul(factor)
 #puts "m_x: #{@m_x}, factor: #{factor}"

  @m_x = @m_x * factor
  @m_y *= factor
  @m_z *= factor
  @m_w *= (factor).abs if @m_w
end

#sub(vec) ⇒ Object

##################################/ Subtract a vector from this one.



29
30
31
32
33
34
# File 'lib/orbit/vector.rb', line 29

def sub(vec)
  @m_x -= vec.m_x
  @m_y -= vec.m_y
  @m_z -= vec.m_z
  @m_w -= vec.m_w
end