
Programming Praxis Tracking Santa
By: a guest on
Dec 24th, 2010 | syntax:
Ruby | size: 1.43 KB | hits: 46 | expires: Never
#!/usr/bin/env ruby
require 'net/http'
def law_of_cosine(lat1, long1, lat2, long2)
Math.acos( (Math.sin(lat1)*Math.sin(lat2)) + (Math.cos(lat1)*Math.cos(lat2)) * Math.cos((long1-long2).abs))
end
def great_circle_distance(rads)
6371.01 * rads
end
santas_distance = 0
d = { "firstlat" => 0, "firstlng" => 0, "secondlat" => 0, "secondlng" => 0}
url = URI.parse('http://www.noradsanta.org/js/data.js')
req = Net::HTTP::Get.new(url.path)
resp = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
resp.body.each do |line|
line.chomp
puts line
if line.include? "lat" and d['firstlat'] == 0
arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
d['firstlat'] = arr[0].to_i
elsif line.include? "lat"
arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
d['secondlat'] = arr[0].to_i
end
if line.include? "lng" and d['firstlng'] == 0
arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
d['firstlng'] = arr[0].to_i
elsif line.include? "lng"
arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
d['secondlng'] = arr[0].to_i
end
if d['firstlat'] != 0 and d['secondlat'] != 0 and d['firstlng'] != 0 and d['secondlng'] != 0
rads = law_of_cosine(d['firstlat'], d['firstlng'], d['secondlat'], d['secondlng'])
santas_distance += great_circle_distance(rads)
d['firstlat'] = 0
d['firstlng'] = 0
d['secondlat'] = 0
d['secondlng'] = 0
end
end
puts santas_distance