/
app2.rb
196 lines (159 loc) · 4.3 KB
/
app2.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require_relative './config/environment'
def setup
#drawing setup
#make sure to recalculate based on new dataset!!!
size 1018, 800 #caculated from ratio of max/min values of gps coord
smooth #anti-aliasing
#set font
text_font create_font("Helvetica Neue", 16)
#variable setup
$time = 0.0
#radius setup
$radius = 7.0
#animation delay
$delay = 3.0
#timeline animation
$timeline_loop = true
#initialize timeline
@timer = Timer.new
#setup a hash trains with minutes they start operating
@schedule = {}
1440.times do |i|
array = Route.includes(:stops).where(:starting => i)
@schedule[i] = array if !array.empty?
end
#setup active cars
$active_cars = []
end
def draw
#refresh background
background 17
#draw title
fill 255
text("NYC ON RAILS", 70, 100)
#draw sub-title
fill 120
text("03.13.2014", 70, 115)
#update functions for classes
if $timeline_loop == true
@timer.update
#initialize on demand
init_vehicles
#update active cars
$active_cars.each {|car| car.update}
end
end
#method for initializing vehicles from schedule
def init_vehicles
id_array = @schedule[$time.to_i]
if id_array != nil
id_array.each do |car|
Vehicle.new(car.attributes, car.stops)
end
end
@schedule[$time.to_i] = nil #reset cars in minutes to nil to free memory
end
#end of processing methods
#---------------------------------------------------------------------------
#begin helper classes
class Timer
include Processing::Proxy #includes all the processing methods
def initialize
@time_tween = 0.0
@end_time = 1440.0
end
def update
#time counter tween
@time_tween += (100 - @time_tween)/$delay
#update function to draw time text
if @time_tween > 95
$time += 1
@time_tween = 0
#stop animation and set time to 0 if it reaches end of time
if $time >= @end_time
$time = 0.0
$timeline_loop = false
end
end
#timeline
@tick_increment = ($app.width.to_f) / 24.0
@tick_spacing = []
(1..24).each {|i| @tick_spacing.push(i * @tick_increment)}
@tick_spacing.each do |spacing|
stroke(135)
stroke_weight(2)
stroke_cap(SQUARE)
line(spacing, 0, spacing, 20)
stroke(85)
line((spacing - (@tick_increment / 2)), 0, (spacing - (@tick_increment / 2)), 14)
end
#line for actual minute
stroke(255, 0, 0)
stroke_weight(4)
@loc_timeline = map($time, 0, 1440, 0, $app.width)
line(@loc_timeline, 0, @loc_timeline, 28)
#text draw and update every frame
@hour = ($time / 60.0).floor
@minute = $time - (@hour * 60.0)
@text_translate = map($time, 0.0, 1440.0, 0.0, 42.0 )
fill 255
text( @hour.to_i.to_s + "." + @minute.to_i.to_s, @loc_timeline - @text_translate, 43)
end
end
class Vehicle
include Processing::Proxy
def initialize(attributes, init_stops)
#save ID for later use
@route_id = attributes["id"]
#split color and assign it to each RBGA value
color = attributes["color"].split(",")
@r = color[0].to_i
@g = color[1].to_i
@b = color[2].to_i
@a = 255.0
#load stops
@stops = init_stops
#instance variables for current stop and next stop
@current_stop = 0
@last_stop = attributes["stop_count"] - 1
#set location data
@current_x = normalize_x(@stops[@current_stop].lon)
@current_y = normalize_y(@stops[@current_stop].lat)
@next_x = @current_x
@next_y = @current_y
#trigger time
@trigger_time = @stops[@current_stop].departure
#add to active cars
$active_cars << self
end
def normalize_y(coord)
map(coord, 40.632836, 40.903125, $app.height, 0)
end
def normalize_x(coord)
map(coord, -74.020065, -73.78121, 0 + 50, $app.width - 50)
end
#method to update next target location
def set_next_stop
@current_stop += 1
@next_x = normalize_x(@stops[@current_stop].lon)
@next_y = normalize_y(@stops[@current_stop].lat)
@trigger_time = @stops[@current_stop].departure
end
def update
#set color for the train
fill @r, @g, @b, @a
no_stroke
#easing animation to next location
@current_x += (@next_x - @current_x)/$delay
@current_y += (@next_y - @current_y)/$delay
#draw circle for train
ellipse(@current_x.to_i, @current_y.to_i, $radius, $radius)
#check trigger time and update next location
if @trigger_time == $time && @current_stop < @last_stop
set_next_stop
elsif @trigger_time == $time && @current_stop == @last_stop
@a = 0
$active_cars.delete_if {|car| car == self }
end
end
end