Scaling a polygon on a map- the latitude refuses to scale
<p><strong>Problem:</strong></p>

<p>I'm trying to scale a polygon on a map, and it appears to be working in the horizontal(longitude),
but the vertical(latitude) remains unchanged.</p>

<p><strong>How I approached it</strong></p>

<p>A transformation with these steps:</p>

<p>1)Converting the latitude and longitude into Cartesian points</p>

<p>2)Moving points to object space by doing a difference with each point and the polygon centroid</p>

<p>3)Scaling each point by a factor</p>

<p>4)Moving them back out into world space by adding back in the centroid.</p>

<p><strong>A Possible Cause:</strong></p>

<p>During the translating and scaling I'm only manipulating the x and y values. Since the
scaling is only of a 2D shape I figured it could be disregarded, but maybe I need to manipulate
it as well somewhere in the process.</p>

<p>My Code: Main Execution Stack followed by all the functions they use.</p>

<pre><code>var originalLatLngs = [{lat:45.250589,lon:-116.653149},{lat:45.250603,lon:-116.651856},{lat:45.250269,lon:-116.651845},{lat:45.250261,lon:-116.653146}];
var cartesianPoints = bounds.map(function(bound){
  return toCartesian(bound.lat,bound.lon);
});
var centriod = getPolygonCentroid(cartesianPoints);
var objectCoordinates = toObjectSpace(centriod,cartesianPoints);
var scaledCoordinates = scaleCoordinates(2,objectCoordinates);
var marginCoordinates = toWorldSpace(centriod,scaledCoordinates);
var newLatLngs = marginCoordinates.map(function(marginCoordinate){
  return toLatLng(marginCoordinate.x,marginCoordinate.y,marginCoordinate.z);
});


function getPolygonCentroid(vertices){
  var centroid = {x:0,y:0};
  var signedArea = 0;
  var x0 = 0; 
  var y0 = 0; 
  var x1 = 0; 
  var y1 = 0; 
  var a = 0;  

  for (var i=0; i&lt;vertices.length-1; i++){
      x0 = vertices[i].x;
      y0 = vertices[i].y;
      x1 = vertices[i+1].x;
      y1 = vertices[i+1].y;
      a = x0*y1 - x1*y0;
      signedArea += a;
      centroid.x += (x0 + x1)*a;
      centroid.y += (y0 + y1)*a;
  }
  x0 = vertices[i].x;
  y0 = vertices[i].y;
  x1 = vertices[0].x;
  y1 = vertices[0].y;
  a = x0*y1 - x1*y0;
  signedArea += a;
  centroid.x += (x0 + x1)*a;
  centroid.y += (y0 + y1)*a;

  signedArea *= 0.5;
  centroid.x /= (6.0*signedArea);
  centroid.y /= (6.0*signedArea);

  return centroid;
}

function toObjectSpace(centroid,worldCoordinates){
  var objectCoordinates = [];
  worldCoordinates.forEach(function(worldCoordinate){
    var newXCoordinate = worldCoordinate.x - centroid.x;
    var newYCoordinate = worldCoordinate.y - centroid.y;
    var orginalZCoordinate = worldCoordinate.z;
    objectCoordinates.push({x:newXCoordinate,y:newYCoordinate,z:originalZCoordinate});
  });
  return objectCoordinates;
}

function scaleCoordinates(factor,coordinates){
  var scaledCoordinates = [];
  coordinates.forEach(function(coordinate){
    var newXCoordinate = coordinate.x * factor;
    var newYCoordinate = coordinate.y * factor;
    var orginalZCoordinate = coordinate.z;
    scaledCoordinates.push({x:newXCoordinate,y:newYCoordinate,z:originalZCoordinate});
  });
  return scaledCoordinates;
}

function toWorldSpace(centroid,objectCoordinates){
  var worldCoordinates = [];
  objectCoordinates.forEach(function(objectCoordinate){
    var newXCoordinate = objectCoordinate.x + centroid.x;
    var newYCoordinate = objectCoordinate.y + centroid.y;
    var orginalZCoordinate = objectCoordinate.z;
    worldCoordinates.push({x:newXCoordinate,y:newYCoordinate,z:originalZCoordinate});
  });
  return worldCoordinates;
}

var toCartesian = function(lat,lon){
  const R = 6371;

  var x = R * Math.cos(deg2rad(lat)) * Math.cos(deg2rad(lon));
  var y = R * Math.cos(deg2rad(lat)) * Math.sin(deg2rad(lon));
  var z = R * Math.sin(deg2rad(lat));

  return {
    x: x,
    y: y,
    z: z
  }
}

var toLatLng = function(x,y,z){
  const R = 6371;
  var lat = Math.asin(z / R);
  var lon = Math.atan2(y, x);
  return {
    lat:rad2deg(lat),
    lon:rad2deg(lon)
  }
}

var deg2rad = function(deg) {
  return deg * (Math.PI/180)
}

var rad2deg = function(radians) {
  return radians * 180 / Math.PI;
};
</code></pre>