Coordinate system in Windows GDI (2)
By Leezy_2000
2003-10-21 16:13
First, how is the coordinates of logical space transformation into coordinates of equipment space?
Let us first clarify how the logical coordinate space is transformed.
World coordinate space to the transformation of the page coordinate space (2D Affine (Note 3) Transform):
Various transformations involved in this process, such as: equal, translation, scaling, image, rotation, cutting, merge, etc. are implemented by assigning appropriate values for each member of the Affine matrix.
The structure corresponding to this matrix is as follows:
Typedef struct _xform {
Float EM11;
FLOAT EM12;
Float EM21;
Float EM22;
Float EDX;
FLOAT EDY;
} XForm, * PxForm;
The formula of the point of a world coordinate space is converted to the point of the page coordinate space is:
XPage = XWORLD * EM11 YWORLD * EM21 EDX;
YPAGE = xWorld * EM12 YWORLD * EM22 EDY; (Formula 1)
Where xWorld, YWorld is the point of the world coordinate space. XPage, YPAGE The location of the above point in the page coordinate space. As for the mathematical properties of the Affine transform and how to achieve equal, translation, scaling, image, rotation, cutting, and merge, and merge, which will expand many (Note 4).
Page coordinate space to the conversion of the device coordinate space:
This process involves several concepts, they are:
Viewport origin: The origin position of the equipment coordinate space that the current page coordinate space is considered. Set and read and read separately with SETVIEWPORTORGEX, GetViewPortorgex. When operating with these two functions, the coordinates involved are coordinates of the equipment space.
Viewing range: The viewport range is not an absolute value for indicating the size of the equipment coordinate space. It is a relative value, which ultimately determines the page coordinate space to the device coordinate space is a reduction or amplification conversion. Access to the viewport range with SetViewPortextex and GetViewPortexTex.
Window origin: The origin of the page coordinate space. With SETWINDOWORGEX, GetWindOWorgex is accessed to the window origin, and the coordinates involved are logical coordinates.
Window range: See the viewpoint of the viewport. Access with SetWindowExtex, getWindowExtex.
The proportional value of the origin and range of the two coordinate spaces is not too difficult to make coordinate conversions between these two coordinate spaces. It is easier to get the following formula:
Page coordinate space to device coordinate space:
XDevice = (XPage-WORGX) * VEXTX / WextX VorgX;
YDEVICE = (YPAGE-WORGY) * VEXTY / Wexty Vorgy; (Formula 2)
Among them, WORGX, WORGY is the origin of the window. (Vorgx Vorgy,) is the origin of the viewport. (WeXTX, WexTy) is a window range. (VEXTX, VEXTY) is a viewport range.
The conversion of equipment coordinate space to page coordinate space can be derived.
In order to better understand the conversion of coordinate space, we will use the above two sets of formulas to implement its own LPTODP. Our function will only fit the platform of the NT class. (9X without world coordinate space, it will be simpler) Source code 2. The process of achieving myLptodP is relatively simple, and only a few main functions to be used here.
INT getGraphicsMode
HDC HDC // Handle to Device Context);
This function is used to get a graphics mode of the specified DC. Graphics mode has two GM_COMPATIBLE and GM_ADVANCED
Only world coordinate space can only be used in GM_ADVANCED. You can swap between the two in the two in SETGRAPHICSMODE.
Bool getWorldtransform
HDC HDC, // Handle to Device Context
LPXFORM LPXFORM / / TRANSFORMATION
);
This function is used to get the Affine matrix associated with the current DC. Through formulas, you should be able to know the default Affine matrix has the form of {1.0,0,0,1.0,0,0}.
Although MYLPTODP has a return value but does not have any errors in the process of implementation.
See the source code 2.
Second, about GDI supplementary instructions
The update of GDI is more embodied in the operation method and in essence. Understand the above concept, then watching the coordinate space of GDI , there will be a feeling of feeling. This article is more focused on the concept of the concept, so it is not particularly illustrated for GDI .
Note 1: Here is only one possible situation, and the specific conversion value is determined by the current coordinate space.
Note: I personally think that GDI's coordinate space is actually coordinate system, but since the English term is Coordinate Space, most books are translated as coordinate space, so it is like it.
Note 3: Everyone knows that by multiplying a 2X2 matrix can complete operations such as zoom, rotation, mirroring.
As shown below:
These transformations are called Linear Transformations. But by multiplying a matrix of 2x2 you cannot complete a single operation. In order to achieve a translation, a set of offsets (corresponding to the X-axis and Y axes, respectively). A 2x2 matrix and a set of offsets constitute an Affine matrix.
Note 4: Please refer to Feng Yuan's "Windows Graphic Programming"