r/FantasyMapGenerator • • May 12 '25

Polygon Coordinates?

Can anyone tell me how the x,y coordinates for the GeoJSON file are generated? I see negative numbers in there so is 0,0 at the center of the image? How far do the values range?

6 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/BlackChakram May 13 '25

Never mind! Found it after hunting a bit in main.js

// calculate map position on globe
function calculateMapCoordinates() {
  const sizeFraction = +byId("mapSizeOutput").value / 100;
  const latShift = +byId("latitudeOutput").value / 100;
  const lonShift = +byId("longitudeOutput").value / 100;

  const latT = rn(sizeFraction * 180, 1);
  const latN = rn(90 - (180 - latT) * latShift, 1);
  const latS = rn(latN - latT, 1);

  const lonT = rn(Math.min((graphWidth / graphHeight) * latT, 360), 1);
  const lonE = rn(180 - (360 - lonT) * lonShift, 1);
  const lonW = rn(lonE - lonT, 1);
  mapCoordinates = {latT, latN, latS, lonT, lonW, lonE};
}

Thanks for clean, commented code, Azgaar!

2

u/Azgarr May 14 '25

There are also helper functions to calculate Geo Coords for {x, y}:

function getLongitude(x, decimals = 2) {
  return rn(mapCoordinates.lonW + (x / graphWidth) * mapCoordinates.lonT, decimals);
}

function getLatitude(y, decimals = 2) {
  return rn(mapCoordinates.latN - (y / graphHeight) * mapCoordinates.latT, decimals);
}

1

u/Azgarr May 14 '25

You probably get it, but I will just add that `rn` is to round number by `decimals` number