Class: IRB::Vec
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Instance Method Summary collapse
- #cross(other) ⇒ Object
- #dot(other) ⇒ Object
-
#initialize(x, y, z) ⇒ Vec
constructor
A new instance of Vec.
- #normalize ⇒ Object
- #sub(other) ⇒ Object
Constructor Details
permalink #initialize(x, y, z) ⇒ Vec
Returns a new instance of Vec.
6 7 8 |
# File 'lib/irb/easter-egg.rb', line 6 def initialize(x, y, z) @x, @y, @z = x, y, z end |
Instance Attribute Details
permalink #x ⇒ Object (readonly)
Returns the value of attribute x.
10 11 12 |
# File 'lib/irb/easter-egg.rb', line 10 def x @x end |
Instance Method Details
permalink #cross(other) ⇒ Object
[View source]
20 21 22 23 |
# File 'lib/irb/easter-egg.rb', line 20 def cross(other) ox, oy, oz = other.x, other.y, other.z Vec.new(@y*oz-@z*oy, @z*ox-@x*oz, @x*oy-@y*ox) end |
permalink #dot(other) ⇒ Object
[View source]
16 17 18 |
# File 'lib/irb/easter-egg.rb', line 16 def dot(other) @x*other.x + @y*other.y + @z*other.z end |
permalink #normalize ⇒ Object
[View source]
25 26 27 28 |
# File 'lib/irb/easter-egg.rb', line 25 def normalize r = Math.sqrt(self.dot(self)) Vec.new(@x / r, @y / r, @z / r) end |
permalink #sub(other) ⇒ Object
[View source]
12 13 14 |
# File 'lib/irb/easter-egg.rb', line 12 def sub(other) Vec.new(@x - other.x, @y - other.y, @z - other.z) end |