lib/gull/polygon.rb in gull-0.2.5 vs lib/gull/polygon.rb in gull-0.2.6

- old
+ new

@@ -5,26 +5,23 @@ def initialize polygon self.coordinates = polygon.split(" ").collect {|coords| coords.split(",").collect {|coord| coord.to_f} } end def centroid - low_x, low_y, high_x, high_y = 0, 0, 0, 0 + low_x = 0 + low_y = 0 + high_x = 0 + high_y = 0 coordinates.each do |pair| - latitude = pair.first - if latitude < low_x or low_x == 0 - low_x = latitude - elsif latitude > high_x or high_x == 0 - high_x = latitude - end + x_bounds = bounds pair.first, low_x, high_x + low_x = x_bounds.first + high_x = x_bounds.last - longitude = pair.last - if longitude < low_y or low_y == 0 - low_y = longitude - elsif longitude > high_y or high_y == 0 - high_y = longitude - end + y_bounds = bounds pair.last, low_y, high_y + low_y = y_bounds.first + high_y = y_bounds.last end center_x = low_x + ((high_x - low_x) / 2) center_y = low_y + ((high_y - low_y) / 2) @@ -45,9 +42,19 @@ "#{url_base}?size=#{opts[:width]}x#{opts[:height]}&maptype=#{opts[:maptype]}&path=color:#{opts[:color]}" + "|weight:#{opts[:weight]}|fillcolor:#{opts[:fillcolor]}|#{coordinates_piped}&key=#{api_key}" end private + + def bounds point, low, high + if point < low or low == 0 + low = point + elsif point > high or high == 0 + high = point + end + + [low, high] + end def coordinates_piped coordinates.collect {|pair| pair.join "," }.join "|" end \ No newline at end of file