Class: Lotu::Vector2d
- Inherits:
-
Object
- Object
- Lotu::Vector2d
- Defined in:
- lib/lotu/helpers/vector2d.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(n) ⇒ Object
- #+(v) ⇒ Object
- #-(v) ⇒ Object
- #/(n) ⇒ Object
- #angle ⇒ Object
- #angle_to(v) ⇒ Object
- #clear_cache ⇒ Object
- #clockwise?(vector) ⇒ Boolean
- #counter_clockwise?(vector) ⇒ Boolean
- #dot(v) ⇒ Object
- #facing_to?(vector) ⇒ Boolean
-
#initialize(x = 0, y = 0) ⇒ Vector2d
constructor
A new instance of Vector2d.
- #length ⇒ Object
- #length_sq ⇒ Object
- #normalize ⇒ Object
- #normalize! ⇒ Object
- #perp ⇒ Object
- #sign_to(vector) ⇒ Object
- #to_s ⇒ Object
- #truncate!(max_l) ⇒ Object
- #zero! ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(x = 0, y = 0) ⇒ Vector2d
Returns a new instance of Vector2d.
10 11 12 13 14 |
# File 'lib/lotu/helpers/vector2d.rb', line 10 def initialize(x=0, y=0) clear_cache @x = Float(x) @y = Float(y) end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/lotu/helpers/vector2d.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/lotu/helpers/vector2d.rb', line 4 def y @y end |
Class Method Details
.up ⇒ Object
6 7 8 |
# File 'lib/lotu/helpers/vector2d.rb', line 6 def self.up @up ||= new(0, -1) end |
Instance Method Details
#*(n) ⇒ Object
84 85 86 |
# File 'lib/lotu/helpers/vector2d.rb', line 84 def *(n) Vector2d.new(@x*n, @y*n) end |
#+(v) ⇒ Object
72 73 74 |
# File 'lib/lotu/helpers/vector2d.rb', line 72 def +(v) Vector2d.new(@x+v.x, @y+v.y) end |
#-(v) ⇒ Object
76 77 78 |
# File 'lib/lotu/helpers/vector2d.rb', line 76 def -(v) Vector2d.new(@x-v.x, @y-v.y) end |
#/(n) ⇒ Object
80 81 82 |
# File 'lib/lotu/helpers/vector2d.rb', line 80 def /(n) Vector2d.new(@x/n, @y/n) end |
#angle ⇒ Object
104 105 106 |
# File 'lib/lotu/helpers/vector2d.rb', line 104 def angle Gosu.angle(0, 0, @x, @y) end |
#angle_to(v) ⇒ Object
108 109 110 |
# File 'lib/lotu/helpers/vector2d.rb', line 108 def angle_to(v) Gosu.angle_diff(angle, v.angle) end |
#clear_cache ⇒ Object
16 17 18 19 20 |
# File 'lib/lotu/helpers/vector2d.rb', line 16 def clear_cache @length = nil @length_sq = nil @normalized = false end |
#clockwise?(vector) ⇒ Boolean
120 121 122 |
# File 'lib/lotu/helpers/vector2d.rb', line 120 def clockwise?(vector) sign_to(vector) == 1 end |
#counter_clockwise?(vector) ⇒ Boolean
124 125 126 |
# File 'lib/lotu/helpers/vector2d.rb', line 124 def counter_clockwise?(vector) !clockwise? end |
#dot(v) ⇒ Object
96 97 98 |
# File 'lib/lotu/helpers/vector2d.rb', line 96 def dot(v) @x*v.x + @y*v.y end |
#facing_to?(vector) ⇒ Boolean
128 129 130 |
# File 'lib/lotu/helpers/vector2d.rb', line 128 def facing_to?(vector) dot(vector) > 0 end |
#length ⇒ Object
31 32 33 |
# File 'lib/lotu/helpers/vector2d.rb', line 31 def length @length ||= Math.sqrt(length_sq) end |
#length_sq ⇒ Object
35 36 37 |
# File 'lib/lotu/helpers/vector2d.rb', line 35 def length_sq @length_sq ||= @x*@x + @y*@y end |
#normalize ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/lotu/helpers/vector2d.rb', line 49 def normalize if zero? Vector2d.new(0,0) else Vector2d.new(@x/length, @y/length) end end |
#normalize! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lotu/helpers/vector2d.rb', line 57 def normalize! if zero? @length = 0 @length_sq = 0 @normalized = true end return self if @normalized @x /= length @y /= length clear_cache @normalized = true self end |
#perp ⇒ Object
100 101 102 |
# File 'lib/lotu/helpers/vector2d.rb', line 100 def perp Vector2d.new(@y, -@x) end |
#sign_to(vector) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/lotu/helpers/vector2d.rb', line 112 def sign_to(vector) if @y * vector.x > @x * vector.y return -1 else return 1 end end |
#to_s ⇒ Object
132 133 134 135 136 137 |
# File 'lib/lotu/helpers/vector2d.rb', line 132 def to_s # TODO tratar de reducir la cantidad de vectores creados, al # menos cuando no se está moviendo #format('%d %.2f, %.2f', object_id, @x, @y) format('%.2f, %.2f', @x, @y) end |
#truncate!(max_l) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/lotu/helpers/vector2d.rb', line 88 def truncate!(max_l) return self if length < max_l normalize! self.x *= max_l self.y *= max_l self end |
#zero! ⇒ Object
22 23 24 25 |
# File 'lib/lotu/helpers/vector2d.rb', line 22 def zero! self.x = 0 self.y = 0 end |
#zero? ⇒ Boolean
27 28 29 |
# File 'lib/lotu/helpers/vector2d.rb', line 27 def zero? @x == 0 && @y == 0 end |