#include <fgmatrix.h>
The standard coordinate system of a has the origin located at the top-left position. X values increase to the right; Y values increase downward.
This coordinate system is the default for the FGDrawBuffer, which renders graphics in a paint device. A user-defined coordinate system can be specified by setting a FGMatrix for the FGDrawBuffer.
Example:
MyWindow::OnPaint(void) { FGMatrix m; // our transformation matrix FGPoint position(30,20); m.translate( 100, 100 ); // rotated coordinate system m.scale( 3, 2); position = m.map(position); WindowText( position.x, position.y, "detator" ); // draw rotated text at 100+30*3, 100+20*2 }
A matrix specifies how to translate, scale, shear or rotate the postitions.
The FGMatrix class contains a 3x3 matrix of the form:
| m11 | m12 | 0 |
| m21 | m22 | 0 |
| dx | dy | 1 |
A matrix transforms a point in the plane to another point:
The point (x, y) is the original point, and (x', y') is the transformed point. (x', y') can be transformed back to (x, y) by performing the same operation on the FGMatrix::invert() inverted matrix.
The elements dx and dy specify horizontal and vertical translation. The elements m11 and m22 specify horizontal and vertical scaling. The elements m12 and m21 specify horizontal and vertical shearing.
The identity matrix has m11 and m22 set to 1; all others are set to 0. This matrix maps a point to itself.
Translation is the simplest transformation. Setting dx and dy will move the coordinate system dx units along the X axis and dy units along the Y axis.
Scaling can be done by setting m11 and m22. For example, setting m11 to 2 and m22 to 1.5 will double the height and increase the width by 50%.
Shearing is controlled by m12 and m21. Setting these elements to values different from zero will twist the coordinate system.
Rotation is achieved by carefully setting both the shearing factors and the scaling factors. The FGMatrix also has a function that sets rotate() directly.
FGMatrix lets you combine transformations like this:
FGMatrix m; // identity matrix m.translate(10, -20); // first translate (10,-20) m.rotate(25); // then rotate 25 degrees m.scale(1.2, 0.7); // finally scale it
Here's the same example using basic matrix operations:
double a = pi/180 * 25; // convert 25 to radians double sina = sin(a); double cosa = cos(a); FGMatrix m1(1, 0, 0, 1, 10, -20); // translation matrix FGMatrix m2( cosa, sina, // rotation matrix -sina, cosa, 0, 0 ); FGMatrix m3(1.2, 0, 0, 0.7, 0, 0); // scaling matrix FGMatrix m; m = m3 * m2 * m1; // combine all transformations
QPainter has functions to translate, scale, shear and rotate the coordinate system without using a FGMatrix. Although these functions are very convenient, it can be more efficient to build a FGMatrix and call QPainter::setWorldMatrix() if you want to perform more than a single transform operation.
Public Types | |
| enum | TransformationMode { Points, Areas } |
Public Member Functions | |
| FGMatrix () | |
| FGMatrix (const FGMatrix &old) | |
| FGMatrix (double m11, double m12, double m21, double m22, double dx, double dy) | |
| void | setMatrix (double m11, double m12, double m21, double m22, double dx, double dy) |
| double | m11 () const |
| double | m12 () const |
| double | m21 () const |
| double | m22 () const |
| double | dx () const |
| double | dy () const |
| void | map (int x, int y, int *tx, int *ty) const |
| void | map (double x, double y, double *tx, double *ty) const |
| FGRect | mapRect (const FGRect &) const |
| FGPoint | map (const FGPoint &p) const |
| FGCircle | map (const FGCircle &c) const |
| FGSize | map (const FGSize &sz) const |
| FGRect | map (const FGRect &r) const |
| FGPointArray | map (const FGPointArray &a) const |
| void | map (FGPointArray &src, FGPointArray &dst) |
| void | reset () |
| bool | isIdentity () const |
| FGMatrix & | translate (double dx, double dy) |
| FGMatrix & | scale (double sx, double sy) |
| FGMatrix & | shear (double sh, double sv) |
| FGMatrix & | rotate (double a) |
| void | shift_x (double x) |
| void | shift_y (double y) |
| void | SetDxDy (double dx, double dy) |
| void | zoom (double val) |
| FGMatrix & | FGMatrix::scale_with_center (double sx, double sy, double center_x, double center_y) |
| bool | isInvertible () const |
| double | det () const |
| FGMatrix | invert (bool *=0) const |
| bool | operator== (const FGMatrix &) const |
| bool | operator!= (const FGMatrix &) const |
| FGMatrix & | operator *= (const FGMatrix &) |
| FGCircle | operator * (const FGCircle &) const |
| FGPoint | operator * (const FGPoint &) const |
| FGSize | operator * (const FGSize &) const |
| FGPointArray | operator * (const FGPointArray &a) const |
Static Public Member Functions | |
| static void | setTransformationMode (FGMatrix::TransformationMode m) |
| static TransformationMode | transformationMode () |
Related Functions | |
| (Note that these are not member functions.) | |
| FGMatrix | operator * (const FGMatrix &m1, const FGMatrix &m2) |
|
|
FGMatrix offers two transformation modes. Calculations can either be done in terms of points (Points mode, the default), or in terms of area (Area mode). In Points mode the transformation is applied to the points that mark out the shape's bounding line. In Areas mode the transformation is applied in such a way that the area of the contained region is correctly transformed under the matrix. Points transformations are applied to the shape's points. Areas transformations are applied (e.g. to the width and height) so that the area is transformed. Example:
Suppose we have a rectangle, In Points mode, the matrix will transform the top-left (10,20) and the bottom-right (39,59) points producing a rectangle with its top-left point at (20,40) and its bottom-right point at (78,118), i.e. with a width of 59 and a height of 79. In Areas mode, the matrix will transform the top-left point in the same way as in Points mode to (20/40), and double the width and height, so the bottom-right will become (69,99), i.e. a width of 60 and a height of 80. Because integer arithmetic is used (for speed), FGrounding differences mean that the modes will produce slightly different results given the same shape and the same transformation, especially when scaling up. This also means that some operations are not commutative.
Under Points mode,
Comparison of Points and Areas TransformationModes |
|
|
Constructs an identity matrix. All elements are set to zero except m11 and m22 (scaling), which are set to 1. |
|
||||||||||||||||||||||||||||
|
Constructs a matrix with the elements, m11, m12, m21, m22, dx and dy. |
|
|
Returns the matrix's determinant. |
|
|
Returns the horizontal translation. |
|
|
Returns the vertical translation. |
|
|
Returns the inverted matrix. If the matrix is singular (not invertible), the identity matrix is returned. If invertible is not 0: the value of *invertible is set to TRUE if the matrix is invertible; otherwise *invertible is set to FALSE.
|
|
|
Returns TRUE if the matrix is the identity matrix; otherwise returns FALSE.
|
|
|
Returns TRUE if the matrix is invertible; otherwise returns FALSE.
|
|
|
Returns the X scaling factor. |
|
|
Returns the vertical shearing factor. |
|
|
Returns the horizontal shearing factor. |
|
|
Returns the Y scaling factor. |
|
|
Returns the point array a transformed by calling map for each point. |
|
|
Please use FGMatrix::mapRect() instead. Note that this method does return the bounding rectangle of the r, when shearing or rotations are used. |
|
|
Transforms p to using the formulae:
|
|
||||||||||||||||||||
|
Transforms ( x, y ) to ( *tx, *ty ) using the following formulae:
|
|
||||||||||||||||||||
|
Transforms ( x, y ) to ( *tx, *ty ) using the formulae:
|
|
|
Returns the transformed rectangle rect. The bounding rectangle is returned if rotation or shearing has been specified. If you need to know the exact region rect maps to use operator*().
|
|
|
Returns the result of multiplying this matrix by matrix m. |
|
|
Returns TRUE if this matrix is not equal to m; otherwise returns FALSE. |
|
|
Returns TRUE if this matrix is equal to m; otherwise returns FALSE. |
|
|
Resets the matrix to an identity matrix. All elements are set to zero, except m11 and m22 (scaling) which are set to 1.
|
|
|
Rotates the coordinate system a degrees counterclockwise. Returns a reference to the matrix.
|
|
||||||||||||
|
Scales the coordinate system unit by sx horizontally and sy vertically. Returns a reference to the matrix.
|
|
||||||||||||||||||||||||||||
|
Sets the matrix elements to the specified values, m11, m12, m21, m22, dx and dy. |
|
|
Sets the transformation mode that FGMatrix and painter transformations use to m.
|
|
||||||||||||
|
Shears the coordinate system by sh horizontally and sv vertically. Returns a reference to the matrix.
|
|
|
Returns the current transformation mode.
|
|
||||||||||||
|
Moves the coordinate system dx along the X-axis and dy along the Y-axis. Returns a reference to the matrix.
|
|
||||||||||||
|
Returns the product of m1 * m2. Note that matrix multiplication is not commutative, i.e. a*b != b*a. |
1.4.0