-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelection_2008.rb
More file actions
69 lines (61 loc) · 1.61 KB
/
Copy pathelection_2008.rb
File metadata and controls
69 lines (61 loc) · 1.61 KB
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
# Based on JAVA example at http://en.wikipedia.org/wiki/Processing_%28programming_language%29
@@usa = PShape.new
COLORS = {:democrat => "#0000ff", :republican => "#ff0000"}
LEGEND_POSITION = [680, 50]
@@results = {
:democrat => {
:candidate => 'Obama',
:states => %w{HI RI CT MA ME NH VT NY NJ
FL NC OH IN IA CO NV PA DE MD MI
WA CA OR IL MN WI DC NM VA}
},
:republican => {
:candidate => 'McCain',
:states => %w{AK GA AL TN WV KY SC WY MT
ID TX AZ UT ND SD NE MS MO AR OK
KS LA}
}
}
def setup
size 950, 600
# Blank_US_Map.svg file can be found at Wikimedia Commons
@@usa = load_shape "http://upload.wikimedia.org/wikipedia/commons/3/32/Blank_US_Map.svg"
smooth # Improves the drawing quality of the SVG
text_font create_font("SanSerif", 30)
no_loop
end
def draw
background 0,0,0,0
# Draw the full map
# shape @@usa, 0, 0
shape @@usa
i = 0
@@results.each do |party, result|
candidate = result[:candidate]
states = result[:states]
c = color(hex(COLORS[party]))
legend i, c, candidate
color_states states, c
i += 1
end
# Save the map as image
save_frame "../output/election_2008.png"
end
def color_states states, c
states.each do |s|
state = @@usa.getChild(s)
# Disable the colors found in the SVG file
state.disable_style
# Set our own coloring
fill c
no_stroke
# Draw a single state
# shape state, 0, 0
shape state
end
end
def legend n, color, candidate
fill color
text_height = text_ascent + text_descent
text candidate, LEGEND_POSITION.first, LEGEND_POSITION.last + (n+1) * text_height
end