Class: Geom::Number3D

Inherits:
Object
  • Object
show all
Defined in:
lib/geom/number.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, z = 0) ⇒ Number3D

Returns a new instance of Number3D.



7
8
9
# File 'lib/geom/number.rb', line 7

def initialize(x=0,y=0,z=0)
  @x, @y, @z = x, y, z
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/geom/number.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/geom/number.rb', line 5

def y
  @y
end

#zObject

Returns the value of attribute z.



5
6
7
# File 'lib/geom/number.rb', line 5

def z
  @z
end

Class Method Details

.cross(v, w, target = nil) ⇒ Object



19
20
21
22
23
# File 'lib/geom/number.rb', line 19

def self.cross(v,w,target=nil)
  target ||= Number3D.new
  target.reset((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x))
  target
end

.dot(v, w) ⇒ Object



25
26
27
# File 'lib/geom/number.rb', line 25

def self.dot(v,w)
  v.x * w.x + v.y * w.y + w.z * v.z
end

.from_str(str) ⇒ Object



29
30
31
32
# File 'lib/geom/number.rb', line 29

def self.from_str(str)
  x,y,z = str.split(' ') if str
  str.nil? ? Number3D.new : Number3D.new(x.to_f,y.to_f,z.to_f)
end

.sub(v, w) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/geom/number.rb', line 11

def self.sub(v,w)
  Number3D.new(
    v.x - w.x,
    v.y - w.y,
    v.z - w.z
  )
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
86
87
# File 'lib/geom/number.rb', line 83

def == (other)
  @x == other.x &&
  @y == other.y &&
  @z == other.z
end

#distance(other) ⇒ Object



68
69
70
71
72
73
# File 'lib/geom/number.rb', line 68

def distance(other)
  Math.sqrt(
    distance_z(other) ** 2 +
    Math.sqrt(distance_x(other)**2 + distance_y(other)**2) ** 2
  )
end

#distance_x(other) ⇒ Object



56
57
58
# File 'lib/geom/number.rb', line 56

def distance_x(other)
  (@x - other.x).abs
end

#distance_y(other) ⇒ Object



60
61
62
# File 'lib/geom/number.rb', line 60

def distance_y(other)
  (@y - other.y).abs
end

#distance_z(other) ⇒ Object



64
65
66
# File 'lib/geom/number.rb', line 64

def distance_z(other)
  (@z - other.z).abs
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/geom/number.rb', line 79

def eql?(other)
  self == other
end

#hashObject



75
76
77
# File 'lib/geom/number.rb', line 75

def hash
  "#{@x}#{@y}#{@z}".hash
end

#minus_eq(v) ⇒ Object



50
51
52
53
54
# File 'lib/geom/number.rb', line 50

def minus_eq(v)
  @x -= v.x
  @y -= v.y
  @z -= v.z
end

#normalizeObject



40
41
42
43
44
45
46
47
48
# File 'lib/geom/number.rb', line 40

def normalize
  mod = Math.sqrt( @x**2 + @y**2 + @z**2 )
  if mod != 0 && mod != 1
    mod = 1 / mod # mults are cheaper then divs
    @x *= mod
    @y *= mod
    @z *= mod
  end
end

#reset(nx = nil, ny = nil, nz = nil) ⇒ Object



34
35
36
37
38
# File 'lib/geom/number.rb', line 34

def reset(nx=nil,ny=nil,nz=nil)
  @x = nx if nx
  @y = ny if ny
  @z = nz if nz
end

#to_floatsObject



93
94
95
# File 'lib/geom/number.rb', line 93

def to_floats
  [@x,@y,@z].join ' '
end

#to_sObject



89
90
91
# File 'lib/geom/number.rb', line 89

def to_s
  "#<Geom::Number3D:#{@x},#{@y},#{@z}>"
end