Search code examples
delphicoordinatescoordinate-transformation

Game Map Viewer MouseX,Y to Long,Latitude


I am not sure how to word this correctly however i have been working on a map viewer that loads a bitmap file to a Direct2D surface. However i want the map viewer mouse X,Y coordinates to match those similar to the game coordinates.

Here is a sample picture of what i am referring to:

https://i.sstatic.net/FOaxG.png

The left map viewer is a separate application, i am trying to mimic this in my own map viewer, however you can see i am at the bottom left corner on both forms, and the coordinates do not match the Lon/Lat of my map viewer.

We are 4624 pixels wide, the bottom left corner should convert to Lon: 16384 Lat: 24577 whilst the top right hand corner will convert to Lon: 90111 Lat: 98304

var
  BSize: TD2DSizeF;
  S4, S5: string;
  FPos: TPoint;
begin
  S4 := 'Longitude: '+FloatToStr((BSize.Width / 360) * (180 + FPos.X));
  S5 := 'Latitude: '+FloatToStr((BSize.Height / 180) * (90 - FPos.Y));

Can anyone point me in the right direction to work this out? Am i misunderstanding the concept?


Solution

  • If I understood you correctly, the formulae should be:

    Lon := 16384 + x / MaxX * (90111 - 16384);
    Lat := 98304 - y / MaxY * (98304 - 24577);
    

    Where MaxX and MaxY are the pixel coordinates of the low-right corner (i.e. Width - 1 and Height - 1, respectivelly).