|
4 | 4 | require 'arcball' |
5 | 5 | require 'csv' |
6 | 6 | # Capital name, Latitude and Longitude from csv |
7 | | -class LatLong < Propane::App |
8 | 7 |
|
| 8 | +Place = Struct.new(:lat, :lon, :name) |
| 9 | + |
| 10 | + |
| 11 | +class LatLong < Propane::App |
9 | 12 | load_library :vector_utils |
10 | 13 | # Uses to_cartesian to map lat lon of cities, read from a csv file, on a globe |
| 14 | + attr_reader :places |
11 | 15 | def setup |
12 | 16 | sketch_title 'Latitude and Longitude' |
13 | 17 | noStroke |
14 | 18 | textSize(9) |
15 | 19 | 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 |
16 | 27 | end |
17 | 28 |
|
18 | 29 | def draw |
19 | 30 | background(20) |
20 | 31 | lights |
21 | 32 | 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) |
27 | 35 | push_matrix |
28 | 36 | translate(p.x, p.y, p.z) |
29 | 37 | polar = VectorUtil.cartesian_to_polar(vec: p) |
30 | 38 | rotate_y(polar.y) |
31 | 39 | rotate_z(polar.z) |
32 | 40 | push_matrix |
33 | 41 | fill(255) |
34 | | - text(name,0,0) if (counter % 3).zero? |
| 42 | + text(place.name,0,0) if (counter % 3).zero? |
35 | 43 | pop_matrix |
36 | | - box(10, 3, 3) |
| 44 | + fill(255, 255, 0, 100) |
| 45 | + box(8, 3, 3) |
37 | 46 | pop_matrix |
38 | 47 | counter += 1 |
39 | 48 | end |
|
0 commit comments