Class: Geom3d::Vector
- Inherits:
-
Object
- Object
- Geom3d::Vector
- Defined in:
- lib/geom3d/vector.rb
Instance Attribute Summary collapse
-
#dx ⇒ Object
Returns the value of attribute dx.
-
#dy ⇒ Object
Returns the value of attribute dy.
-
#dz ⇒ Object
Returns the value of attribute dz.
Instance Method Summary collapse
- #*(scale) ⇒ Object
- #**(power) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(scale) ⇒ Object
- #==(other) ⇒ Object
- #cross(other) ⇒ Object
- #dot(other) ⇒ Object
- #flatten ⇒ Object
-
#initialize(*args) ⇒ Vector
constructor
A new instance of Vector.
- #length ⇒ Object
- #norm ⇒ Object (also: #unit)
-
#to_ary ⇒ Object
Allows flatten and multiple assignment to work with this class.
- #to_s ⇒ Object
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
#dx ⇒ Object
Returns the value of attribute dx.
5 6 7 |
# File 'lib/geom3d/vector.rb', line 5 def dx @dx end |
#dy ⇒ Object
Returns the value of attribute dy.
5 6 7 |
# File 'lib/geom3d/vector.rb', line 5 def dy @dy end |
#dz ⇒ Object
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 |
#flatten ⇒ Object
72 73 74 |
# File 'lib/geom3d/vector.rb', line 72 def flatten to_ary.flatten end |
#length ⇒ Object
59 60 61 |
# File 'lib/geom3d/vector.rb', line 59 def length Math.sqrt(self.dot(self)) end |
#norm ⇒ Object Also known as: unit
53 54 55 |
# File 'lib/geom3d/vector.rb', line 53 def norm self / self.length end |
#to_ary ⇒ Object
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_s ⇒ Object
63 64 65 |
# File 'lib/geom3d/vector.rb', line 63 def to_s "Vector(%.3f,%.3f,%.3f)" % [@dx, @dy, @dz] end |