1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdatastream.h"
46 #include "qpainterpath.h"
56 \brief The QMatrix class specifies 2D transformations of a
62 A matrix specifies how to translate, scale, shear or rotate the
63 coordinate system, and is typically used when rendering graphics.
64 QMatrix, in contrast to QTransform, does not allow perspective
65 transformations. QTransform is the recommended transformation
68 A QMatrix object can be built using the setMatrix(), scale(),
69 rotate(), translate() and shear() functions. Alternatively, it
70 can be built by applying \l {QMatrix#Basic Matrix
71 Operations}{basic matrix operations}. The matrix can also be
72 defined when constructed, and it can be reset to the identity
73 matrix (the default) using the reset() function.
75 The QMatrix class supports mapping of graphic primitives: A given
76 point, line, polygon, region, or painter path can be mapped to the
77 coordinate system defined by \e this matrix using the map()
78 function. In case of a rectangle, its coordinates can be
79 transformed using the mapRect() function. A rectangle can also be
80 transformed into a \e polygon (mapped to the coordinate system
81 defined by \e this matrix), using the mapToPolygon() function.
83 QMatrix provides the isIdentity() function which returns true if
84 the matrix is the identity matrix, and the isInvertible() function
85 which returns true if the matrix is non-singular (i.e. AB = BA =
86 I). The inverted() function returns an inverted copy of \e this
87 matrix if it is invertible (otherwise it returns the identity
88 matrix). In addition, QMatrix provides the determinant() function
89 returning the matrix's determinant.
91 Finally, the QMatrix class supports matrix multiplication, and
92 objects of the class can be streamed as well as compared.
96 \section1 Rendering Graphics
98 When rendering graphics, the matrix defines the transformations
99 but the actual transformation is performed by the drawing routines
102 By default, QPainter operates on the associated device's own
103 coordinate system. The standard coordinate system of a
104 QPaintDevice has its origin located at the top-left position. The
105 \e x values increase to the right; \e y values increase
106 downward. For a complete description, see the \l {Coordinate
107 System}{coordinate system} documentation.
109 QPainter has functions to translate, scale, shear and rotate the
110 coordinate system without using a QMatrix. For example:
114 \o \inlineimage qmatrix-simpletransformation.png
116 \snippet doc/src/snippets/matrix/matrix.cpp 0
119 Although these functions are very convenient, it can be more
120 efficient to build a QMatrix and call QPainter::setMatrix() if you
121 want to perform more than a single transform operation. For
126 \o \inlineimage qmatrix-combinedtransformation.png
128 \snippet doc/src/snippets/matrix/matrix.cpp 1
131 \section1 Basic Matrix Operations
133 \image qmatrix-representation.png
135 A QMatrix object contains a 3 x 3 matrix. The \c dx and \c dy
136 elements specify horizontal and vertical translation. The \c m11
137 and \c m22 elements specify horizontal and vertical scaling. And
138 finally, the \c m21 and \c m12 elements specify horizontal and
139 vertical \e shearing.
141 QMatrix transforms a point in the plane to another point using the
144 \snippet doc/src/snippets/code/src_gui_painting_qmatrix.cpp 0
146 The point \e (x, y) is the original point, and \e (x', y') is the
147 transformed point. \e (x', y') can be transformed back to \e (x,
148 y) by performing the same operation on the inverted() matrix.
150 The various matrix elements can be set when constructing the
151 matrix, or by using the setMatrix() function later on. They can also
152 be manipulated using the translate(), rotate(), scale() and
153 shear() convenience functions, The currently set values can be
154 retrieved using the m11(), m12(), m21(), m22(), dx() and dy()
157 Translation is the simplest transformation. Setting \c dx and \c
158 dy will move the coordinate system \c dx units along the X axis
159 and \c dy units along the Y axis. Scaling can be done by setting
160 \c m11 and \c m22. For example, setting \c m11 to 2 and \c m22 to
161 1.5 will double the height and increase the width by 50%. The
162 identity matrix has \c m11 and \c m22 set to 1 (all others are set
163 to 0) mapping a point to itself. Shearing is controlled by \c m12
164 and \c m21. Setting these elements to values different from zero
165 will twist the coordinate system. Rotation is achieved by
166 carefully setting both the shearing factors and the scaling
169 Here's the combined transformations example using basic matrix
174 \o \inlineimage qmatrix-combinedtransformation.png
176 \snippet doc/src/snippets/matrix/matrix.cpp 2
179 \sa QPainter, QTransform, {Coordinate System},
180 {painting/affine}{Affine Transformations Example}, {Transformations Example}
184 // some defines to inline some code
185 #define MAPDOUBLE(x, y, nx, ny) \
189 nx = _m11*fx + _m21*fy + _dx; \
190 ny = _m12*fx + _m22*fy + _dy; \
193 #define MAPINT(x, y, nx, ny) \
197 nx = qRound(_m11*fx + _m21*fy + _dx); \
198 ny = qRound(_m12*fx + _m22*fy + _dy); \
201 /*****************************************************************************
202 QMatrix member functions
203 *****************************************************************************/
205 \fn QMatrix::QMatrix(Qt::Initialization)
210 Constructs an identity matrix.
212 All elements are set to zero except \c m11 and \c m22 (specifying
213 the scale), which are set to 1.
229 Constructs a matrix with the elements, \a m11, \a m12, \a m21, \a
230 m22, \a dx and \a dy.
235 QMatrix::QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
247 Constructs a matrix that is a copy of the given \a matrix.
249 QMatrix::QMatrix(const QMatrix &matrix)
260 Sets the matrix elements to the specified values, \a m11, \a m12,
261 \a m21, \a m22, \a dx and \a dy.
263 Note that this function replaces the previous values. QMatrix
264 provide the translate(), rotate(), scale() and shear() convenience
265 functions to manipulate the various matrix elements based on the
266 currently defined coordinate system.
271 void QMatrix::setMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
283 \fn qreal QMatrix::m11() const
285 Returns the horizontal scaling factor.
287 \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix
292 \fn qreal QMatrix::m12() const
294 Returns the vertical shearing factor.
296 \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix
301 \fn qreal QMatrix::m21() const
303 Returns the horizontal shearing factor.
305 \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix
310 \fn qreal QMatrix::m22() const
312 Returns the vertical scaling factor.
314 \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix
319 \fn qreal QMatrix::dx() const
321 Returns the horizontal translation factor.
323 \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix
328 \fn qreal QMatrix::dy() const
330 Returns the vertical translation factor.
332 \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix
338 Maps the given coordinates \a x and \a y into the coordinate
339 system defined by this matrix. The resulting values are put in *\a
340 tx and *\a ty, respectively.
342 The coordinates are transformed using the following formulas:
344 \snippet doc/src/snippets/code/src_gui_painting_qmatrix.cpp 1
346 The point (x, y) is the original point, and (x', y') is the
349 \sa {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}
352 void QMatrix::map(qreal x, qreal y, qreal *tx, qreal *ty) const
354 MAPDOUBLE(x, y, *tx, *ty);
362 Maps the given coordinates \a x and \a y into the coordinate
363 system defined by this matrix. The resulting values are put in *\a
364 tx and *\a ty, respectively. Note that the transformed coordinates
365 are rounded to the nearest integer.
368 void QMatrix::map(int x, int y, int *tx, int *ty) const
370 MAPINT(x, y, *tx, *ty);
373 QRect QMatrix::mapRect(const QRect &rect) const
376 if (_m12 == 0.0F && _m21 == 0.0F) {
377 int x = qRound(_m11*rect.x() + _dx);
378 int y = qRound(_m22*rect.y() + _dy);
379 int w = qRound(_m11*rect.width());
380 int h = qRound(_m22*rect.height());
389 result = QRect(x, y, w, h);
391 // see mapToPolygon for explanations of the algorithm.
394 MAPDOUBLE(rect.left(), rect.top(), x0, y0);
399 MAPDOUBLE(rect.right() + 1, rect.top(), x, y);
400 xmin = qMin(xmin, x);
401 ymin = qMin(ymin, y);
402 xmax = qMax(xmax, x);
403 ymax = qMax(ymax, y);
404 MAPDOUBLE(rect.right() + 1, rect.bottom() + 1, x, y);
405 xmin = qMin(xmin, x);
406 ymin = qMin(ymin, y);
407 xmax = qMax(xmax, x);
408 ymax = qMax(ymax, y);
409 MAPDOUBLE(rect.left(), rect.bottom() + 1, x, y);
410 xmin = qMin(xmin, x);
411 ymin = qMin(ymin, y);
412 xmax = qMax(xmax, x);
413 ymax = qMax(ymax, y);
414 result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin), qRound(ymax)-qRound(ymin));
420 \fn QRectF QMatrix::mapRect(const QRectF &rectangle) const
422 Creates and returns a QRectF object that is a copy of the given \a
423 rectangle, mapped into the coordinate system defined by this
426 The rectangle's coordinates are transformed using the following
429 \snippet doc/src/snippets/code/src_gui_painting_qmatrix.cpp 2
431 If rotation or shearing has been specified, this function returns
432 the \e bounding rectangle. To retrieve the exact region the given
433 \a rectangle maps to, use the mapToPolygon() function instead.
435 \sa mapToPolygon(), {QMatrix#Basic Matrix Operations}{Basic Matrix
438 QRectF QMatrix::mapRect(const QRectF &rect) const
441 if (_m12 == 0.0F && _m21 == 0.0F) {
442 qreal x = _m11*rect.x() + _dx;
443 qreal y = _m22*rect.y() + _dy;
444 qreal w = _m11*rect.width();
445 qreal h = _m22*rect.height();
454 result = QRectF(x, y, w, h);
458 MAPDOUBLE(rect.x(), rect.y(), x0, y0);
463 MAPDOUBLE(rect.x() + rect.width(), rect.y(), x, y);
464 xmin = qMin(xmin, x);
465 ymin = qMin(ymin, y);
466 xmax = qMax(xmax, x);
467 ymax = qMax(ymax, y);
468 MAPDOUBLE(rect.x() + rect.width(), rect.y() + rect.height(), x, y);
469 xmin = qMin(xmin, x);
470 ymin = qMin(ymin, y);
471 xmax = qMax(xmax, x);
472 ymax = qMax(ymax, y);
473 MAPDOUBLE(rect.x(), rect.y() + rect.height(), x, y);
474 xmin = qMin(xmin, x);
475 ymin = qMin(ymin, y);
476 xmax = qMax(xmax, x);
477 ymax = qMax(ymax, y);
478 result = QRectF(xmin, ymin, xmax-xmin, ymax - ymin);
484 \fn QRect QMatrix::mapRect(const QRect &rectangle) const
487 Creates and returns a QRect object that is a copy of the given \a
488 rectangle, mapped into the coordinate system defined by this
489 matrix. Note that the transformed coordinates are rounded to the
495 \fn QPoint operator*(const QPoint &point, const QMatrix &matrix)
498 This is the same as \a{matrix}.map(\a{point}).
503 QPoint QMatrix::map(const QPoint &p) const
507 return QPoint(qRound(_m11*fx + _m21*fy + _dx),
508 qRound(_m12*fx + _m22*fy + _dy));
512 \fn QPointF operator*(const QPointF &point, const QMatrix &matrix)
515 Same as \a{matrix}.map(\a{point}).
523 Creates and returns a QPointF object that is a copy of the given
524 \a point, mapped into the coordinate system defined by this
527 QPointF QMatrix::map(const QPointF &point) const
529 qreal fx = point.x();
530 qreal fy = point.y();
531 return QPointF(_m11*fx + _m21*fy + _dx, _m12*fx + _m22*fy + _dy);
535 \fn QPoint QMatrix::map(const QPoint &point) const
538 Creates and returns a QPoint object that is a copy of the given \a
539 point, mapped into the coordinate system defined by this
540 matrix. Note that the transformed coordinates are rounded to the
545 \fn QLineF operator*(const QLineF &line, const QMatrix &matrix)
548 This is the same as \a{matrix}.map(\a{line}).
554 \fn QLine operator*(const QLine &line, const QMatrix &matrix)
557 This is the same as \a{matrix}.map(\a{line}).
565 Creates and returns a QLineF object that is a copy of the given \a
566 line, mapped into the coordinate system defined by this matrix.
568 QLineF QMatrix::map(const QLineF &line) const
570 return QLineF(map(line.p1()), map(line.p2()));
576 Creates and returns a QLine object that is a copy of the given \a
577 line, mapped into the coordinate system defined by this matrix.
578 Note that the transformed coordinates are rounded to the nearest
581 QLine QMatrix::map(const QLine &line) const
583 return QLine(map(line.p1()), map(line.p2()));
587 \fn QPolygonF operator *(const QPolygonF &polygon, const QMatrix &matrix)
590 This is the same as \a{matrix}.map(\a{polygon}).
596 \fn QPolygon operator*(const QPolygon &polygon, const QMatrix &matrix)
599 This is the same as \a{matrix}.map(\a{polygon}).
604 QPolygon QMatrix::map(const QPolygon &a) const
609 const QPoint *da = a.constData();
610 QPoint *dp = p.data();
611 for(i = 0; i < size; i++) {
612 MAPINT(da[i].x(), da[i].y(), dp[i].rx(), dp[i].ry());
618 \fn QPolygonF QMatrix::map(const QPolygonF &polygon) const
621 Creates and returns a QPolygonF object that is a copy of the given
622 \a polygon, mapped into the coordinate system defined by this
625 QPolygonF QMatrix::map(const QPolygonF &a) const
630 const QPointF *da = a.constData();
631 QPointF *dp = p.data();
632 for(i = 0; i < size; i++) {
633 MAPDOUBLE(da[i].xp, da[i].yp, dp[i].xp, dp[i].yp);
639 \fn QPolygon QMatrix::map(const QPolygon &polygon) const
642 Creates and returns a QPolygon object that is a copy of the given
643 \a polygon, mapped into the coordinate system defined by this
644 matrix. Note that the transformed coordinates are rounded to the
649 \fn QRegion operator*(const QRegion ®ion, const QMatrix &matrix)
652 This is the same as \a{matrix}.map(\a{region}).
657 extern QPainterPath qt_regionToPath(const QRegion ®ion);
660 \fn QRegion QMatrix::map(const QRegion ®ion) const
663 Creates and returns a QRegion object that is a copy of the given
664 \a region, mapped into the coordinate system defined by this matrix.
666 Calling this method can be rather expensive if rotations or
669 QRegion QMatrix::map(const QRegion &r) const
671 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) { // translate or identity
672 if (_dx == 0.0 && _dy == 0.0) // Identity
675 copy.translate(qRound(_dx), qRound(_dy));
679 QPainterPath p = map(qt_regionToPath(r));
680 return p.toFillPolygon().toPolygon();
684 \fn QPainterPath operator *(const QPainterPath &path, const QMatrix &matrix)
687 This is the same as \a{matrix}.map(\a{path}).
695 Creates and returns a QPainterPath object that is a copy of the
696 given \a path, mapped into the coordinate system defined by this
699 QPainterPath QMatrix::map(const QPainterPath &path) const
702 return QPainterPath();
704 QPainterPath copy = path;
706 // Translate or identity
707 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {
710 if (_dx != 0.0 || _dy != 0.0) {
712 for (int i=0; i<path.elementCount(); ++i) {
713 QPainterPath::Element &e = copy.d_ptr->elements[i];
722 for (int i=0; i<path.elementCount(); ++i) {
723 QPainterPath::Element &e = copy.d_ptr->elements[i];
724 qreal fx = e.x, fy = e.y;
725 e.x = _m11*fx + _m21*fy + _dx;
726 e.y = _m12*fx + _m22*fy + _dy;
734 \fn QRegion QMatrix::mapToRegion(const QRect &rectangle) const
736 Returns the transformed rectangle \a rectangle as a QRegion
737 object. A rectangle which has been rotated or sheared may result
738 in a non-rectangular region being returned.
740 Use the mapToPolygon() or map() function instead.
743 QRegion QMatrix::mapToRegion(const QRect &rect) const
748 } else if (m12() == 0.0F && m21() == 0.0F) {
749 int x = qRound(m11()*rect.x() + dx());
750 int y = qRound(m22()*rect.y() + dy());
751 int w = qRound(m11()*rect.width());
752 int h = qRound(m22()*rect.height());
761 result = QRect(x, y, w, h);
763 result = QRegion(mapToPolygon(rect));
770 \fn QPolygon QMatrix::mapToPolygon(const QRect &rectangle) const
772 Creates and returns a QPolygon representation of the given \a
773 rectangle, mapped into the coordinate system defined by this
776 The rectangle's coordinates are transformed using the following
779 \snippet doc/src/snippets/code/src_gui_painting_qmatrix.cpp 3
781 Polygons and rectangles behave slightly differently when
782 transformed (due to integer rounding), so
783 \c{matrix.map(QPolygon(rectangle))} is not always the same as
784 \c{matrix.mapToPolygon(rectangle)}.
786 \sa mapRect(), {QMatrix#Basic Matrix Operations}{Basic Matrix
789 QPolygon QMatrix::mapToPolygon(const QRect &rect) const
793 if (_m12 == 0.0F && _m21 == 0.0F) {
794 x[0] = _m11*rect.x() + _dx;
795 y[0] = _m22*rect.y() + _dy;
796 qreal w = _m11*rect.width();
797 qreal h = _m22*rect.height();
813 qreal right = rect.x() + rect.width();
814 qreal bottom = rect.y() + rect.height();
815 MAPDOUBLE(rect.x(), rect.y(), x[0], y[0]);
816 MAPDOUBLE(right, rect.y(), x[1], y[1]);
817 MAPDOUBLE(right, bottom, x[2], y[2]);
818 MAPDOUBLE(rect.x(), bottom, x[3], y[3]);
822 for(i = 0; i< 4; i++)
823 qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i]));
824 qDebug("width=%f, height=%f", qSqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0])),
825 qSqrt((x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3])));
827 // all coordinates are correctly, tranform to a pointarray
828 // (rounding to the next integer)
829 a.setPoints(4, qRound(x[0]), qRound(y[0]),
830 qRound(x[1]), qRound(y[1]),
831 qRound(x[2]), qRound(y[2]),
832 qRound(x[3]), qRound(y[3]));
837 Resets the matrix to an identity matrix, i.e. all elements are set
838 to zero, except \c m11 and \c m22 (specifying the scale) which are
841 \sa QMatrix(), isIdentity(), {QMatrix#Basic Matrix
842 Operations}{Basic Matrix Operations}
845 void QMatrix::reset()
848 _m12 = _m21 = _dx = _dy = 0.0;
852 \fn bool QMatrix::isIdentity() const
854 Returns true if the matrix is the identity matrix, otherwise
861 Moves the coordinate system \a dx along the x axis and \a dy along
862 the y axis, and returns a reference to the matrix.
867 QMatrix &QMatrix::translate(qreal dx, qreal dy)
869 _dx += dx*_m11 + dy*_m21;
870 _dy += dy*_m22 + dx*_m12;
875 \fn QMatrix &QMatrix::scale(qreal sx, qreal sy)
877 Scales the coordinate system by \a sx horizontally and \a sy
878 vertically, and returns a reference to the matrix.
883 QMatrix &QMatrix::scale(qreal sx, qreal sy)
893 Shears the coordinate system by \a sh horizontally and \a sv
894 vertically, and returns a reference to the matrix.
899 QMatrix &QMatrix::shear(qreal sh, qreal sv)
901 qreal tm11 = sv*_m21;
902 qreal tm12 = sv*_m22;
903 qreal tm21 = sh*_m11;
904 qreal tm22 = sh*_m12;
912 const qreal deg2rad = qreal(0.017453292519943295769); // pi/180
915 \fn QMatrix &QMatrix::rotate(qreal degrees)
917 Rotates the coordinate system the given \a degrees
920 Note that if you apply a QMatrix to a point defined in widget
921 coordinates, the direction of the rotation will be clockwise
922 because the y-axis points downwards.
924 Returns a reference to the matrix.
929 QMatrix &QMatrix::rotate(qreal a)
933 if (a == 90. || a == -270.)
935 else if (a == 270. || a == -90.)
940 qreal b = deg2rad*a; // convert to radians
941 sina = qSin(b); // fast and convenient
944 qreal tm11 = cosa*_m11 + sina*_m21;
945 qreal tm12 = cosa*_m12 + sina*_m22;
946 qreal tm21 = -sina*_m11 + cosa*_m21;
947 qreal tm22 = -sina*_m12 + cosa*_m22;
948 _m11 = tm11; _m12 = tm12;
949 _m21 = tm21; _m22 = tm22;
954 \fn bool QMatrix::isInvertible() const
956 Returns true if the matrix is invertible, otherwise returns false.
963 \fn qreal QMatrix::det() const
965 Returns the matrix's determinant.
972 \fn qreal QMatrix::determinant() const
974 Returns the matrix's determinant.
978 \fn QMatrix QMatrix::invert(bool *invertible) const
980 Returns an inverted copy of this matrix.
982 Use the inverted() function instead.
986 Returns an inverted copy of this matrix.
988 If the matrix is singular (not invertible), the returned matrix is
989 the identity matrix. If \a invertible is valid (i.e. not 0), its
990 value is set to true if the matrix is invertible, otherwise it is
996 QMatrix QMatrix::inverted(bool *invertible) const
998 qreal dtr = determinant();
1001 *invertible = false; // singular matrix
1002 return QMatrix(true);
1004 else { // invertible matrix
1007 qreal dinv = 1.0/dtr;
1008 return QMatrix((_m22*dinv), (-_m12*dinv),
1009 (-_m21*dinv), (_m11*dinv),
1010 ((_m21*_dy - _m22*_dx)*dinv),
1011 ((_m12*_dx - _m11*_dy)*dinv),
1018 \fn bool QMatrix::operator==(const QMatrix &matrix) const
1020 Returns true if this matrix is equal to the given \a matrix,
1021 otherwise returns false.
1024 bool QMatrix::operator==(const QMatrix &m) const
1026 return _m11 == m._m11 &&
1035 \fn bool QMatrix::operator!=(const QMatrix &matrix) const
1037 Returns true if this matrix is not equal to the given \a matrix,
1038 otherwise returns false.
1041 bool QMatrix::operator!=(const QMatrix &m) const
1043 return _m11 != m._m11 ||
1052 \fn QMatrix &QMatrix::operator *=(const QMatrix &matrix)
1055 Returns the result of multiplying this matrix by the given \a
1059 QMatrix &QMatrix::operator *=(const QMatrix &m)
1061 qreal tm11 = _m11*m._m11 + _m12*m._m21;
1062 qreal tm12 = _m11*m._m12 + _m12*m._m22;
1063 qreal tm21 = _m21*m._m11 + _m22*m._m21;
1064 qreal tm22 = _m21*m._m12 + _m22*m._m22;
1066 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1067 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1069 _m11 = tm11; _m12 = tm12;
1070 _m21 = tm21; _m22 = tm22;
1071 _dx = tdx; _dy = tdy;
1076 \fn QMatrix QMatrix::operator *(const QMatrix &matrix) const
1078 Returns the result of multiplying this matrix by the given \a
1081 Note that matrix multiplication is not commutative, i.e. a*b !=
1085 QMatrix QMatrix::operator *(const QMatrix &m) const
1087 qreal tm11 = _m11*m._m11 + _m12*m._m21;
1088 qreal tm12 = _m11*m._m12 + _m12*m._m22;
1089 qreal tm21 = _m21*m._m11 + _m22*m._m21;
1090 qreal tm22 = _m21*m._m12 + _m22*m._m22;
1092 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1093 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1094 return QMatrix(tm11, tm12, tm21, tm22, tdx, tdy, true);
1098 Assigns the given \a matrix's values to this matrix.
1100 QMatrix &QMatrix::operator=(const QMatrix &matrix)
1114 Returns the matrix as a QVariant.
1116 QMatrix::operator QVariant() const
1118 return QVariant(QVariant::Matrix, this);
1121 Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m)
1127 /*****************************************************************************
1128 QMatrix stream functions
1129 *****************************************************************************/
1130 #ifndef QT_NO_DATASTREAM
1132 \fn QDataStream &operator<<(QDataStream &stream, const QMatrix &matrix)
1135 Writes the given \a matrix to the given \a stream and returns a
1136 reference to the stream.
1138 \sa {Serializing Qt Data Types}
1141 QDataStream &operator<<(QDataStream &s, const QMatrix &m)
1143 if (s.version() == 1) {
1144 s << (float)m.m11() << (float)m.m12() << (float)m.m21()
1145 << (float)m.m22() << (float)m.dx() << (float)m.dy();
1147 s << double(m.m11())
1158 \fn QDataStream &operator>>(QDataStream &stream, QMatrix &matrix)
1161 Reads the given \a matrix from the given \a stream and returns a
1162 reference to the stream.
1164 \sa {Serializing Qt Data Types}
1167 QDataStream &operator>>(QDataStream &s, QMatrix &m)
1169 if (s.version() == 1) {
1170 float m11, m12, m21, m22, dx, dy;
1171 s >> m11; s >> m12; s >> m21; s >> m22;
1173 m.setMatrix(m11, m12, m21, m22, dx, dy);
1176 double m11, m12, m21, m22, dx, dy;
1183 m.setMatrix(m11, m12, m21, m22, dx, dy);
1187 #endif // QT_NO_DATASTREAM
1189 #ifndef QT_NO_DEBUG_STREAM
1190 QDebug operator<<(QDebug dbg, const QMatrix &m)
1192 dbg.nospace() << "QMatrix("
1194 << " 12=" << m.m12()
1195 << " 21=" << m.m21()
1196 << " 22=" << m.m22()
1205 \fn QRect QMatrix::map(const QRect &rect) const
1208 Creates and returns a QRect object that is a copy of the given
1209 rectangle, mapped into the coordinate system defined by this
1212 Use the mapRect() function instead.
1217 \fn bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
1222 \brief The qFuzzyCompare function is for comparing two matrices
1223 using a fuzziness factor.
1225 Returns true if \a m1 and \a m2 are equal, allowing for a small
1226 fuzziness factor for floating-point comparisons; false otherwise.