Module: Yeah::Vec2

Defined in:
lib/yeah/vec2.rb

Overview

Vec2 provides methods for manipulating 2-dimensional vectors.

A vector is represented by a sequence of Numeric component values.

Class Method Summary collapse

Class Method Details

.add(ax, ay, bx, by) ⇒ (Numeric, Numeric)


47
48
49
# File 'lib/yeah/vec2.rb', line 47

def add(ax, ay, bx, by)
  [ax + bx, ay + by]
end

.direction(x, y) ⇒ Numeric


29
30
31
# File 'lib/yeah/vec2.rb', line 29

def direction(x, y)
  Math.atan2(y, x)
end

.direction_to(ax, ay, bx, by) ⇒ Numeric


100
101
102
# File 'lib/yeah/vec2.rb', line 100

def direction_to(ax, ay, bx, by)
  Math.atan2(by - ay, bx - by)
end

.distance_to(ax, ay, bx, by) ⇒ Numeric


91
92
93
# File 'lib/yeah/vec2.rb', line 91

def distance_to(ax, ay, bx, by)
  Math.sqrt((ax - bx) ** 2 + (ay - by) ** 2)
end

.divide(ax, ay, b) ⇒ (Numeric, Numeric)


73
74
75
# File 'lib/yeah/vec2.rb', line 73

def divide(ax, ay, b)
  [ax / b, ay / b]
end

.dot_product(ax, ay, bx, by) ⇒ (Numeric, Numeric)


82
83
84
# File 'lib/yeah/vec2.rb', line 82

def dot_product(ax, ay, bx, by)
  ax * bx + ay * by
end

.equal?(ax, ay, bx, by) ⇒ true, false


13
14
15
# File 'lib/yeah/vec2.rb', line 13

def equal?(ax, ay, bx, by)
  ax == bx && ay == by
end

.magnitude(x, y) ⇒ Numeric Also known as: length


20
21
22
# File 'lib/yeah/vec2.rb', line 20

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

.multiply(ax, ay, b) ⇒ (Numeric, Numeric)


65
66
67
# File 'lib/yeah/vec2.rb', line 65

def multiply(ax, ay, b)
  [ax * b, ay * b]
end

.normalize(x, y) ⇒ (Numeric, Numeric)


37
38
39
40
# File 'lib/yeah/vec2.rb', line 37

def normalize(x, y)
  mag = magnitude(x, y)
  [x / mag, y / mag]
end

.subtract(ax, ay, bx, by) ⇒ (Numeric, Numeric)


56
57
58
# File 'lib/yeah/vec2.rb', line 56

def subtract(ax, ay, bx, by)
  [ax - bx, ay - by]
end