Class: Pongo::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/pongo/util/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px = 0.0, py = 0.0) ⇒ Vector

Returns a new instance of Vector.



5
6
7
# File 'lib/pongo/util/vector.rb', line 5

def initialize(px=0.0, py=0.0)
  set_to(px, py)
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/pongo/util/vector.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/pongo/util/vector.rb', line 3

def y
  @y
end

Instance Method Details

#*(s_or_v) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/pongo/util/vector.rb', line 69

def *(s_or_v)
  if s_or_v.is_a? Vector
    times(s_or_v)
  else
    mult(s_or_v)
  end
end

#+(v) ⇒ Object Also known as: plus



30
31
32
# File 'lib/pongo/util/vector.rb', line 30

def +(v)
  Vector.new(@x + v.x, @y + v.y)
end

#-(v) ⇒ Object Also known as: minus



42
43
44
# File 'lib/pongo/util/vector.rb', line 42

def -(v)
  Vector.new(@x - v.x, @y - v.y)
end

#/(s) ⇒ Object Also known as: div



77
78
79
80
# File 'lib/pongo/util/vector.rb', line 77

def /(s)
  s = 0.0001 if s == 0
  Vector.new(@x / s, @y / s)
end

#copy(v) ⇒ Object



14
15
16
# File 'lib/pongo/util/vector.rb', line 14

def copy(v)
  set_to(v.x, v.y)
end

#cross(v) ⇒ Object



26
27
28
# File 'lib/pongo/util/vector.rb', line 26

def cross(v)
  @x * v.y - @y * v.x
end

#distance(v) ⇒ Object



95
96
97
# File 'lib/pongo/util/vector.rb', line 95

def distance(v)
  minus(v).magnitude
end

#div!(s) ⇒ Object Also known as: div_equals



83
84
85
86
87
88
# File 'lib/pongo/util/vector.rb', line 83

def div!(s)
  s = 0.0001 if s == 0
  @x /= s
  @y /= s
  self
end

#dot(v) ⇒ Object



22
23
24
# File 'lib/pongo/util/vector.rb', line 22

def dot(v)
  @x * v.x + @y * v.y
end

#dupObject



18
19
20
# File 'lib/pongo/util/vector.rb', line 18

def dup
  Vector.new(@x, @y)
end

#magnitudeObject



91
92
93
# File 'lib/pongo/util/vector.rb', line 91

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

#minus!(v) ⇒ Object Also known as: minus_equals



47
48
49
50
51
# File 'lib/pongo/util/vector.rb', line 47

def minus!(v)
  @x -= v.x
  @y -= v.y
  self
end

#mult(s) ⇒ Object



54
55
56
# File 'lib/pongo/util/vector.rb', line 54

def mult(s)
  Vector.new(@x * s, @y * s)
end

#mult!(s) ⇒ Object Also known as: mult_equals



58
59
60
61
62
# File 'lib/pongo/util/vector.rb', line 58

def mult!(s)
  @x *= s
  @y *= s
  self
end

#normalizeObject



99
100
101
102
103
104
105
# File 'lib/pongo/util/vector.rb', line 99

def normalize
  if @x == 0 and @y == 0
    mult(10000.0)
  else
    mult(1.0 / magnitude)
  end
end

#normalize!Object



107
108
109
110
111
112
# File 'lib/pongo/util/vector.rb', line 107

def normalize!
  unless @x == 0 and @y == 0
    mult!(1.0 / magnitude)
  end
  self
end

#plus!(v) ⇒ Object Also known as: plus_equals



35
36
37
38
39
# File 'lib/pongo/util/vector.rb', line 35

def plus!(v)
  @x += v.x
  @y += v.y
  self
end

#rotate(rad) ⇒ Object



118
119
120
121
122
123
# File 'lib/pongo/util/vector.rb', line 118

def rotate(rad)
  Vector.new(
    @x * Math.cos(rad) - @y * Math.sin(rad),
    @x * Math.sin(rad) + @y * Math.cos(rad)
  )
end

#rotate!(rad) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/pongo/util/vector.rb', line 125

def rotate!(rad)
  @x, @y = [
    @x * Math.cos(rad) - @y * Math.sin(rad),
    @x * Math.sin(rad) + @y * Math.cos(rad)
  ]
  self
end

#set_to(px, py = nil) ⇒ Object



9
10
11
12
# File 'lib/pongo/util/vector.rb', line 9

def set_to(px, py=nil)
  @x, @y = py ? [px.to_f, py.to_f] : [px.x, px.y]
  self
end

#times(v) ⇒ Object



65
66
67
# File 'lib/pongo/util/vector.rb', line 65

def times(v)
  Vector.new(x * v.x, y * v.y)
end

#to_sObject



114
115
116
# File 'lib/pongo/util/vector.rb', line 114

def to_s
  "#{@x} : #{@y}"
end