class Geom3d::Vector

Attributes

dx[RW]
dy[RW]
dz[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/geom3d/vector.rb, line 7
def initialize *args
  @dx, @dy, @dz = args.flatten
end

Public Instance Methods

*(scale) click to toggle source
# File lib/geom3d/vector.rb, line 19
def * scale
  Vector.new(@dx * scale, @dy * scale, @dz * scale)
end
**(power) click to toggle source
# File lib/geom3d/vector.rb, line 23
def ** power
  Vector.new(@dx ** power, @dy ** power, @dz ** power)
end
+(other) click to toggle source
# File lib/geom3d/vector.rb, line 15
def + other
  Vector.new(@dx + other.dx, @dy + other.dy, @dz + other.dz)
end
+@() click to toggle source
# File lib/geom3d/vector.rb, line 31
def +@
  self
end
-(other) click to toggle source
# File lib/geom3d/vector.rb, line 11
def - other
  Vector.new(@dx - other.dx, @dy - other.dy, @dz - other.dz)
end
-@() click to toggle source
# File lib/geom3d/vector.rb, line 35
def -@
  Vector.new(-@dx, -@dy, -@dz)
end
/(scale) click to toggle source
# File lib/geom3d/vector.rb, line 27
def / scale
  self * (1.0/scale)
end
==(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/geom3d/vector.rb, line 43
def dot other
  @dx * other.dx + @dy * other.dy + @dz * other.dz
end
flatten() click to toggle source
# File lib/geom3d/vector.rb, line 72
def flatten
  to_ary.flatten
end
length() click to toggle source
# File lib/geom3d/vector.rb, line 59
def length
  Math.sqrt(self.dot(self))
end
norm() click to toggle source
# File lib/geom3d/vector.rb, line 53
def norm
  self / self.length
end
Also aliased as: unit
to_ary() click to toggle source

Allows flatten and multiple assignment to work with this class

# File lib/geom3d/vector.rb, line 68
def to_ary
  [@dx, @dy, @dz]
end
to_s() click to toggle source
# File lib/geom3d/vector.rb, line 63
def to_s
  "Vector(%.3f,%.3f,%.3f)" % [@dx, @dy, @dz]
end
unit()
Alias for: norm