The QWMatrix class specifies 2D transformations of a coordinate system.
.PP
The standard coordinate system of a paint device has the origin located at the top-left position. X values increase to the right; Y values increase downward.
.PP
This coordinate system is the default for the QPainter, which renders graphics in a paint device. A user-defined coordinate system can be specified by setting a QWMatrix for the painter.
.PP
Example:
.PP
.nf
.br
MyWidget::paintEvent( QPaintEvent * )
.br
{
.br
QPainter p; // our painter
.br
QWMatrix m; // our transformation matrix
.br
m.rotate( 22.5 ); // rotated coordinate system
.br
p.begin( this ); // start painting
.br
p.setWorldMatrix( m ); // use rotated coordinate system
.br
p.drawText( 30,20, "detator" ); // draw rotated text at 30,20
.br
p.end(); // painting done
.br
}
.br
.fi
.PP
A matrix specifies how to translate, scale, shear or rotate the graphics; the actual transformation is performed by the drawing routines in QPainter and by QPixmap::xForm().
.PP
The QWMatrix class contains a 3x3 matrix of the form:
.nf
.TS
l
-
l.
m11 m12 0
m21 m22 0
dx dy 1
.TE
.fi
.PP
A matrix transforms a point in the plane to another point:
.PP
.nf
.br
x' = m11*x + m21*y + dx
.br
y' = m22*y + m12*x + dy
.br
.fi
.PP
The point \fI(x, y)\fR is the original point, and \fI(x', y')\fR is the transformed point. \fI(x', y')\fR can be transformed back to \fI(x, y)\fR by performing the same operation on the inverted matrix.
.PP
The elements \fIdx\fR and \fIdy\fR specify horizontal and vertical translation. The elements \fIm11\fR and \fIm22\fR specify horizontal and vertical scaling. The elements \fIm12\fR and \fIm21\fR specify horizontal and vertical shearing.
.PP
The identity matrix has \fIm11\fR and \fIm22\fR set to 1; all others are set to 0. This matrix maps a point to itself.
.PP
Translation is the simplest transformation. Setting \fIdx\fR and \fIdy\fR will move the coordinate system \fIdx\fR units along the X axis and \fIdy\fR units along the Y axis.
.PP
Scaling can be done by setting \fIm11\fR and \fIm22\fR. For example, setting \fIm11\fR to 2 and \fIm22\fR to 1.5 will double the height and increase the width by 50%.
.PP
Shearing is controlled by \fIm12\fR and \fIm21\fR. Setting these elements to values different from zero will twist the coordinate system.
.PP
Rotation is achieved by carefully setting both the shearing factors and the scaling factors. The QWMatrix also has a function that sets rotation directly.
.PP
QWMatrix lets you combine transformations like this:
.PP
.nf
.br
QWMatrix m; // identity matrix
.br
m.translate(10, -20); // first translate (10,-20)
.br
m.rotate(25); // then rotate 25 degrees
.br
m.scale(1.2, 0.7); // finally scale it
.br
.fi
.PP
Here's the same example using basic matrix operations:
QPainter has functions to translate, scale, shear and rotate the coordinate system without using a QWMatrix. Although these functions are very convenient, it can be more efficient to build a QWMatrix and call QPainter::setWorldMatrix() if you want to perform more than a single transform operation.
.PP
See also QPainter::setWorldMatrix(), QPixmap::xForm(), Graphics Classes, and Image Processing Classes.
.SS "Member Type Documentation"
.SH "QWMatrix::TransformationMode"
.PP
QWMatrix offers two transformation modes. Calculations can either be done in terms of points (Points mode, the default), or in terms of area (Area mode).
.PP
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.
.TP
\fCQWMatrix::Points\fR - transformations are applied to the shape's points.
.TP
\fCQWMatrix::Areas\fR - transformations are applied (e.g. to the width and height) so that the area is transformed.
.PP
Example:
.PP
Suppose we have a rectangle, \fCQRect( 10, 20, 30, 40 )\fR and a transformation matrix \fCQWMatrix( 2, 0, 0, 2, 0, 0 )\fR to double the rectangle's size.
.PP
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.
.PP
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.
.PP
Because integer arithmetic is used (for speed), rounding 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.
.PP
Under Points mode, \fCmatrix * ( region1 | region2 )\fR is not equal to \fCmatrix * region1 | matrix * region2\fR. Under Area mode, \fCmatrix * (pointarray[i])\fR is not neccesarily equal to \fC(matrix * pointarry)[i]\fR.
.PP
<center>
.ce 1
.B "[Image Omitted]"
.PP
</center>
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QWMatrix::QWMatrix ()"
Constructs an identity matrix. All elements are set to zero except \fIm11\fR and \fIm22\fR (scaling), which are set to 1.
If the matrix is singular (not invertible), the identity matrix is returned.
.PP
If \fIinvertible\fR is not 0: the value of \fI*invertible\fR is set to TRUE if the matrix is invertible; otherwise \fI*invertible\fR is set to FALSE.
.PP
See also isInvertible().
.PP
Example: t14/cannon.cpp.
.SH "bool QWMatrix::isIdentity () const"
Returns TRUE if the matrix is the identity matrix; otherwise returns FALSE.
.PP
See also reset().
.SH "bool QWMatrix::isInvertible () const"
Returns TRUE if the matrix is invertible; otherwise returns FALSE.
.PP
See also invert().
.SH "double QWMatrix::m11 () const"
Returns the X scaling factor.
.SH "double QWMatrix::m12 () const"
Returns the vertical shearing factor.
.SH "double QWMatrix::m21 () const"
Returns the horizontal shearing factor.
.SH "double QWMatrix::m22 () const"
Returns the Y scaling factor.
.SH "void QWMatrix::map ( int x, int y, int * tx, int * ty ) const"
Transforms ( \fIx\fR, \fIy\fR ) to ( \fI*tx\fR, \fI*ty\fR ) using the formulae:
.PP
.nf
.br
*tx = m11*x + m21*y + dx (rounded to the nearest integer)
.br
*ty = m22*y + m12*x + dy (rounded to the nearest integer)
.br
.fi
.PP
Examples:
.)l t14/cannon.cpp and xform/xform.cpp.
.SH "void QWMatrix::map ( double x, double y, double * tx, double * ty ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Transforms ( \fIx\fR, \fIy\fR ) to ( \fI*tx\fR, \fI*ty\fR ) using the following formulae:
.PP
.nf
.br
*tx = m11*x + m21*y + dx
.br
*ty = m22*y + m12*x + dy
.br
.fi
.SH "QPoint QWMatrix::map ( const QPoint & p ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Transforms \fIp\fR to using the formulae:
.PP
.nf
.br
retx = m11*px + m21*py + dx (rounded to the nearest integer)
.br
rety = m22*py + m12*px + dy (rounded to the nearest integer)
.br
.fi
.SH "QRect QWMatrix::map ( const QRect & r ) const"
\fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code.
.PP
Please use QWMatrix::mapRect() instead.
.PP
Note that this method does return the bounding rectangle of the \fIr\fR, when shearing or rotations are used.
.SH "QPointArray QWMatrix::map ( const QPointArray & a ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns the point array \fIa\fR transformed by calling map for each point.
.SH "QRegion QWMatrix::map ( const QRegion & r ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Transforms the region \fIr\fR.
.PP
Calling this method can be rather expensive, if rotations or shearing are used.
.SH "QRect QWMatrix::mapRect ( const QRect & rect ) const"
Returns the transformed rectangle \fIrect\fR.
.PP
The bounding rectangle is returned if rotation or shearing has been specified.
.PP
If you need to know the exact region \fIrect\fR maps to use operator*().
.PP
See also operator*().
.PP
Example: xform/xform.cpp.
.SH "QPointArray QWMatrix::mapToPolygon ( const QRect & rect ) const"
Returns the transformed rectangle \fIrect\fR as a polygon.
.PP
Polygons and rectangles behave slightly differently when transformed (due to integer rounding), so \fCmatrix.map( QPointArray( rect ) )\fR is not always the same as \fCmatrix.mapToPolygon( rect )\fR.
.SH "QRegion QWMatrix::mapToRegion ( const QRect & rect ) const"
Returns the transformed rectangle \fIrect\fR.
.PP
A rectangle which has been rotated or sheared may result in a non-rectangular region being returned.
.PP
Calling this method can be expensive, if rotations or shearing are used. If you just need to know the bounding rectangle of the returned region, use mapRect() which is a lot faster than this function.
.PP
See also QWMatrix::mapRect().
.SH "bool QWMatrix::operator!= ( const QWMatrix & m ) const"
Returns TRUE if this matrix is not equal to \fIm\fR; otherwise returns FALSE.
.SH "QWMatrix & QWMatrix::operator*= ( const QWMatrix & m )"
Returns the result of multiplying this matrix by matrix \fIm\fR.
.SH "bool QWMatrix::operator== ( const QWMatrix & m ) const"
Returns TRUE if this matrix is equal to \fIm\fR; otherwise returns FALSE.
.SH "void QWMatrix::reset ()"
Resets the matrix to an identity matrix.
.PP
All elements are set to zero, except \fIm11\fR and \fIm22\fR (scaling) which are set to 1.
.PP
See also isIdentity().
.SH "QWMatrix & QWMatrix::rotate ( double a )"
Rotates the coordinate system \fIa\fR degrees counterclockwise.
.PP
Returns a reference to the matrix.
.PP
See also translate(), scale(), and shear().
.PP
Examples:
.)l canvas/canvas.cpp, desktop/desktop.cpp, drawdemo/drawdemo.cpp, t14/cannon.cpp, and xform/xform.cpp.
.SH "QWMatrix & QWMatrix::scale ( double sx, double sy )"
Scales the coordinate system unit by \fIsx\fR horizontally and \fIsy\fR vertically.
.PP
Returns a reference to the matrix.
.PP
See also translate(), shear(), and rotate().
.PP
Examples:
.)l canvas/canvas.cpp, fileiconview/qfileiconview.cpp, movies/main.cpp, qmag/qmag.cpp, showimg/showimg.cpp, and xform/xform.cpp.