1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdatastream.h"
46 #include "qpainterpath.h"
47 #include "qpainterpath_p.h"
57 \brief The QMatrix class specifies 2D transformations of a
63 A matrix specifies how to translate, scale, shear or rotate the
64 coordinate system, and is typically used when rendering graphics.
65 QMatrix, in contrast to QTransform, does not allow perspective
66 transformations. QTransform is the recommended transformation
69 A QMatrix object can be built using the setMatrix(), scale(),
70 rotate(), translate() and shear() functions. Alternatively, it
71 can be built by applying \l {QMatrix#Basic Matrix
72 Operations}{basic matrix operations}. The matrix can also be
73 defined when constructed, and it can be reset to the identity
74 matrix (the default) using the reset() function.
76 The QMatrix class supports mapping of graphic primitives: A given
77 point, line, polygon, region, or painter path can be mapped to the
78 coordinate system defined by \e this matrix using the map()
79 function. In case of a rectangle, its coordinates can be
80 transformed using the mapRect() function. A rectangle can also be
81 transformed into a \e polygon (mapped to the coordinate system
82 defined by \e this matrix), using the mapToPolygon() function.
84 QMatrix provides the isIdentity() function which returns true if
85 the matrix is the identity matrix, and the isInvertible() function
86 which returns true if the matrix is non-singular (i.e. AB = BA =
87 I). The inverted() function returns an inverted copy of \e this
88 matrix if it is invertible (otherwise it returns the identity
89 matrix). In addition, QMatrix provides the determinant() function
90 returning the matrix's determinant.
92 Finally, the QMatrix class supports matrix multiplication, and
93 objects of the class can be streamed as well as compared.
97 \section1 Rendering Graphics
99 When rendering graphics, the matrix defines the transformations
100 but the actual transformation is performed by the drawing routines
103 By default, QPainter operates on the associated device's own
104 coordinate system. The standard coordinate system of a
105 QPaintDevice has its origin located at the top-left position. The
106 \e x values increase to the right; \e y values increase
107 downward. For a complete description, see the \l {Coordinate
108 System}{coordinate system} documentation.
110 QPainter has functions to translate, scale, shear and rotate the
111 coordinate system without using a QMatrix. For example:
115 \li \inlineimage qmatrix-simpletransformation.png
117 \snippet matrix/matrix.cpp 0
120 Although these functions are very convenient, it can be more
121 efficient to build a QMatrix and call QPainter::setMatrix() if you
122 want to perform more than a single transform operation. For
127 \li \inlineimage qmatrix-combinedtransformation.png
129 \snippet matrix/matrix.cpp 1
132 \section1 Basic Matrix Operations
134 \image qmatrix-representation.png
136 A QMatrix object contains a 3 x 3 matrix. The \c dx and \c dy
137 elements specify horizontal and vertical translation. The \c m11
138 and \c m22 elements specify horizontal and vertical scaling. And
139 finally, the \c m21 and \c m12 elements specify horizontal and
140 vertical \e shearing.
142 QMatrix transforms a point in the plane to another point using the
145 \snippet code/src_gui_painting_qmatrix.cpp 0
147 The point \e (x, y) is the original point, and \e (x', y') is the
148 transformed point. \e (x', y') can be transformed back to \e (x,
149 y) by performing the same operation on the inverted() matrix.
151 The various matrix elements can be set when constructing the
152 matrix, or by using the setMatrix() function later on. They can also
153 be manipulated using the translate(), rotate(), scale() and
154 shear() convenience functions, The currently set values can be
155 retrieved using the m11(), m12(), m21(), m22(), dx() and dy()
158 Translation is the simplest transformation. Setting \c dx and \c
159 dy will move the coordinate system \c dx units along the X axis
160 and \c dy units along the Y axis. Scaling can be done by setting
161 \c m11 and \c m22. For example, setting \c m11 to 2 and \c m22 to
162 1.5 will double the height and increase the width by 50%. The
163 identity matrix has \c m11 and \c m22 set to 1 (all others are set
164 to 0) mapping a point to itself. Shearing is controlled by \c m12
165 and \c m21. Setting these elements to values different from zero
166 will twist the coordinate system. Rotation is achieved by
167 carefully setting both the shearing factors and the scaling
170 Here's the combined transformations example using basic matrix
175 \li \inlineimage qmatrix-combinedtransformation.png
177 \snippet matrix/matrix.cpp 2
180 \sa QPainter, QTransform, {Coordinate System},
181 {painting/affine}{Affine Transformations Example}, {Transformations Example}
185 // some defines to inline some code
186 #define MAPDOUBLE(x, y, nx, ny) \
190 nx = _m11*fx + _m21*fy + _dx; \
191 ny = _m12*fx + _m22*fy + _dy; \
194 #define MAPINT(x, y, nx, ny) \
198 nx = qRound(_m11*fx + _m21*fy + _dx); \
199 ny = qRound(_m12*fx + _m22*fy + _dy); \
202 /*****************************************************************************
203 QMatrix member functions
204 *****************************************************************************/
206 \fn QMatrix::QMatrix(Qt::Initialization)
211 Constructs an identity matrix.
213 All elements are set to zero except \c m11 and \c m22 (specifying
214 the scale), which are set to 1.
230 Constructs a matrix with the elements, \a m11, \a m12, \a m21, \a
231 m22, \a dx and \a dy.
236 QMatrix::QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
248 Constructs a matrix that is a copy of the given \a matrix.
250 QMatrix::QMatrix(const QMatrix &matrix)
261 Sets the matrix elements to the specified values, \a m11, \a m12,
262 \a m21, \a m22, \a dx and \a dy.
264 Note that this function replaces the previous values. QMatrix
265 provide the translate(), rotate(), scale() and shear() convenience
266 functions to manipulate the various matrix elements based on the
267 currently defined coordinate system.
272 void QMatrix::setMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
284 \fn qreal QMatrix::m11() const
286 Returns the horizontal scaling factor.
288 \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix
293 \fn qreal QMatrix::m12() const
295 Returns the vertical shearing factor.
297 \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix
302 \fn qreal QMatrix::m21() const
304 Returns the horizontal shearing factor.
306 \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix
311 \fn qreal QMatrix::m22() const
313 Returns the vertical scaling factor.
315 \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix
320 \fn qreal QMatrix::dx() const
322 Returns the horizontal translation factor.
324 \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix
329 \fn qreal QMatrix::dy() const
331 Returns the vertical translation factor.
333 \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix
339 Maps the given coordinates \a x and \a y into the coordinate
340 system defined by this matrix. The resulting values are put in *\a
341 tx and *\a ty, respectively.
343 The coordinates are transformed using the following formulas:
345 \snippet code/src_gui_painting_qmatrix.cpp 1
347 The point (x, y) is the original point, and (x', y') is the
350 \sa {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}
353 void QMatrix::map(qreal x, qreal y, qreal *tx, qreal *ty) const
355 MAPDOUBLE(x, y, *tx, *ty);
363 Maps the given coordinates \a x and \a y into the coordinate
364 system defined by this matrix. The resulting values are put in *\a
365 tx and *\a ty, respectively. Note that the transformed coordinates
366 are rounded to the nearest integer.
369 void QMatrix::map(int x, int y, int *tx, int *ty) const
371 MAPINT(x, y, *tx, *ty);
374 QRect QMatrix::mapRect(const QRect &rect) const
377 if (_m12 == 0.0F && _m21 == 0.0F) {
378 int x = qRound(_m11*rect.x() + _dx);
379 int y = qRound(_m22*rect.y() + _dy);
380 int w = qRound(_m11*rect.width());
381 int h = qRound(_m22*rect.height());
390 result = QRect(x, y, w, h);
392 // see mapToPolygon for explanations of the algorithm.
395 MAPDOUBLE(rect.left(), rect.top(), x0, y0);
400 MAPDOUBLE(rect.right() + 1, rect.top(), x, y);
401 xmin = qMin(xmin, x);
402 ymin = qMin(ymin, y);
403 xmax = qMax(xmax, x);
404 ymax = qMax(ymax, y);
405 MAPDOUBLE(rect.right() + 1, rect.bottom() + 1, x, y);
406 xmin = qMin(xmin, x);
407 ymin = qMin(ymin, y);
408 xmax = qMax(xmax, x);
409 ymax = qMax(ymax, y);
410 MAPDOUBLE(rect.left(), rect.bottom() + 1, x, y);
411 xmin = qMin(xmin, x);
412 ymin = qMin(ymin, y);
413 xmax = qMax(xmax, x);
414 ymax = qMax(ymax, y);
415 result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin), qRound(ymax)-qRound(ymin));
421 \fn QRectF QMatrix::mapRect(const QRectF &rectangle) const
423 Creates and returns a QRectF object that is a copy of the given \a
424 rectangle, mapped into the coordinate system defined by this
427 The rectangle's coordinates are transformed using the following
430 \snippet code/src_gui_painting_qmatrix.cpp 2
432 If rotation or shearing has been specified, this function returns
433 the \e bounding rectangle. To retrieve the exact region the given
434 \a rectangle maps to, use the mapToPolygon() function instead.
436 \sa mapToPolygon(), {QMatrix#Basic Matrix Operations}{Basic Matrix
439 QRectF QMatrix::mapRect(const QRectF &rect) const
442 if (_m12 == 0.0F && _m21 == 0.0F) {
443 qreal x = _m11*rect.x() + _dx;
444 qreal y = _m22*rect.y() + _dy;
445 qreal w = _m11*rect.width();
446 qreal h = _m22*rect.height();
455 result = QRectF(x, y, w, h);
459 MAPDOUBLE(rect.x(), rect.y(), x0, y0);
464 MAPDOUBLE(rect.x() + rect.width(), rect.y(), x, y);
465 xmin = qMin(xmin, x);
466 ymin = qMin(ymin, y);
467 xmax = qMax(xmax, x);
468 ymax = qMax(ymax, y);
469 MAPDOUBLE(rect.x() + rect.width(), rect.y() + rect.height(), x, y);
470 xmin = qMin(xmin, x);
471 ymin = qMin(ymin, y);
472 xmax = qMax(xmax, x);
473 ymax = qMax(ymax, y);
474 MAPDOUBLE(rect.x(), rect.y() + rect.height(), x, y);
475 xmin = qMin(xmin, x);
476 ymin = qMin(ymin, y);
477 xmax = qMax(xmax, x);
478 ymax = qMax(ymax, y);
479 result = QRectF(xmin, ymin, xmax-xmin, ymax - ymin);
485 \fn QRect QMatrix::mapRect(const QRect &rectangle) const
488 Creates and returns a QRect object that is a copy of the given \a
489 rectangle, mapped into the coordinate system defined by this
490 matrix. Note that the transformed coordinates are rounded to the
496 \fn QPoint operator*(const QPoint &point, const QMatrix &matrix)
499 This is the same as \a{matrix}.map(\a{point}).
504 QPoint QMatrix::map(const QPoint &p) const
508 return QPoint(qRound(_m11*fx + _m21*fy + _dx),
509 qRound(_m12*fx + _m22*fy + _dy));
513 \fn QPointF operator*(const QPointF &point, const QMatrix &matrix)
516 Same as \a{matrix}.map(\a{point}).
524 Creates and returns a QPointF object that is a copy of the given
525 \a point, mapped into the coordinate system defined by this
528 QPointF QMatrix::map(const QPointF &point) const
530 qreal fx = point.x();
531 qreal fy = point.y();
532 return QPointF(_m11*fx + _m21*fy + _dx, _m12*fx + _m22*fy + _dy);
536 \fn QPoint QMatrix::map(const QPoint &point) const
539 Creates and returns a QPoint object that is a copy of the given \a
540 point, mapped into the coordinate system defined by this
541 matrix. Note that the transformed coordinates are rounded to the
546 \fn QLineF operator*(const QLineF &line, const QMatrix &matrix)
549 This is the same as \a{matrix}.map(\a{line}).
555 \fn QLine operator*(const QLine &line, const QMatrix &matrix)
558 This is the same as \a{matrix}.map(\a{line}).
566 Creates and returns a QLineF object that is a copy of the given \a
567 line, mapped into the coordinate system defined by this matrix.
569 QLineF QMatrix::map(const QLineF &line) const
571 return QLineF(map(line.p1()), map(line.p2()));
577 Creates and returns a QLine object that is a copy of the given \a
578 line, mapped into the coordinate system defined by this matrix.
579 Note that the transformed coordinates are rounded to the nearest
582 QLine QMatrix::map(const QLine &line) const
584 return QLine(map(line.p1()), map(line.p2()));
588 \fn QPolygonF operator *(const QPolygonF &polygon, const QMatrix &matrix)
591 This is the same as \a{matrix}.map(\a{polygon}).
597 \fn QPolygon operator*(const QPolygon &polygon, const QMatrix &matrix)
600 This is the same as \a{matrix}.map(\a{polygon}).
605 QPolygon QMatrix::map(const QPolygon &a) const
610 const QPoint *da = a.constData();
611 QPoint *dp = p.data();
612 for(i = 0; i < size; i++) {
613 MAPINT(da[i].x(), da[i].y(), dp[i].rx(), dp[i].ry());
619 \fn QPolygonF QMatrix::map(const QPolygonF &polygon) const
622 Creates and returns a QPolygonF object that is a copy of the given
623 \a polygon, mapped into the coordinate system defined by this
626 QPolygonF QMatrix::map(const QPolygonF &a) const
631 const QPointF *da = a.constData();
632 QPointF *dp = p.data();
633 for(i = 0; i < size; i++) {
634 MAPDOUBLE(da[i].xp, da[i].yp, dp[i].xp, dp[i].yp);
640 \fn QPolygon QMatrix::map(const QPolygon &polygon) const
643 Creates and returns a QPolygon object that is a copy of the given
644 \a polygon, mapped into the coordinate system defined by this
645 matrix. Note that the transformed coordinates are rounded to the
650 \fn QRegion operator*(const QRegion ®ion, const QMatrix &matrix)
653 This is the same as \a{matrix}.map(\a{region}).
658 extern QPainterPath qt_regionToPath(const QRegion ®ion);
661 \fn QRegion QMatrix::map(const QRegion ®ion) const
664 Creates and returns a QRegion object that is a copy of the given
665 \a region, mapped into the coordinate system defined by this matrix.
667 Calling this method can be rather expensive if rotations or
670 QRegion QMatrix::map(const QRegion &r) const
672 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) { // translate or identity
673 if (_dx == 0.0 && _dy == 0.0) // Identity
676 copy.translate(qRound(_dx), qRound(_dy));
680 QPainterPath p = map(qt_regionToPath(r));
681 return p.toFillPolygon().toPolygon();
685 \fn QPainterPath operator *(const QPainterPath &path, const QMatrix &matrix)
688 This is the same as \a{matrix}.map(\a{path}).
696 Creates and returns a QPainterPath object that is a copy of the
697 given \a path, mapped into the coordinate system defined by this
700 QPainterPath QMatrix::map(const QPainterPath &path) const
703 return QPainterPath();
705 QPainterPath copy = path;
707 // Translate or identity
708 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {
711 if (_dx != 0.0 || _dy != 0.0) {
713 for (int i=0; i<path.elementCount(); ++i) {
714 QPainterPath::Element &e = copy.d_ptr->elements[i];
723 for (int i=0; i<path.elementCount(); ++i) {
724 QPainterPath::Element &e = copy.d_ptr->elements[i];
725 qreal fx = e.x, fy = e.y;
726 e.x = _m11*fx + _m21*fy + _dx;
727 e.y = _m12*fx + _m22*fy + _dy;
735 \fn QPolygon QMatrix::mapToPolygon(const QRect &rectangle) const
737 Creates and returns a QPolygon representation of the given \a
738 rectangle, mapped into the coordinate system defined by this
741 The rectangle's coordinates are transformed using the following
744 \snippet code/src_gui_painting_qmatrix.cpp 3
746 Polygons and rectangles behave slightly differently when
747 transformed (due to integer rounding), so
748 \c{matrix.map(QPolygon(rectangle))} is not always the same as
749 \c{matrix.mapToPolygon(rectangle)}.
751 \sa mapRect(), {QMatrix#Basic Matrix Operations}{Basic Matrix
754 QPolygon QMatrix::mapToPolygon(const QRect &rect) const
758 if (_m12 == 0.0F && _m21 == 0.0F) {
759 x[0] = _m11*rect.x() + _dx;
760 y[0] = _m22*rect.y() + _dy;
761 qreal w = _m11*rect.width();
762 qreal h = _m22*rect.height();
778 qreal right = rect.x() + rect.width();
779 qreal bottom = rect.y() + rect.height();
780 MAPDOUBLE(rect.x(), rect.y(), x[0], y[0]);
781 MAPDOUBLE(right, rect.y(), x[1], y[1]);
782 MAPDOUBLE(right, bottom, x[2], y[2]);
783 MAPDOUBLE(rect.x(), bottom, x[3], y[3]);
787 for(i = 0; i< 4; i++)
788 qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i]));
789 qDebug("width=%f, height=%f", qSqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0])),
790 qSqrt((x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3])));
792 // all coordinates are correctly, tranform to a pointarray
793 // (rounding to the next integer)
794 a.setPoints(4, qRound(x[0]), qRound(y[0]),
795 qRound(x[1]), qRound(y[1]),
796 qRound(x[2]), qRound(y[2]),
797 qRound(x[3]), qRound(y[3]));
802 Resets the matrix to an identity matrix, i.e. all elements are set
803 to zero, except \c m11 and \c m22 (specifying the scale) which are
806 \sa QMatrix(), isIdentity(), {QMatrix#Basic Matrix
807 Operations}{Basic Matrix Operations}
810 void QMatrix::reset()
813 _m12 = _m21 = _dx = _dy = 0.0;
817 \fn bool QMatrix::isIdentity() const
819 Returns true if the matrix is the identity matrix, otherwise
826 Moves the coordinate system \a dx along the x axis and \a dy along
827 the y axis, and returns a reference to the matrix.
832 QMatrix &QMatrix::translate(qreal dx, qreal dy)
834 _dx += dx*_m11 + dy*_m21;
835 _dy += dy*_m22 + dx*_m12;
840 \fn QMatrix &QMatrix::scale(qreal sx, qreal sy)
842 Scales the coordinate system by \a sx horizontally and \a sy
843 vertically, and returns a reference to the matrix.
848 QMatrix &QMatrix::scale(qreal sx, qreal sy)
858 Shears the coordinate system by \a sh horizontally and \a sv
859 vertically, and returns a reference to the matrix.
864 QMatrix &QMatrix::shear(qreal sh, qreal sv)
866 qreal tm11 = sv*_m21;
867 qreal tm12 = sv*_m22;
868 qreal tm21 = sh*_m11;
869 qreal tm22 = sh*_m12;
877 const qreal deg2rad = qreal(0.017453292519943295769); // pi/180
880 \fn QMatrix &QMatrix::rotate(qreal degrees)
882 Rotates the coordinate system the given \a degrees
885 Note that if you apply a QMatrix to a point defined in widget
886 coordinates, the direction of the rotation will be clockwise
887 because the y-axis points downwards.
889 Returns a reference to the matrix.
894 QMatrix &QMatrix::rotate(qreal a)
898 if (a == 90. || a == -270.)
900 else if (a == 270. || a == -90.)
905 qreal b = deg2rad*a; // convert to radians
906 sina = qSin(b); // fast and convenient
909 qreal tm11 = cosa*_m11 + sina*_m21;
910 qreal tm12 = cosa*_m12 + sina*_m22;
911 qreal tm21 = -sina*_m11 + cosa*_m21;
912 qreal tm22 = -sina*_m12 + cosa*_m22;
913 _m11 = tm11; _m12 = tm12;
914 _m21 = tm21; _m22 = tm22;
919 \fn bool QMatrix::isInvertible() const
921 Returns true if the matrix is invertible, otherwise returns false.
928 \fn qreal QMatrix::determinant() const
930 Returns the matrix's determinant.
934 Returns an inverted copy of this matrix.
936 If the matrix is singular (not invertible), the returned matrix is
937 the identity matrix. If \a invertible is valid (i.e. not 0), its
938 value is set to true if the matrix is invertible, otherwise it is
944 QMatrix QMatrix::inverted(bool *invertible) const
946 qreal dtr = determinant();
949 *invertible = false; // singular matrix
950 return QMatrix(true);
952 else { // invertible matrix
955 qreal dinv = 1.0/dtr;
956 return QMatrix((_m22*dinv), (-_m12*dinv),
957 (-_m21*dinv), (_m11*dinv),
958 ((_m21*_dy - _m22*_dx)*dinv),
959 ((_m12*_dx - _m11*_dy)*dinv),
966 \fn bool QMatrix::operator==(const QMatrix &matrix) const
968 Returns true if this matrix is equal to the given \a matrix,
969 otherwise returns false.
972 bool QMatrix::operator==(const QMatrix &m) const
974 return _m11 == m._m11 &&
983 \fn bool QMatrix::operator!=(const QMatrix &matrix) const
985 Returns true if this matrix is not equal to the given \a matrix,
986 otherwise returns false.
989 bool QMatrix::operator!=(const QMatrix &m) const
991 return _m11 != m._m11 ||
1000 \fn QMatrix &QMatrix::operator *=(const QMatrix &matrix)
1003 Returns the result of multiplying this matrix by the given \a
1007 QMatrix &QMatrix::operator *=(const QMatrix &m)
1009 qreal tm11 = _m11*m._m11 + _m12*m._m21;
1010 qreal tm12 = _m11*m._m12 + _m12*m._m22;
1011 qreal tm21 = _m21*m._m11 + _m22*m._m21;
1012 qreal tm22 = _m21*m._m12 + _m22*m._m22;
1014 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1015 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1017 _m11 = tm11; _m12 = tm12;
1018 _m21 = tm21; _m22 = tm22;
1019 _dx = tdx; _dy = tdy;
1024 \fn QMatrix QMatrix::operator *(const QMatrix &matrix) const
1026 Returns the result of multiplying this matrix by the given \a
1029 Note that matrix multiplication is not commutative, i.e. a*b !=
1033 QMatrix QMatrix::operator *(const QMatrix &m) const
1035 qreal tm11 = _m11*m._m11 + _m12*m._m21;
1036 qreal tm12 = _m11*m._m12 + _m12*m._m22;
1037 qreal tm21 = _m21*m._m11 + _m22*m._m21;
1038 qreal tm22 = _m21*m._m12 + _m22*m._m22;
1040 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1041 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1042 return QMatrix(tm11, tm12, tm21, tm22, tdx, tdy, true);
1046 Assigns the given \a matrix's values to this matrix.
1048 QMatrix &QMatrix::operator=(const QMatrix &matrix)
1062 Returns the matrix as a QVariant.
1064 QMatrix::operator QVariant() const
1066 return QVariant(QVariant::Matrix, this);
1069 Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m)
1075 /*****************************************************************************
1076 QMatrix stream functions
1077 *****************************************************************************/
1078 #ifndef QT_NO_DATASTREAM
1080 \fn QDataStream &operator<<(QDataStream &stream, const QMatrix &matrix)
1083 Writes the given \a matrix to the given \a stream and returns a
1084 reference to the stream.
1086 \sa {Serializing Qt Data Types}
1089 QDataStream &operator<<(QDataStream &s, const QMatrix &m)
1091 if (s.version() == 1) {
1092 s << (float)m.m11() << (float)m.m12() << (float)m.m21()
1093 << (float)m.m22() << (float)m.dx() << (float)m.dy();
1095 s << double(m.m11())
1106 \fn QDataStream &operator>>(QDataStream &stream, QMatrix &matrix)
1109 Reads the given \a matrix from the given \a stream and returns a
1110 reference to the stream.
1112 \sa {Serializing Qt Data Types}
1115 QDataStream &operator>>(QDataStream &s, QMatrix &m)
1117 if (s.version() == 1) {
1118 float m11, m12, m21, m22, dx, dy;
1119 s >> m11; s >> m12; s >> m21; s >> m22;
1121 m.setMatrix(m11, m12, m21, m22, dx, dy);
1124 double m11, m12, m21, m22, dx, dy;
1131 m.setMatrix(m11, m12, m21, m22, dx, dy);
1135 #endif // QT_NO_DATASTREAM
1137 #ifndef QT_NO_DEBUG_STREAM
1138 QDebug operator<<(QDebug dbg, const QMatrix &m)
1140 dbg.nospace() << "QMatrix("
1142 << " 12=" << m.m12()
1143 << " 21=" << m.m21()
1144 << " 22=" << m.m22()
1153 \fn bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
1158 \brief The qFuzzyCompare function is for comparing two matrices
1159 using a fuzziness factor.
1161 Returns true if \a m1 and \a m2 are equal, allowing for a small
1162 fuzziness factor for floating-point comparisons; false otherwise.