Class: Uzi::Vector2

Inherits:
Object
  • Object
show all
Defined in:
lib/uzi-vector2.rb,
lib/uzi-vector2/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/uzi-vector2.rb', line 3

def x
  @x
end

#yObject

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

#-@Object



27
28
29
# File 'lib/uzi-vector2.rb', line 27

def -@
  Vector2.new( -@x, -@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

#lengthObject



37
38
39
# File 'lib/uzi-vector2.rb', line 37

def length
  Math.sqrt( @x**2 + @y**2 )
end

#normalizeObject



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

#perpObject



60
61
62
# File 'lib/uzi-vector2.rb', line 60

def perp
  Vector2.new( @y, -@x )
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