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
64 A matrix specifies how to translate, scale, shear or rotate the
65 coordinate system, and is typically used when rendering graphics.
66 QMatrix, in contrast to QTransform, does not allow perspective
67 transformations. QTransform is the recommended transformation
70 A QMatrix object can be built using the setMatrix(), scale(),
71 rotate(), translate() and shear() functions. Alternatively, it
72 can be built by applying \l {QMatrix#Basic Matrix
73 Operations}{basic matrix operations}. The matrix can also be
74 defined when constructed, and it can be reset to the identity
75 matrix (the default) using the reset() function.
77 The QMatrix class supports mapping of graphic primitives: A given
78 point, line, polygon, region, or painter path can be mapped to the
79 coordinate system defined by \e this matrix using the map()
80 function. In case of a rectangle, its coordinates can be
81 transformed using the mapRect() function. A rectangle can also be
82 transformed into a \e polygon (mapped to the coordinate system
83 defined by \e this matrix), using the mapToPolygon() function.
85 QMatrix provides the isIdentity() function which returns true if
86 the matrix is the identity matrix, and the isInvertible() function
87 which returns true if the matrix is non-singular (i.e. AB = BA =
88 I). The inverted() function returns an inverted copy of \e this
89 matrix if it is invertible (otherwise it returns the identity
90 matrix). In addition, QMatrix provides the determinant() function
91 returning the matrix's determinant.
93 Finally, the QMatrix class supports matrix multiplication, and
94 objects of the class can be streamed as well as compared.
98 \section1 Rendering Graphics
100 When rendering graphics, the matrix defines the transformations
101 but the actual transformation is performed by the drawing routines
104 By default, QPainter operates on the associated device's own
105 coordinate system. The standard coordinate system of a
106 QPaintDevice has its origin located at the top-left position. The
107 \e x values increase to the right; \e y values increase
108 downward. For a complete description, see the \l {Coordinate
109 System}{coordinate system} documentation.
111 QPainter has functions to translate, scale, shear and rotate the
112 coordinate system without using a QMatrix. For example:
116 \li \inlineimage qmatrix-simpletransformation.png
118 \snippet matrix/matrix.cpp 0
121 Although these functions are very convenient, it can be more
122 efficient to build a QMatrix and call QPainter::setMatrix() if you
123 want to perform more than a single transform operation. For
128 \li \inlineimage qmatrix-combinedtransformation.png
130 \snippet matrix/matrix.cpp 1
133 \section1 Basic Matrix Operations
135 \image qmatrix-representation.png
137 A QMatrix object contains a 3 x 3 matrix. The \c dx and \c dy
138 elements specify horizontal and vertical translation. The \c m11
139 and \c m22 elements specify horizontal and vertical scaling. And
140 finally, the \c m21 and \c m12 elements specify horizontal and
141 vertical \e shearing.
143 QMatrix transforms a point in the plane to another point using the
146 \snippet code/src_gui_painting_qmatrix.cpp 0
148 The point \e (x, y) is the original point, and \e (x', y') is the
149 transformed point. \e (x', y') can be transformed back to \e (x,
150 y) by performing the same operation on the inverted() matrix.
152 The various matrix elements can be set when constructing the
153 matrix, or by using the setMatrix() function later on. They can also
154 be manipulated using the translate(), rotate(), scale() and
155 shear() convenience functions, The currently set values can be
156 retrieved using the m11(), m12(), m21(), m22(), dx() and dy()
159 Translation is the simplest transformation. Setting \c dx and \c
160 dy will move the coordinate system \c dx units along the X axis
161 and \c dy units along the Y axis. Scaling can be done by setting
162 \c m11 and \c m22. For example, setting \c m11 to 2 and \c m22 to
163 1.5 will double the height and increase the width by 50%. The
164 identity matrix has \c m11 and \c m22 set to 1 (all others are set
165 to 0) mapping a point to itself. Shearing is controlled by \c m12
166 and \c m21. Setting these elements to values different from zero
167 will twist the coordinate system. Rotation is achieved by
168 carefully setting both the shearing factors and the scaling
171 Here's the combined transformations example using basic matrix
176 \li \inlineimage qmatrix-combinedtransformation.png
178 \snippet matrix/matrix.cpp 2
181 \sa QPainter, QTransform, {Coordinate System},
182 {painting/affine}{Affine Transformations Example}, {Transformations Example}
186 // some defines to inline some code
187 #define MAPDOUBLE(x, y, nx, ny) \
191 nx = _m11*fx + _m21*fy + _dx; \
192 ny = _m12*fx + _m22*fy + _dy; \
195 #define MAPINT(x, y, nx, ny) \
199 nx = qRound(_m11*fx + _m21*fy + _dx); \
200 ny = qRound(_m12*fx + _m22*fy + _dy); \
203 /*****************************************************************************
204 QMatrix member functions
205 *****************************************************************************/
207 \fn QMatrix::QMatrix(Qt::Initialization)
212 Constructs an identity matrix.
214 All elements are set to zero except \c m11 and \c m22 (specifying
215 the scale), which are set to 1.
231 Constructs a matrix with the elements, \a m11, \a m12, \a m21, \a
232 m22, \a dx and \a dy.
237 QMatrix::QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
249 Constructs a matrix that is a copy of the given \a matrix.
251 QMatrix::QMatrix(const QMatrix &matrix)
262 Sets the matrix elements to the specified values, \a m11, \a m12,
263 \a m21, \a m22, \a dx and \a dy.
265 Note that this function replaces the previous values. QMatrix
266 provide the translate(), rotate(), scale() and shear() convenience
267 functions to manipulate the various matrix elements based on the
268 currently defined coordinate system.
273 void QMatrix::setMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
285 \fn qreal QMatrix::m11() const
287 Returns the horizontal scaling factor.
289 \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix
294 \fn qreal QMatrix::m12() const
296 Returns the vertical shearing factor.
298 \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix
303 \fn qreal QMatrix::m21() const
305 Returns the horizontal shearing factor.
307 \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix
312 \fn qreal QMatrix::m22() const
314 Returns the vertical scaling factor.
316 \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix
321 \fn qreal QMatrix::dx() const
323 Returns the horizontal translation factor.
325 \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix
330 \fn qreal QMatrix::dy() const
332 Returns the vertical translation factor.
334 \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix
340 Maps the given coordinates \a x and \a y into the coordinate
341 system defined by this matrix. The resulting values are put in *\a
342 tx and *\a ty, respectively.
344 The coordinates are transformed using the following formulas:
346 \snippet code/src_gui_painting_qmatrix.cpp 1
348 The point (x, y) is the original point, and (x', y') is the
351 \sa {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}
354 void QMatrix::map(qreal x, qreal y, qreal *tx, qreal *ty) const
356 MAPDOUBLE(x, y, *tx, *ty);
364 Maps the given coordinates \a x and \a y into the coordinate
365 system defined by this matrix. The resulting values are put in *\a
366 tx and *\a ty, respectively. Note that the transformed coordinates
367 are rounded to the nearest integer.
370 void QMatrix::map(int x, int y, int *tx, int *ty) const
372 MAPINT(x, y, *tx, *ty);
375 QRect QMatrix::mapRect(const QRect &rect) const
378 if (_m12 == 0.0F && _m21 == 0.0F) {
379 int x = qRound(_m11*rect.x() + _dx);
380 int y = qRound(_m22*rect.y() + _dy);
381 int w = qRound(_m11*rect.width());
382 int h = qRound(_m22*rect.height());
391 result = QRect(x, y, w, h);
393 // see mapToPolygon for explanations of the algorithm.
396 MAPDOUBLE(rect.left(), rect.top(), x0, y0);
401 MAPDOUBLE(rect.right() + 1, rect.top(), x, y);
402 xmin = qMin(xmin, x);
403 ymin = qMin(ymin, y);
404 xmax = qMax(xmax, x);
405 ymax = qMax(ymax, y);
406 MAPDOUBLE(rect.right() + 1, rect.bottom() + 1, x, y);
407 xmin = qMin(xmin, x);
408 ymin = qMin(ymin, y);
409 xmax = qMax(xmax, x);
410 ymax = qMax(ymax, y);
411 MAPDOUBLE(rect.left(), rect.bottom() + 1, x, y);
412 xmin = qMin(xmin, x);
413 ymin = qMin(ymin, y);
414 xmax = qMax(xmax, x);
415 ymax = qMax(ymax, y);
416 result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin), qRound(ymax)-qRound(ymin));
422 \fn QRectF QMatrix::mapRect(const QRectF &rectangle) const
424 Creates and returns a QRectF object that is a copy of the given \a
425 rectangle, mapped into the coordinate system defined by this
428 The rectangle's coordinates are transformed using the following
431 \snippet code/src_gui_painting_qmatrix.cpp 2
433 If rotation or shearing has been specified, this function returns
434 the \e bounding rectangle. To retrieve the exact region the given
435 \a rectangle maps to, use the mapToPolygon() function instead.
437 \sa mapToPolygon(), {QMatrix#Basic Matrix Operations}{Basic Matrix
440 QRectF QMatrix::mapRect(const QRectF &rect) const
443 if (_m12 == 0.0F && _m21 == 0.0F) {
444 qreal x = _m11*rect.x() + _dx;
445 qreal y = _m22*rect.y() + _dy;
446 qreal w = _m11*rect.width();
447 qreal h = _m22*rect.height();
456 result = QRectF(x, y, w, h);
460 MAPDOUBLE(rect.x(), rect.y(), x0, y0);
465 MAPDOUBLE(rect.x() + rect.width(), rect.y(), x, y);
466 xmin = qMin(xmin, x);
467 ymin = qMin(ymin, y);
468 xmax = qMax(xmax, x);
469 ymax = qMax(ymax, y);
470 MAPDOUBLE(rect.x() + rect.width(), rect.y() + rect.height(), x, y);
471 xmin = qMin(xmin, x);
472 ymin = qMin(ymin, y);
473 xmax = qMax(xmax, x);
474 ymax = qMax(ymax, y);
475 MAPDOUBLE(rect.x(), rect.y() + rect.height(), x, y);
476 xmin = qMin(xmin, x);
477 ymin = qMin(ymin, y);
478 xmax = qMax(xmax, x);
479 ymax = qMax(ymax, y);
480 result = QRectF(xmin, ymin, xmax-xmin, ymax - ymin);
486 \fn QRect QMatrix::mapRect(const QRect &rectangle) const
489 Creates and returns a QRect object that is a copy of the given \a
490 rectangle, mapped into the coordinate system defined by this
491 matrix. Note that the transformed coordinates are rounded to the
497 \fn QPoint operator*(const QPoint &point, const QMatrix &matrix)
500 This is the same as \a{matrix}.map(\a{point}).
505 QPoint QMatrix::map(const QPoint &p) const
509 return QPoint(qRound(_m11*fx + _m21*fy + _dx),
510 qRound(_m12*fx + _m22*fy + _dy));
514 \fn QPointF operator*(const QPointF &point, const QMatrix &matrix)
517 Same as \a{matrix}.map(\a{point}).
525 Creates and returns a QPointF object that is a copy of the given
526 \a point, mapped into the coordinate system defined by this
529 QPointF QMatrix::map(const QPointF &point) const
531 qreal fx = point.x();
532 qreal fy = point.y();
533 return QPointF(_m11*fx + _m21*fy + _dx, _m12*fx + _m22*fy + _dy);
537 \fn QPoint QMatrix::map(const QPoint &point) const
540 Creates and returns a QPoint object that is a copy of the given \a
541 point, mapped into the coordinate system defined by this
542 matrix. Note that the transformed coordinates are rounded to the
547 \fn QLineF operator*(const QLineF &line, const QMatrix &matrix)
550 This is the same as \a{matrix}.map(\a{line}).
556 \fn QLine operator*(const QLine &line, const QMatrix &matrix)
559 This is the same as \a{matrix}.map(\a{line}).
567 Creates and returns a QLineF object that is a copy of the given \a
568 line, mapped into the coordinate system defined by this matrix.
570 QLineF QMatrix::map(const QLineF &line) const
572 return QLineF(map(line.p1()), map(line.p2()));
578 Creates and returns a QLine object that is a copy of the given \a
579 line, mapped into the coordinate system defined by this matrix.
580 Note that the transformed coordinates are rounded to the nearest
583 QLine QMatrix::map(const QLine &line) const
585 return QLine(map(line.p1()), map(line.p2()));
589 \fn QPolygonF operator *(const QPolygonF &polygon, const QMatrix &matrix)
592 This is the same as \a{matrix}.map(\a{polygon}).
598 \fn QPolygon operator*(const QPolygon &polygon, const QMatrix &matrix)
601 This is the same as \a{matrix}.map(\a{polygon}).
606 QPolygon QMatrix::map(const QPolygon &a) const
611 const QPoint *da = a.constData();
612 QPoint *dp = p.data();
613 for(i = 0; i < size; i++) {
614 MAPINT(da[i].x(), da[i].y(), dp[i].rx(), dp[i].ry());
620 \fn QPolygonF QMatrix::map(const QPolygonF &polygon) const
623 Creates and returns a QPolygonF object that is a copy of the given
624 \a polygon, mapped into the coordinate system defined by this
627 QPolygonF QMatrix::map(const QPolygonF &a) const
632 const QPointF *da = a.constData();
633 QPointF *dp = p.data();
634 for(i = 0; i < size; i++) {
635 MAPDOUBLE(da[i].xp, da[i].yp, dp[i].xp, dp[i].yp);
641 \fn QPolygon QMatrix::map(const QPolygon &polygon) const
644 Creates and returns a QPolygon object that is a copy of the given
645 \a polygon, mapped into the coordinate system defined by this
646 matrix. Note that the transformed coordinates are rounded to the
651 \fn QRegion operator*(const QRegion ®ion, const QMatrix &matrix)
654 This is the same as \a{matrix}.map(\a{region}).
659 extern QPainterPath qt_regionToPath(const QRegion ®ion);
662 \fn QRegion QMatrix::map(const QRegion ®ion) const
665 Creates and returns a QRegion object that is a copy of the given
666 \a region, mapped into the coordinate system defined by this matrix.
668 Calling this method can be rather expensive if rotations or
671 QRegion QMatrix::map(const QRegion &r) const
673 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) { // translate or identity
674 if (_dx == 0.0 && _dy == 0.0) // Identity
677 copy.translate(qRound(_dx), qRound(_dy));
681 QPainterPath p = map(qt_regionToPath(r));
682 return p.toFillPolygon().toPolygon();
686 \fn QPainterPath operator *(const QPainterPath &path, const QMatrix &matrix)
689 This is the same as \a{matrix}.map(\a{path}).
697 Creates and returns a QPainterPath object that is a copy of the
698 given \a path, mapped into the coordinate system defined by this
701 QPainterPath QMatrix::map(const QPainterPath &path) const
704 return QPainterPath();
706 QPainterPath copy = path;
708 // Translate or identity
709 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {
712 if (_dx != 0.0 || _dy != 0.0) {
714 for (int i=0; i<path.elementCount(); ++i) {
715 QPainterPath::Element &e = copy.d_ptr->elements[i];
724 for (int i=0; i<path.elementCount(); ++i) {
725 QPainterPath::Element &e = copy.d_ptr->elements[i];
726 qreal fx = e.x, fy = e.y;
727 e.x = _m11*fx + _m21*fy + _dx;
728 e.y = _m12*fx + _m22*fy + _dy;
736 \fn QPolygon QMatrix::mapToPolygon(const QRect &rectangle) const
738 Creates and returns a QPolygon representation of the given \a
739 rectangle, mapped into the coordinate system defined by this
742 The rectangle's coordinates are transformed using the following
745 \snippet code/src_gui_painting_qmatrix.cpp 3
747 Polygons and rectangles behave slightly differently when
748 transformed (due to integer rounding), so
749 \c{matrix.map(QPolygon(rectangle))} is not always the same as
750 \c{matrix.mapToPolygon(rectangle)}.
752 \sa mapRect(), {QMatrix#Basic Matrix Operations}{Basic Matrix
755 QPolygon QMatrix::mapToPolygon(const QRect &rect) const
759 if (_m12 == 0.0F && _m21 == 0.0F) {
760 x[0] = _m11*rect.x() + _dx;
761 y[0] = _m22*rect.y() + _dy;
762 qreal w = _m11*rect.width();
763 qreal h = _m22*rect.height();
779 qreal right = rect.x() + rect.width();
780 qreal bottom = rect.y() + rect.height();
781 MAPDOUBLE(rect.x(), rect.y(), x[0], y[0]);
782 MAPDOUBLE(right, rect.y(), x[1], y[1]);
783 MAPDOUBLE(right, bottom, x[2], y[2]);
784 MAPDOUBLE(rect.x(), bottom, x[3], y[3]);
788 for(i = 0; i< 4; i++)
789 qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i]));
790 qDebug("width=%f, height=%f", qSqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0])),
791 qSqrt((x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3])));
793 // all coordinates are correctly, tranform to a pointarray
794 // (rounding to the next integer)
795 a.setPoints(4, qRound(x[0]), qRound(y[0]),
796 qRound(x[1]), qRound(y[1]),
797 qRound(x[2]), qRound(y[2]),
798 qRound(x[3]), qRound(y[3]));
803 Resets the matrix to an identity matrix, i.e. all elements are set
804 to zero, except \c m11 and \c m22 (specifying the scale) which are
807 \sa QMatrix(), isIdentity(), {QMatrix#Basic Matrix
808 Operations}{Basic Matrix Operations}
811 void QMatrix::reset()
814 _m12 = _m21 = _dx = _dy = 0.0;
818 \fn bool QMatrix::isIdentity() const
820 Returns true if the matrix is the identity matrix, otherwise
827 Moves the coordinate system \a dx along the x axis and \a dy along
828 the y axis, and returns a reference to the matrix.
833 QMatrix &QMatrix::translate(qreal dx, qreal dy)
835 _dx += dx*_m11 + dy*_m21;
836 _dy += dy*_m22 + dx*_m12;
841 \fn QMatrix &QMatrix::scale(qreal sx, qreal sy)
843 Scales the coordinate system by \a sx horizontally and \a sy
844 vertically, and returns a reference to the matrix.
849 QMatrix &QMatrix::scale(qreal sx, qreal sy)
859 Shears the coordinate system by \a sh horizontally and \a sv
860 vertically, and returns a reference to the matrix.
865 QMatrix &QMatrix::shear(qreal sh, qreal sv)
867 qreal tm11 = sv*_m21;
868 qreal tm12 = sv*_m22;
869 qreal tm21 = sh*_m11;
870 qreal tm22 = sh*_m12;
878 const qreal deg2rad = qreal(0.017453292519943295769); // pi/180
881 \fn QMatrix &QMatrix::rotate(qreal degrees)
883 Rotates the coordinate system the given \a degrees
886 Note that if you apply a QMatrix to a point defined in widget
887 coordinates, the direction of the rotation will be clockwise
888 because the y-axis points downwards.
890 Returns a reference to the matrix.
895 QMatrix &QMatrix::rotate(qreal a)
899 if (a == 90. || a == -270.)
901 else if (a == 270. || a == -90.)
906 qreal b = deg2rad*a; // convert to radians
907 sina = qSin(b); // fast and convenient
910 qreal tm11 = cosa*_m11 + sina*_m21;
911 qreal tm12 = cosa*_m12 + sina*_m22;
912 qreal tm21 = -sina*_m11 + cosa*_m21;
913 qreal tm22 = -sina*_m12 + cosa*_m22;
914 _m11 = tm11; _m12 = tm12;
915 _m21 = tm21; _m22 = tm22;
920 \fn bool QMatrix::isInvertible() const
922 Returns true if the matrix is invertible, otherwise returns false.
929 \fn qreal QMatrix::determinant() const
931 Returns the matrix's determinant.
935 Returns an inverted copy of this matrix.
937 If the matrix is singular (not invertible), the returned matrix is
938 the identity matrix. If \a invertible is valid (i.e. not 0), its
939 value is set to true if the matrix is invertible, otherwise it is
945 QMatrix QMatrix::inverted(bool *invertible) const
947 qreal dtr = determinant();
950 *invertible = false; // singular matrix
951 return QMatrix(true);
953 else { // invertible matrix
956 qreal dinv = 1.0/dtr;
957 return QMatrix((_m22*dinv), (-_m12*dinv),
958 (-_m21*dinv), (_m11*dinv),
959 ((_m21*_dy - _m22*_dx)*dinv),
960 ((_m12*_dx - _m11*_dy)*dinv),
967 \fn bool QMatrix::operator==(const QMatrix &matrix) const
969 Returns true if this matrix is equal to the given \a matrix,
970 otherwise returns false.
973 bool QMatrix::operator==(const QMatrix &m) const
975 return _m11 == m._m11 &&
984 \fn bool QMatrix::operator!=(const QMatrix &matrix) const
986 Returns true if this matrix is not equal to the given \a matrix,
987 otherwise returns false.
990 bool QMatrix::operator!=(const QMatrix &m) const
992 return _m11 != m._m11 ||
1001 \fn QMatrix &QMatrix::operator *=(const QMatrix &matrix)
1004 Returns the result of multiplying this matrix by the given \a
1008 QMatrix &QMatrix::operator *=(const QMatrix &m)
1010 qreal tm11 = _m11*m._m11 + _m12*m._m21;
1011 qreal tm12 = _m11*m._m12 + _m12*m._m22;
1012 qreal tm21 = _m21*m._m11 + _m22*m._m21;
1013 qreal tm22 = _m21*m._m12 + _m22*m._m22;
1015 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1016 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1018 _m11 = tm11; _m12 = tm12;
1019 _m21 = tm21; _m22 = tm22;
1020 _dx = tdx; _dy = tdy;
1025 \fn QMatrix QMatrix::operator *(const QMatrix &matrix) const
1027 Returns the result of multiplying this matrix by the given \a
1030 Note that matrix multiplication is not commutative, i.e. a*b !=
1034 QMatrix QMatrix::operator *(const QMatrix &m) const
1036 qreal tm11 = _m11*m._m11 + _m12*m._m21;
1037 qreal tm12 = _m11*m._m12 + _m12*m._m22;
1038 qreal tm21 = _m21*m._m11 + _m22*m._m21;
1039 qreal tm22 = _m21*m._m12 + _m22*m._m22;
1041 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1042 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1043 return QMatrix(tm11, tm12, tm21, tm22, tdx, tdy, true);
1047 Assigns the given \a matrix's values to this matrix.
1049 QMatrix &QMatrix::operator=(const QMatrix &matrix)
1063 Returns the matrix as a QVariant.
1065 QMatrix::operator QVariant() const
1067 return QVariant(QVariant::Matrix, this);
1070 Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m)
1076 /*****************************************************************************
1077 QMatrix stream functions
1078 *****************************************************************************/
1079 #ifndef QT_NO_DATASTREAM
1081 \fn QDataStream &operator<<(QDataStream &stream, const QMatrix &matrix)
1084 Writes the given \a matrix to the given \a stream and returns a
1085 reference to the stream.
1087 \sa {Serializing Qt Data Types}
1090 QDataStream &operator<<(QDataStream &s, const QMatrix &m)
1092 if (s.version() == 1) {
1093 s << (float)m.m11() << (float)m.m12() << (float)m.m21()
1094 << (float)m.m22() << (float)m.dx() << (float)m.dy();
1096 s << double(m.m11())
1107 \fn QDataStream &operator>>(QDataStream &stream, QMatrix &matrix)
1110 Reads the given \a matrix from the given \a stream and returns a
1111 reference to the stream.
1113 \sa {Serializing Qt Data Types}
1116 QDataStream &operator>>(QDataStream &s, QMatrix &m)
1118 if (s.version() == 1) {
1119 float m11, m12, m21, m22, dx, dy;
1120 s >> m11; s >> m12; s >> m21; s >> m22;
1122 m.setMatrix(m11, m12, m21, m22, dx, dy);
1125 double m11, m12, m21, m22, dx, dy;
1132 m.setMatrix(m11, m12, m21, m22, dx, dy);
1136 #endif // QT_NO_DATASTREAM
1138 #ifndef QT_NO_DEBUG_STREAM
1139 QDebug operator<<(QDebug dbg, const QMatrix &m)
1141 dbg.nospace() << "QMatrix("
1143 << " 12=" << m.m12()
1144 << " 21=" << m.m21()
1145 << " 22=" << m.m22()
1154 \fn bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
1159 \brief The qFuzzyCompare function is for comparing two matrices
1160 using a fuzziness factor.
1162 Returns true if \a m1 and \a m2 are equal, allowing for a small
1163 fuzziness factor for floating-point comparisons; false otherwise.