![]() |
Home · Overviews · Examples |
The QMatrix class specifies 2D transformations of a coordinate system. More...
The QMatrix class specifies 2D transformations of a coordinate system.
A matrix specifies how to translate, scale, shear or rotate the coordinate system, and is typically used when rendering graphics.
A QMatrix object can be built using the setMatrix, scale(), rotate(), translate() and shear() functions. Alternatively, it can be built by applying basic matrix operations. The matrix can also be defined when constructed, and it can be reset to the identity matrix (the default) using the reset function.
The QMatrix class supports mapping of graphic primitives: A given point, line, polygon, region, or painter path can be mapped to the coordinate system defined by this matrix using the map function. In case of a rectangle, its coordinates can be transformed using the mapRect function. A rectangle can also be transformed into a polygon (mapped to the coordinate system defined by this matrix), using the mapToPolygon function.
QMatrix provides the isIdentity function which returns true if the matrix is the identity matrix, and the isInvertible function which returns true if the matrix is non-singular (i.e. AB = BA = I). The inverted() function returns an inverted copy of this matrix if it is invertible (otherwise it returns the identity matrix). In addition, QMatrix provides the det function returning the matrix's determinant.
Finally, the QMatrix class supports matrix multiplication, and objects of the class can be streamed as well as compared.
When rendering graphics, the matrix defines the transformations but the actual transformation is performed by the drawing routines in QPainter.
By default, QPainter operates on the associated device's own coordinate system. The standard coordinate system of a QPaintDevice has its origin located at the top-left position. The x values increase to the right; y values increase downward. For a complete description, see the coordinate system documentation.
QPainter has functions to translate, scale, shear and rotate the coordinate system without using a QMatrix. For example:
![]() | void SimpleTransformation::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(QPen(Qt::blue, 1, Qt::DashLine)); painter.drawRect(0, 0, 100, 100); painter.rotate(45); painter.setFont(QFont("Helvetica", 24)); painter.setPen(QPen(Qt::black, 1)); painter.drawText(20, 10, "QMatrix"); } |
Although these functions are very convenient, it can be more efficient to build a QMatrix and call QPainter::setMatrix() if you want to perform more than a single transform operation. For example:
![]() | void CombinedTransformation::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(QPen(Qt::blue, 1, Qt::DashLine)); painter.drawRect(0, 0, 100, 100); QMatrix matrix; matrix.translate(50, 50); matrix.rotate(45); matrix.scale(0.5, 1.0); painter.setMatrix(matrix); painter.setFont(QFont("Helvetica", 24)); painter.setPen(QPen(Qt::black, 1)); painter.drawText(20, 10, "QMatrix"); } |
A QMatrix object contains a 3 x 3 matrix. The dx and dy elements specify horizontal and vertical translation. The m11 and m22 elements specify horizontal and vertical scaling. And finally, the m21 and m12 elements specify horizontal and vertical shearing.
QMatrix transforms a point in the plane to another point using the following formulas:
x' = m11*x + m21*y + dx y' = m22*y + m12*x + dy
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 inverted() matrix.
The various matrix elements can be set when constructing the matrix, or by using the setMatrix function later on. They also be manipulated using the translate(), rotate(), scale() and shear() convenience functions, The currently set values can be retrieved using the m11, m12, m21, m22, dx and dy functions.
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%. The identity matrix has m11 and m22 set to 1 (all others are set to 0) mapping a point to itself. 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.
Here's the combined transformations example using basic matrix operations:
![]() | void BasicOperations::paintEvent(QPaintEvent *) { double pi = 3.14; double a = pi/180 * 45.0; double sina = sin(a); double cosa = cos(a); QMatrix translationMatrix(1, 0, 0, 1, 50.0, 50.0); QMatrix rotationMatrix(cosa, sina, -sina, cosa, 0, 0); QMatrix scalingMatrix(0.5, 0, 0, 1.0, 0, 0); QMatrix matrix; matrix = scalingMatrix * rotationMatrix * translationMatrix; QPainter painter(this); painter.setPen(QPen(Qt::blue, 1, Qt::DashLine)); painter.drawRect(0, 0, 100, 100); painter.setMatrix(matrix); painter.setFont(QFont("Helvetica", 24)); painter.setPen(QPen(Qt::black, 1)); painter.drawText(20, 10, "QMatrix"); } |
See also QPainter, The Coordinate System, Affine Transformations Demo, and Transformations Example.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.5_01 |