Search code examples
qtmathcoordinate-transformation

Strategy to find screen coordinates that span the entire canvas


I am writing an app to display bike rack stations in a city. I am pulling the coordinates from a json file and trying to convert the latitude/longitudes into screen coordinates.

The framework I am using (Qt) calculates y from top to bottom and x from left to right, so obviously I wrote conversion functions for this. The conversion functions look like this:

/* 
The minLatitude has already been calculated to be
the latitude of the southernmost rack

*/
qreal BikeRackSystem::getY(qreal latitude)
{
  qreal diffY = latitude - minLatitude;
  return height - (diffY*height);
}

/* 
The minLatitude has already been calculated to be
the latitude of the southernmost rack

*/
qreal BikeRackSystem::getX(qreal longitude)
{
  qreal diffX = longitude - minLongitude;
  return width * diffX;
}

However I end up with this kind of image

enter image description here

This is close, but not quite what I was looking for. How can I change my conversion functions to give me screen coordinates that span the whole screen?

Edit: If it is at all interesting...I am using QGraphicsEllipseItem subclasses to draw the racks on a QGraphicsScene.


Solution

  • I found out that my conversion function wasn't properly set up. I found the best answer here

    The crucial part mentioned in the answer was:

            x - min                                  max - min
    f(x) = ---------   ===>   f(min) = 0;  f(max) =  --------- = 1
           max - min                                 max - min