Class: Uzi::Vector2
- Inherits:
-
Object
- Object
- Uzi::Vector2
- Defined in:
- lib/uzi-vector2.rb,
lib/uzi-vector2/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(v2) ⇒ Object
- #-(v2) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #==(v2) ⇒ Object
- #dot(v2) ⇒ Object
-
#initialize(x = 0.0, y = 0.0) ⇒ Vector2
constructor
A new instance of Vector2.
- #length ⇒ Object
- #normalize ⇒ Object
- #normalize! ⇒ Object
- #perp ⇒ Object
- #rotate(theta) ⇒ Object
- #rotate!(theta) ⇒ Object
Constructor Details
#initialize(x = 0.0, y = 0.0) ⇒ Vector2
Returns a new instance of Vector2.
5 6 7 |
# File 'lib/uzi-vector2.rb', line 5 def initialize(x=0.0, y=0.0) @x, @y = x.to_f, y.to_f end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
3 4 5 |
# File 'lib/uzi-vector2.rb', line 3 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
3 4 5 |
# File 'lib/uzi-vector2.rb', line 3 def y @y end |
Instance Method Details
#*(scalar) ⇒ Object
19 20 21 |
# File 'lib/uzi-vector2.rb', line 19 def *(scalar) Vector2.new( @x * scalar, @y * scalar ) end |
#+(v2) ⇒ Object
15 16 17 |
# File 'lib/uzi-vector2.rb', line 15 def +(v2) Vector2.new( @x + v2.x, @y + v2.y ) end |
#-(v2) ⇒ Object
11 12 13 |
# File 'lib/uzi-vector2.rb', line 11 def -(v2) Vector2.new( @x - v2.x, @y - v2.y ) end |
#/(scalar) ⇒ Object
23 24 25 |
# File 'lib/uzi-vector2.rb', line 23 def /(scalar) Vector2.new( @x / scalar, @y / scalar ) end |
#==(v2) ⇒ Object
31 32 33 |
# File 'lib/uzi-vector2.rb', line 31 def ==(v2) @x == v2.x && @y == v2.y end |
#dot(v2) ⇒ Object
41 42 43 |
# File 'lib/uzi-vector2.rb', line 41 def dot(v2) @x * v2.x + @y * v2.y end |
#length ⇒ Object
37 38 39 |
# File 'lib/uzi-vector2.rb', line 37 def length Math.sqrt( @x**2 + @y**2 ) end |
#normalize ⇒ Object
45 46 47 48 49 |
# File 'lib/uzi-vector2.rb', line 45 def normalize len = length Vector2.new( @x / len, @y / len ) end |
#normalize! ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/uzi-vector2.rb', line 51 def normalize! len = length @x /= len @y /= len return len end |
#rotate(theta) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/uzi-vector2.rb', line 64 def rotate(theta) theta *= Math::PI / 180.0 tx = (@x * Math.cos(theta)) - (@y * Math.sin(theta)) ty = (@x * Math.sin(theta)) + (@y * Math.cos(theta)) Vector2.new(tx, ty) end |
#rotate!(theta) ⇒ Object
73 74 75 76 |
# File 'lib/uzi-vector2.rb', line 73 def rotate!(theta) v2 = self.rotate(theta) @x, @y = v2.x, v2.y end |