and physical device coordinates
In this section we'll talk about a variety of ways that wxPython allows you to manage coordinates, sizing, and the like. Let's start with coordinate axis management. Ordinarily, the coordinate system of a wxPython device context starts with (0, 0) in the upper-left, with the y coordinate getting larger as you move down, and the x coordinate getting larger as you move right. This is in keeping with the standard of nearly every graphics system since the beginning of time, and of course, the y-axis is flipped from the normal way a mathematical plane is drawn. Should the inconsistency with your geometry textbook bother you, you can change the orientation with the method SetAxisOrientation(xLeftRight, yBottomUp); both of the parameters are Boolean. If xLeftRight is True, then the x-axis increases from left to right, if it's False then the axis is flipped and increases from right to left. Similarly, if yBottomUp is True then the y-axis increases as it heads up, otherwise, it increases as it heads down.
The coordinate axis is measured in pixels. However, at times you'll want to store your dimension in some more useful real-world measurement. You can manage the conversion by setting a map mode with the method SetMapMode(mode). The map mode is a conversion that the device context will use between the physical coordinates of the screen and the logical coordinates in the measurement you specify. The legal values of the mode are displayed in table 12.7.
|
Mode |
Logical Unit |
|
wx.MM_LOMETRIC |
0.1 millimeters |
|
wx.MM_METRIC |
1 millimeter |
CHAPTER 12
Manipulating basic graphical images Table 12.7 Device context mapping modes (continued)
CHAPTER 12
Manipulating basic graphical images Table 12.7 Device context mapping modes (continued)
|
Mode |
Logical Unit |
|
wx.MM_POINTS |
A point (as in the printing unit of measure—point size). Equal to 1/72nd of an inch. |
|
wx.MM_TEXT |
Default value. The unit is 1 pixel. |
|
wx.MM_TWIPS |
Printing unit, 20 to a point, or 1440 to an inch. |
The accuracy of the logical to physical mapping depends on how well your system reports the dot pitch of the monitor in order to perform the conversion. You can see the value being used for the conversion with the method GetPPl(), which returns the value of pixels per inch. In practice, I wouldn't depend on a logical inch exactly matching a real inch.
The mapping mode conversions are automatically applied to your points and sizes when you use a device context method. Sometimes you will need to perform the conversion outside the device context. To convert from your logical coordinate to the device coordinate you use the methods LogicalToDeviceX(x) and LogicalToDeviceY(y). Both of these methods take in the integer measurements in the logical coordinates, apply the mapping mode, and return the measurement in device coordinates. There are related methods which use the mapping mode, but do not care about the current orientation of the axes, in essence taking an absolute value of the coordinate. Those methods are called LogicalToDevice-XRel(x) and LogicalToDeviceYRel(y).
The inverse set of methods take a location on the device axis and convert it to the logical coordinate system. Those methods, as you might guess, are called DeviceToLogicalX(x), DeviceToLogicalY(y), DeviceToLogicalXRel(x), and Device-ToLogcialYRel(y).
There are a series of device context methods that allow some information and control over the portion of the device context that you are drawing in. Often, you want to restrict drawing updates to a particular section of the device context. This is usually done for performance reasons, especially if you know that only one portion of a large or complex graphic needs to be redrawn. This kind of redraw is called clipping, and the method for setting it is SetClippingRegion(x, y, width, height). The four parameters specify a rectangle with the upper left corner, and the dimensions. Once set, only drawing actions taking place within the clipping region will be processed. To unset a clipping region, use the method Destroy-ClippingRegion(), which completely clears the clipping region. After that method is called, drawing actions are processed throughout the device context. To read the current clipping region, use the method GetClippingBox(), which returns a tuple (x, y, width, height).
As you draw into a device context, wxPython maintains the value of the rectangle that would minimally surround all the drawing you have done to the context. This rectangle is called the bounding box, and is often useful in determining if the context needs to be refreshed. You can get the four sides of the bounding box with the methods MaxX(), MaxY(), MinX(), MinY(). These methods return the appropriate minimum or maximum value in device coordinates for the bounding box in the direction specifed. If there's a specific point on the screen that you want within the bounding box for some reason, you can add it with the method Calc-BoundingBox(x, y), which recalculates the extent of the bounding box exactly as though you had drawn something at that point. You can start the bounding box calculations all over with the method ResetBoundingBox(). After that method is called, the bounding box reverts to its default state, as if nothing had been drawn to the context.
Post a comment