Class: Geom3d::Vector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Vector

Returns a new instance of Vector.



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

def initialize *args
  @dx, @dy, @dz = args.flatten
end

Instance Attribute Details

#dxObject

Returns the value of attribute dx.



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

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



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

def dy
  @dy
end

#dzObject

Returns the value of attribute dz.



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

def dz
  @dz
end

Instance Method Details

#*(scale) ⇒ Object



19
20
21
# File 'lib/geom3d/vector.rb', line 19

def * scale
  Vector.new(@dx * scale, @dy * scale, @dz * scale)
end

#**(power) ⇒ Object



23
24
25
# File 'lib/geom3d/vector.rb', line 23

def ** power
  Vector.new(@dx ** power, @dy ** power, @dz ** power)
end

#+(other) ⇒ Object



15
16
17
# File 'lib/geom3d/vector.rb', line 15

def + other
  Vector.new(@dx + other.dx, @dy + other.dy, @dz + other.dz)
end

#+@Object



31
32
33
# File 'lib/geom3d/vector.rb', line 31

def +@
  self
end

#-(other) ⇒ Object



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

def - other
  Vector.new(@dx - other.dx, @dy - other.dy, @dz - other.dz)
end

#-@Object



35
36
37
# File 'lib/geom3d/vector.rb', line 35

def -@
  Vector.new(-@dx, -@dy, -@dz)
end

#/(scale) ⇒ Object



27
28
29
# File 'lib/geom3d/vector.rb', line 27

def / scale
  self * (1.0/scale)
end

#==(other) ⇒ Object



39
40
41
# File 'lib/geom3d/vector.rb', line 39

def == other
  (@dx - other.dx).abs < EPS && (@dy - other.dy).abs < EPS && (@dz -  other.dz).abs < EPS
end

#cross(other) ⇒ Object



47
48
49
50
51
# File 'lib/geom3d/vector.rb', line 47

def cross other
  Vector.new(@dy * other.dz - @dz * other.dy,
             @dz * other.dx - @dx * other.dz,
             @dx * other.dy - @dy * other.dx)
end

#dot(other) ⇒ Object



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

def dot other
  @dx * other.dx + @dy * other.dy + @dz * other.dz
end

#flattenObject



72
73
74
# File 'lib/geom3d/vector.rb', line 72

def flatten
  to_ary.flatten
end

#lengthObject



59
60
61
# File 'lib/geom3d/vector.rb', line 59

def length
  Math.sqrt(self.dot(self))
end

#normObject Also known as: unit



53
54
55
# File 'lib/geom3d/vector.rb', line 53

def norm
  self / self.length
end

#to_aryObject

Allows flatten and multiple assignment to work with this class



68
69
70
# File 'lib/geom3d/vector.rb', line 68

def to_ary
  [@dx, @dy, @dz]
end

#to_sObject



63
64
65
# File 'lib/geom3d/vector.rb', line 63

def to_s
  "Vector(%.3f,%.3f,%.3f)" % [@dx, @dy, @dz]
end