require 'json'
require 'object_hash_rb'
require 'spherical_mercator'

REVERSE = false

# Modify this.
INPUT_DATA = [
  [
    [0.0, 0.0],
    [0.0, -66.5],
    [90.0, -66.5],
    [90.0, 0.0]
  ],
  [
    [41.1328125, -14.8120600612],
    [37.5897216796, -16.2515937327],
    [35.8978271484, -18.208480196],
    [35.5517578125, -20.0094839553],
    [35.8428955078, -21.4837409071],
    [38.1390380859, -24.9810788582],
    [42.2973632812, -25.1950004243],
    [44.1430664062, -24.8266249565],
    [44.7747802734, -25.2049411535],
    [43.7255859375, -26.608174374],
    [43.8079833984, -28.6664911769],
    [45.4010009765, -29.1233732108],
    [45.8129882812, -29.7166812872],
    [46.0162353515, -30.8031925462],
    [44.4177246093, -31.0670507727],
    [43.9398193359, -31.2879398926],
    [42.9345703125, -31.5083126989],
    [42.099609375, -30.5149490451],
    [41.6326904296, -30.3396948489],
    [40.2099609375, -31.1893084395],
    [39.5068359375, -31.6487052899],
    [39.0179443359, -31.5176787812],
    [38.9959716796, -31.1893084395],
    [39.2321777343, -30.991737045],
    [38.4411621093, -30.6095497971],
    [37.8149414062, -31.0246941285],
    [37.5128173828, -31.1329014011],
    [37.271118164, -30.869225348],
    [37.1887207031, -29.9692116596],
    [37.1337890625, -29.7739138699],
    [36.3537597656, -29.4300294045],
    [35.7659912109, -29.4252449854],
    [34.9200439453, -28.3865678196],
    [34.5849609375, -27.693256343],
    [34.3212890625, -27.0786915529],
    [33.8049316406, -26.9122738266],
    [33.2885742187, -27.2399786718],
    [33.22265625, -27.8099277809],
    [32.3822021484, -28.105903469],
    [32.173461914, -28.3188889157],
    [31.9812011718, -28.8783496476],
    [32.255859375, -29.3534516686],
    [31.5856933593, -30.5054838989],
    [31.7449951171, -30.8079106813],
    [31.4099121093, -31.0529339857],
    [30.6518554687, -32.3010630253],
    [30.8935546875, -32.6671247331],
    [29.3939208984, -33.073130945],
    [29.1412353515, -33.3351177475],
    [28.3172607421, -33.655780832],
    [27.8723144531, -33.4726901926],
    [27.3999023437, -33.3442960191],
    [26.3397216796, -32.9856279745],
    [25.1037597656, -33.1513493853],
    [24.7467041015, -33.3580616127],
    [23.3679199218, -34.4205048801],
    [21.3739013671, -37.8141237016],
    [20.8959960937, -38.0091482264],
    [19.8248291015, -40.0192013076],
    [18.10546875, -41.2901899558],
    [17.847290039, -42.6299172938],
    [15.8038330078, -44.0244215196],
    [15.7818603515, -44.5943789672],
    [16.1059570312, -46.0960908021],
    [18.0120849609, -46.589069083],
    [17.8747558593, -47.5209104785],
    [19.8852539062, -49.0126538639],
    [20.4400634765, -49.0234614632],
    [20.9289550781, -49.2606351836],
    [21.4178466796, -48.9261085776],
    [22.2583007812, -48.6256474088],
    [22.7032470703, -48.8104811247],
    [23.8513183593, -48.9946359835],
    [23.7194824218, -49.9724223542],
    [25.1641845703, -51.0517543646],
    [25.8618164062, -52.8757609818],
    [26.8780517578, -54.4540733252],
    [26.5100097656, -55.0909436222],
    [25.6365966796, -55.4913036282],
    [24.5050048828, -56.752722872],
    [24.7412109375, -57.6777230585],
    [24.0380859375, -58.4936938205],
    [24.351196289, -58.7425567687],
    [24.3347167968, -59.2012513234],
    [25.6585693359, -59.5036673571],
    [27.3889160156, -60.4788788301],
    [28.9929199218, -60.6758689536],
    [30.5859375, -60.4518002807],
    [30.9594726562, -60.0648404601],
    [32.4975585937, -59.5426724336],
    [34.62890625, -59.5231755354],
    [36.4141845703, -59.1899989406],
    [37.5457763671, -59.0913823845],

    [39.10583496093751, -58.967003820180736],
    [40.02319335937501, -59.00945615147479],
    [40.72631835937501, -59.377988012638895],
    [41.37451171875, -59.37239142233717],
    [42.20397949218751, -58.805207224740826],
    [42.2039794921875, -58.2863954828810341],
    [43.3465576171, -58.3757983403],
    [46.0656738281, -58.341217608],
    [46.3568115234, -58.4017116676],
    [54.2944335937, -58.7682001592],
    [62.4243164062, -58.1938712649],
    [66.1596679687, -56.6562264935],
    [67.2802734375, -53.6316106065],
    [64.9731445312, -50.1346643221],
    [71.2956682377, -40.6795653955],
    [75.3552246093, -39.1556223934],
    [87.0502006721, -35.1690949038],
    [87.9824215262, -33.3976364174],
    [86.0247577326, -31.4568768991],
    [83.2280951703, -22.9855315232],
    [86.8326824728, -19.2470122812],
    [80.5557287218, -15.8702061022],
    [73.5329982876, -16.5266913219],
    [70.4255954406, -15.3015121521],
    [60.1711660454, -16.854104691],
    [55.1682474618, -19.3350001987],
    [53.1484356112, -18.835780817],
    [52.3715848994, -17.8920635791],
    [50.9111055614, -17.684944907],
    [49.6681444225, -18.3350732278],
    [45.0381141805, -14.8214087646]
  ]
]

def spherical_mercator(latlng)
  d = 180 / Math::PI
  max = 85.0511287798
  lat = latlng[0].clamp(-max, max)
  sin = Math.sin(lat * d)
  r = 6378137 # Earth radius.

  return [
    (2 * Math.atan(Math.exp(latlng[1] / r)) - (Math::PI / 2)) * d,
		latlng[1] * d / r
  ]
end

def truncate_number(input)
  # Truncate to 5 digits.
  format('%<input>.5f', input: input).to_f
end

MULTIPLIER = [
  0.5, -0.5
]
MERCATOR = SphericalMercator.new(size: 256, round: false)
ORIGIN = MERCATOR.px([0,0], 1)
def reproject_point(point)
  # projected = spherical_mercator([point[REVERSE ? 0 : 1], point[REVERSE ? 1 : 0]])
  projected = MERCATOR.px(point, 1)
  return [(projected[0] - ORIGIN[0]) * MULTIPLIER[0], (projected[1] - ORIGIN[1]) * MULTIPLIER[1]]
end

def reproject(data)
  if (data.length == 2 && data[0].is_a?(Numeric)) then
    return reproject_point(data)
  else
    return data.map { |value| reproject(value) }
  end
end

def main
  puts(JSON.pretty_generate(reproject(INPUT_DATA)))
  #puts(MERCATOR.px([90, -66.5], 1))
  #puts(MERCATOR.px([0,0], 1))
end

main