Class: Geom::Number3D
- Inherits:
-
Object
- Object
- Geom::Number3D
- Defined in:
- lib/geom/number.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #distance(other) ⇒ Object
- #distance_x(other) ⇒ Object
- #distance_y(other) ⇒ Object
- #distance_z(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(x = 0, y = 0, z = 0) ⇒ Number3D
constructor
A new instance of Number3D.
- #minus_eq(v) ⇒ Object
- #normalize ⇒ Object
- #reset(nx = nil, ny = nil, nz = nil) ⇒ Object
- #to_floats ⇒ Object
- #to_s ⇒ Object
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
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/geom/number.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/geom/number.rb', line 5 def y @y end |
#z ⇒ Object
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 |
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
79 80 81 |
# File 'lib/geom/number.rb', line 79 def eql?(other) self == other end |
#hash ⇒ Object
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 |
#normalize ⇒ Object
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_floats ⇒ Object
93 94 95 |
# File 'lib/geom/number.rb', line 93 def to_floats [@x,@y,@z].join ' ' end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/geom/number.rb', line 89 def to_s "#<Geom::Number3D:#{@x},#{@y},#{@z}>" end |