Class: Lotu::Vector2d

Inherits:
Object
  • Object
show all
Defined in:
lib/lotu/helpers/vector2d.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/lotu/helpers/vector2d.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/lotu/helpers/vector2d.rb', line 4

def y
  @y
end

Class Method Details

.upObject



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

#angleObject



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_cacheObject



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

Returns:

  • (Boolean)


120
121
122
# File 'lib/lotu/helpers/vector2d.rb', line 120

def clockwise?(vector)
  sign_to(vector) == 1
end

#counter_clockwise?(vector) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


128
129
130
# File 'lib/lotu/helpers/vector2d.rb', line 128

def facing_to?(vector)
  dot(vector) > 0
end

#lengthObject



31
32
33
# File 'lib/lotu/helpers/vector2d.rb', line 31

def length
  @length ||= Math.sqrt(length_sq)
end

#length_sqObject



35
36
37
# File 'lib/lotu/helpers/vector2d.rb', line 35

def length_sq
  @length_sq ||= @x*@x + @y*@y
end

#normalizeObject



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

#perpObject



100
101
102
# File 'lib/lotu/helpers/vector2d.rb', line 100

def perp
  Vector2d.new(@y, -@x)
end

#rotate(radians) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/lotu/helpers/vector2d.rb', line 132

def rotate radians
  new_x = @x * Math.cos(radians) - @y * Math.sin(radians)
  new_y = @y * Math.cos(radians) + @x * Math.sin(radians)
  #self.x = new_x
  #self.y = new_y
  #self
  Vector2d.new( new_x, new_y )
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_sObject



141
142
143
144
145
146
# File 'lib/lotu/helpers/vector2d.rb', line 141

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

Returns:

  • (Boolean)


27
28
29
# File 'lib/lotu/helpers/vector2d.rb', line 27

def zero?
  @x == 0 && @y == 0
end