Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused between logical coordinates and device coordinates in Windows API

I have been looking into a Visual Studio C++ Windows application project which used two functions SetWindowExt (...) and SetViewportExt (...). I am confused about what these two functions do and why they are necessary. Searching about these functions, I came to the concept of logical coordinates and device coordinates.

Can anyone please explain what is the importance of these two concepts?

like image 355
Viku Avatar asked Apr 07 '13 12:04

Viku


1 Answers

Device coordinates are the simplest to understand. They are directly related to the device that you're using—e.g., the screen or a printer.

For an example, let's look at a window displayed on the screen. Device coordinates are defined relative to a particular device, so in the case of a window, everything will be in client coordinates. That means the origin will be the upper-left corner of the window's client area and the y-axis will run from top to bottom. All units are measured in pixels, since this is an on-screen element.

You use these all the time, so you probably already understand them better than you think. For example, whenever you handle a mouse event or a window resize, you get and set device coordinates.

Logical coordinates take the current mapping mode into account. Each device context (DC) can have a mapping mode applied to it (GetMapMode and SetMapMode). The various available mapping modes are defined by the MM_Xxx values. Each of these different mapping modes will cause the origin and y-axis direction to be interpreted differently. The documentation will tell you exactly how they work.

When you manipulate a device context (e.g., draw onto it), the current mapping mode is taken into account and thus you work with logical coordinates.

With the default MM_TEXT mapping mode, each logical unit maps to one device unit (remember, for a window, this would be one pixel), so no conversion is required. In this mapping mode, the logical and device coordinate systems work exactly the same way. And since this is the default and probably the one you work with most of the time, it is probably the source of your confusion.

Relevant reading: Coordinate Spaces and Transformations (MSDN)

like image 178
Cody Gray Avatar answered Oct 19 '22 20:10

Cody Gray