viewport to window mapping - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# viewport to window mapping

Posts: 1Member
Hi ...
I need to draw a map .I have the latitude longitude values of it .I was able to map these to screen coordinates and then draw the map.Once the map is drawn,as the mouse moves over the map i need to show the latitude and longitude values. This is where i am struck .For the initial case its simple,just rearrangement of window to view port mapping formula .i.e.,
u = (x-xmin)* sx+ umin
v = (y-ymin)* sy + vmin
But once the zoom and pan operations are applied on the map, I dont know how to get the latitude longitude values for each screen coordinate .

• Posts: 4Member
This is how I do it. Going from window to viewport coordinates is a linear transform of this kind (dunno if I can post LaTeX code here, it would look way better):

| Xs | = |eM11 0 | |X| + |eDx|
| Ys | |0 eM22| |Y| |eDy|

(Xs,Ys) are screen coordinates, (X,Y) are window/world coordinates.
This transform represents scaling in x (given by eM11), scaling in y (given by eM22) and translation (given by eDx and eDy). Scaling = zoom (eM11 and eM22 will always have the same value), translation = pan.

So, when you zoom, if the structure which represents the transform is called m_TWorld2Screen, you have to update eM11 and eM22, but also eDx and eDy, since the translation is affected by zoom (it's not the same distance):

[code]
m_TWorld2Screen.eM11 = newVal;
m_TWorld2Screen.eM22 = newVal;
m_TWorld2Screen.eDx = (m_TWorld2Screen - m_iWidth/2.0f) * newVal / oldZoomFactor + m_iWidth/2.0f;
m_TWorld2Screen.eDy = (m_TWorld2Screen.eDy- m_iHeight/2.0f) * newVal / oldZoomFactor + m_iHeight/2.0f;
[/code]

-newVal is the new zoom factor (between 0 and 1)
-m_iWidth and m_iHeight are the viewport's dimensions
-oldZoomFactor is the previous zoom factor

And when you pan, you only need to update eDx and eDy:

[code]
m_TWorld2Screen.eDx = - m_TWorld2Screen.eDx / m_TWorld2Screen.eM11;
m_TWorld2Screen.eDy = - m_TWorld2Screen.eDy / m_TWorld2Screen.eM22;
[/code]

The proof of these formulas is quite tedious to write without LaTeX, but this works for me.
You might need to define m_TScreen2World to do the inverse transform. If you understood my concept, ask away