-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.rb
79 lines (70 loc) · 2.04 KB
/
team.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Team < ActiveRecord::Base
has_many :clues
belongs_to :play
def clue_points
points = []
clues.each do |clue|
points << [clue.lat, clue.long, clue.photo.url(:medium)]
end
points
end
def clue_total
clues ? clues.size : nil
end
# astonishingly, the following translation from java appears to work. no points for rubyishness.
def perimeter
hull = []
possible_points = clues.find(:all, :conditions => {:on_path => true})
return [] unless possible_points.size >= 3
angleToLastPoint = 0
reference_point = last_point_on_hull = lowest_point_on_hull
begin
hull << last_point_on_hull
lastAngle = Float::MAX
lastPoint = nil
possible_points.each do |clue|
next if ((clue.x == last_point_on_hull.x) && (clue.y == last_point_on_hull.y)) # should only compare x and y values
angle = Math::atan2(-clue.y - -last_point_on_hull.y, clue.x - last_point_on_hull.x)
#########
if ((angle < 0.0) || (angle < angleToLastPoint))
angle += 2.0 * Math::PI
end
if (angle < lastAngle)
lastPoint = clue
lastAngle = angle
end
end
last_point_on_hull = lastPoint
angleToLastPoint = lastAngle
end while (last_point_on_hull != reference_point)
return hull
end
def perimeter_as_arrays
arr = []
perimeter.each do |p|
arr << [p.y, p.x]
end
return arr
end
def area
total = 0
ppoints = perimeter
return 0 unless (ppoints.size >= 3)
pprev = ppoints.last
ppoints.each do |p|
total += (pprev.x * -p.y) - (p.x * -pprev.y)
pprev = p
end
return (total * 1000000).to_i
end
# Determine the lowest point on the hull.
def lowest_point_on_hull
lastPointOnHull = Clue.new(:long => 0, :lat => -Float::MAX )
clues.find(:all, :conditions => {:on_path => true}).each do |clue|
if (clue.on_path && (clue.y > lastPointOnHull.y))
lastPointOnHull = clue
end
end
return lastPointOnHull
end
end