Class: Vector

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

Instance Method Summary collapse

Instance Method Details

#cross(v2) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
# File 'lib/lib/helper/lib/vector.rb', line 20

def cross v2
  raise ArgumentError, "Vectors should be 3D" if self.size != 3 or v2.size != 3
  Vector[
    self[1]*v2[2] - self[2]*v2[1],
    self[2]*v2[0] - self[0]*v2[2],
    self[0]*v2[1] - self[1]*v2[0]
  ]
end

#inspect(format = '%.4f') ⇒ Object



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

def inspect format = '%.4f'
  return "Vector[]" if size == 0
  (["\nVector[ "] + ["        "]*(size-1)).zip(to_s.lines).map{|v| v.join}.join() +" ]"
end

#longest_string(format) ⇒ Object



11
12
13
# File 'lib/lib/helper/lib/vector.rb', line 11

def longest_string format
  map{|x| (format % x).length}.max
end

#rotate(direction, origin = , angle) ⇒ Object



17
18
19
# File 'lib/lib/helper/lib/vector.rb', line 17

def rotate direction, origin = Vector[0,0,0], angle
  Vector[*(rotation_matrix(direction, origin, angle) * Vector[*(self),1]).to_a[0,3]]
end

#to_s(format = '%.4f') ⇒ Object



2
3
4
5
6
# File 'lib/lib/helper/lib/vector.rb', line 2

def to_s format = '%.4f'
  return "" if size == 0
  w = longest_string format
  map{|v| "%#{w}s" % (format % v)}.to_a.join("\n")
end

#to_vObject



14
15
16
# File 'lib/lib/helper/lib/vector.rb', line 14

def to_v
  self
end