Skip to content

Commit 6ab74c4

Browse files
committed
Uncouple CSV load from draw loop
1 parent 6067024 commit 6ab74c4

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

processing_app/library/vector_utils/lat_lon.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,45 @@
44
require 'arcball'
55
require 'csv'
66
# Capital name, Latitude and Longitude from csv
7-
class LatLong < Propane::App
87

8+
Place = Struct.new(:lat, :lon, :name)
9+
10+
11+
class LatLong < Propane::App
912
load_library :vector_utils
1013
# Uses to_cartesian to map lat lon of cities, read from a csv file, on a globe
14+
attr_reader :places
1115
def setup
1216
sketch_title 'Latitude and Longitude'
1317
noStroke
1418
textSize(9)
1519
Processing::ArcBall.init self
20+
@places = []
21+
CSV.foreach(data_path('capitals.csv'), headers: true) do |row|
22+
lat = row['CapitalLatitude'].to_f
23+
lon = row['CapitalLongitude'].to_f
24+
name = row['CapitalName']
25+
places << Place.new(lat, lon, name)
26+
end
1627
end
1728

1829
def draw
1930
background(20)
2031
lights
2132
counter = 0
22-
CSV.foreach(data_path('capitals.csv'), headers: true) do |row|
23-
lat = row['CapitalLatitude'].to_f
24-
lon = row['CapitalLongitude'].to_f
25-
name = row['CapitalName']
26-
p = VectorUtil.to_cartesian(lat: lat, long: lon, radius: 300)
33+
places.each do |place|
34+
p = VectorUtil.to_cartesian(lat: place.lat, long: place.lon, radius: 300)
2735
push_matrix
2836
translate(p.x, p.y, p.z)
2937
polar = VectorUtil.cartesian_to_polar(vec: p)
3038
rotate_y(polar.y)
3139
rotate_z(polar.z)
3240
push_matrix
3341
fill(255)
34-
text(name,0,0) if (counter % 3).zero?
42+
text(place.name,0,0) if (counter % 3).zero?
3543
pop_matrix
36-
box(10, 3, 3)
44+
fill(255, 255, 0, 100)
45+
box(8, 3, 3)
3746
pop_matrix
3847
counter += 1
3948
end

0 commit comments

Comments
 (0)