Module: Rubygoal::Util

Defined in:
lib/rubygoal/util.rb

Class Method Summary collapse

Class Method Details

.angle(x1, y1, x2, y2) ⇒ Object



16
17
18
# File 'lib/rubygoal/util.rb', line 16

def angle(x1, y1, x2, y2)
  Math.atan2(y2 - y1, x2 - x1) / Math::PI * 180.0
end

.distance(x1, y1, x2, y2) ⇒ Object



12
13
14
# File 'lib/rubygoal/util.rb', line 12

def distance(x1, y1, x2, y2)
  Math.hypot(x2 - x1, y2 - y1)
end

.offset_x(angle, distance) ⇒ Object



4
5
6
# File 'lib/rubygoal/util.rb', line 4

def offset_x(angle, distance)
  distance * Math.cos(angle * Math::PI / 180.0)
end

.offset_y(angle, distance) ⇒ Object



8
9
10
# File 'lib/rubygoal/util.rb', line 8

def offset_y(angle, distance)
  distance * Math.sin(angle * Math::PI / 180.0)
end

.positive_angle(x1, y1, x2, y2) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rubygoal/util.rb', line 20

def positive_angle(x1, y1, x2, y2)
  angle = self.angle(x1, y1, x2, y2)
  if angle < 0
    360 + angle
  else
    angle
  end
end

.y_intercept_with_line(x, pos1, pos2) ⇒ Object



29
30
31
32
33
# File 'lib/rubygoal/util.rb', line 29

def y_intercept_with_line(x, pos1, pos2)
  slope = (pos2.y - pos1.y) / (pos2.x - pos1.x)

  Position.new(x, slope * (x - pos1.x) + pos1.y)
end