Coordinate System in Windows GDI (2)
By leezy_2000
1. How is the coordinate of the Logical space converted into the coordinate of the device space?
Let's clarify how the logical coordinate space is converted.
The transformation from the world coordinate space to the page coordinate space (two-dimensional affine(Note 3)Conversion ):
Various transformations involved in this process, such as equality, translation, scaling, image, rotation, cut, and merge, are implemented by specifying appropriate values for each member of the affine matrix.
The structure of this matrix is as follows:
typedef struct _XFORM {
FLOAT eM11;
FLOAT eM12;
FLOAT eM21;
FLOAT eM22;
FLOAT eDx;
FLOAT eDy;
} XFORM, *PXFORM;
The formula for converting a point in a world coordinate space to a point in a page coordinate space is as follows:
Xpage = xworld * em11 + yworld * em21 + EDX;
Ypage = xworld * EM12 + yworld * em22 + Edy;(Formula 1)
Xworld and yworld are the points in the world coordinate space. Xpage and ypage are the locations corresponding to the preceding points in the coordinate space of the page. As for the mathematical attributes of affine transformations and how to achieve equal page size, translation, scaling, image, rotation, cutting, and merging, this section does not provide a detailed description, this will expand the size of this article (note 4 ).
Conversion from page coordinate space to device coordinate space:
This process involves several concepts:
View origin: the origin of the device coordinate space in the current page coordinate space. UseSetviewportorgex and getviewportorgexSet and read respectively. When using these two functions, the coordinates involved are the coordinates of the device space.
View range: The view range is not an absolute value used to indicate the size of the device's coordinate space. It is a relative value, and the proportion of the same window range determines whether the page coordinate space is a zoom-in or zoom-in conversion from the page coordinate space to the device coordinate space. UseSetviewportextex,GetviewportextexAccess the access port range.
Window origin: the origin of the page coordinate space. UseSetdomainworgex, getdomainworgexAccess the window origin. The coordinates involved are logical coordinates.
Window range: see the description of the window range. UseSetmediawextex,Getwindowextex.
The coordinate transformation between the two coordinate spaces is not too difficult because of the ratio of the origin value and range of the two coordinate spaces. The following formula can be obtained easily:
Page coordinate space to device coordinate space:
Xdevice = (xpage-worgx) * vextx/wextx + vorgx;
Ydevice = (ypage-worgy) * vexty/wexty + vorgy;(Formula 2)
(Worgx, worgy) indicates the origin of the window. (Vorgx vorgy,) is the origin of the view. (Wextx, wexty) is the window range. (Vextx, vexty) is the range of the view.
You can deduce the conversion from the device coordinate space to the page coordinate space.
To better understand the transformation of coordinate space, we will use the above two sets of formulas to implement our ownLptodp. Our function will only be applicable to platforms with NT classes. (9x does not have the world coordinate space, it will be simpler) for specific implementation, see Source Code 2. The process of implementing mylptodp is relatively simple. Here we will only describe the main functions to be used.
int GetGraphicsMode(
HDC hdc // handle to device context
);
This function is used to obtain the graphic mode of the specified DC. There are two graphical modes: gm_compatible and gm_advanced.
Only gm_advanced can use the world coordinate space. You can use setgraphicsmode to switch between the two.
BOOL GetWorldTransform(
HDC hdc, // handle to device context
LPXFORM lpXform // transformation
);
This function is used to obtain the affine matrix associated with the current DC. Using Formula 1,
It should be known that the default affine matrix has the form of {1.0, 1.0.
Although mylptodp has a return value, this value is meaningless and no error processing is performed during implementation.
See Source Code 2.
Ii. supplementary instructions on GDI +
For various coordinate spaces, the update of GDI + is more reflected in the operation method than in essence. After understanding the above concepts and looking at the coordinate space of GDI +, we will have a clear feeling. This article focuses more on the establishment of concepts, so we will not separately detail the GDI +.
Note 1: This is only one possible scenario. The specific converted value must be determined by the current coordinate space.
NOTE 2: I personally think that the coordinate space of GDI is actually a coordinate system, but this is also true because the corresponding English term is coordinate space and most books are translated into a coordinate space.
NOTE 3: You all know that You can perform operations such as scaling, rotating, and mirroring by multiplying a 2x2 matrix.
For example:
These transformations are called linear transformations. However, by multiplying 2x2 matrices, you cannot perform operations such as translation. To achieve the goal of translation, you also need to add a set of offsets (corresponding to the X axis and Y axis respectively ). A 2x2 matrix and a set of offsets constitute the affine matrix.
Note 4: see Feng Yuan's Windows graphic programming