📄 qmatrix.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qdatastream.h"#include "qdebug.h"#include "qmath_p.h"#include "qmatrix.h"#include "qregion.h"#include "qpainterpath.h"#include "qvariant.h"#include <limits.h>/*! \class QMatrix \brief The QMatrix class specifies 2D transformations of a coordinate system. \ingroup multimedia 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 \l {QMatrix#Basic Matrix Operations}{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 \e 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 \e polygon (mapped to the coordinate system defined by \e 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 \e 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. \tableofcontents \section1 Rendering Graphics 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 \e x values increase to the right; \e y values increase downward. For a complete description, see the \l {The Coordinate System}{coordinate system} documentation. QPainter has functions to translate, scale, shear and rotate the coordinate system without using a QMatrix. For example: \table 100% \row \o \inlineimage qmatrix-simpletransformation.png \o \quotefromfile snippets/matrix/matrix.cpp \skipto SimpleTransformation::paintEvent \printuntil } \endtable 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: \table 100% \row \o \inlineimage qmatrix-combinedtransformation.png \o \quotefromfile snippets/matrix/matrix.cpp \skipto CombinedTransformation::paintEvent \printuntil } \endtable \section1 Basic Matrix Operations \image qmatrix-representation.png A QMatrix object contains a 3 x 3 matrix. The \c dx and \c dy elements specify horizontal and vertical translation. The \c m11 and \c m22 elements specify horizontal and vertical scaling. And finally, the \c m21 and \c m12 elements specify horizontal and vertical \e shearing. QMatrix transforms a point in the plane to another point using the following formulas: \code x' = m11*x + m21*y + dx y' = m22*y + m12*x + dy \endcode The point \e (x, y) is the original point, and \e (x', y') is the transformed point. \e (x', y') can be transformed back to \e (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 \c dx and \c dy will move the coordinate system \c dx units along the X axis and \c dy units along the Y axis. Scaling can be done by setting \c m11 and \c m22. For example, setting \c m11 to 2 and \c m22 to 1.5 will double the height and increase the width by 50%. The identity matrix has \c m11 and \c m22 set to 1 (all others are set to 0) mapping a point to itself. Shearing is controlled by \c m12 and \c 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: \table 100% \row \o \inlineimage qmatrix-combinedtransformation.png \o \quotefromfile snippets/matrix/matrix.cpp \skipto BasicOperations::paintEvent \printuntil } \endtable \sa QPainter, {The Coordinate System}, {demos/affine}{Affine Transformations Demo}, {Transformations Example}*/// some defines to inline some code#define MAPDOUBLE(x, y, nx, ny) \{ \ qreal fx = x; \ qreal fy = y; \ nx = _m11*fx + _m21*fy + _dx; \ ny = _m12*fx + _m22*fy + _dy; \}#define MAPINT(x, y, nx, ny) \{ \ qreal fx = x; \ qreal fy = y; \ nx = qRound(_m11*fx + _m21*fy + _dx); \ ny = qRound(_m12*fx + _m22*fy + _dy); \}/***************************************************************************** QMatrix member functions *****************************************************************************//*! Constructs an identity matrix. All elements are set to zero except \c m11 and \c m22 (specifying the scale), which are set to 1. \sa reset()*/QMatrix::QMatrix(){ _m11 = _m22 = 1.0; _m12 = _m21 = _dx = _dy = 0.0;}/*! Constructs a matrix with the elements, \a m11, \a m12, \a m21, \a m22, \a dx and \a dy. \sa setMatrix()*/QMatrix::QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy){ _m11 = m11; _m12 = m12; _m21 = m21; _m22 = m22; _dx = dx; _dy = dy;}/*! Constructs a matrix that is a copy of the given \a matrix. */QMatrix::QMatrix(const QMatrix &matrix){ *this = matrix;}/*! Sets the matrix elements to the specified values, \a m11, \a m12, \a m21, \a m22, \a dx and \a dy. Note that this function replaces the previous values. QMatrix provide the translate(), rotate(), scale() and shear() convenience functions to manipulate the various matrix elements based on the currently defined coordinate system. \sa QMatrix()*/void QMatrix::setMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy){ _m11 = m11; _m12 = m12; _m21 = m21; _m22 = m22; _dx = dx; _dy = dy;}/*! \fn qreal QMatrix::m11() const Returns the horizontal scaling factor. \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*//*! \fn qreal QMatrix::m12() const Returns the vertical shearing factor. \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*//*! \fn qreal QMatrix::m21() const Returns the horizontal shearing factor. \sa shear(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*//*! \fn qreal QMatrix::m22() const Returns the vertical scaling factor. \sa scale(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*//*! \fn qreal QMatrix::dx() const Returns the horizontal translation factor. \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*//*! \fn qreal QMatrix::dy() const Returns the vertical translation factor. \sa translate(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*//*! Maps the given coordinates \a x and \a y into the coordinate system defined by this matrix. The resulting values are put in *\a tx and *\a ty, respectively. The coordinates are transformed using the following formulas: \code x' = m11*x + m21*y + dx y' = m22*y + m12*x + dy \endcode The point (x, y) is the original point, and (x', y') is the transformed point. \sa {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*/void QMatrix::map(qreal x, qreal y, qreal *tx, qreal *ty) const{ MAPDOUBLE(x, y, *tx, *ty);}/*! \overload Maps the given coordinates \a x and \a y into the coordinate system defined by this matrix. The resulting values are put in *\a tx and *\a ty, respectively. Note that the transformed coordinates are rounded to the nearest integer.*/void QMatrix::map(int x, int y, int *tx, int *ty) const{ MAPINT(x, y, *tx, *ty);}QRect QMatrix::mapRect(const QRect &rect) const{ QRect result; if (_m12 == 0.0F && _m21 == 0.0F) { int x = qRound(_m11*rect.x() + _dx); int y = qRound(_m22*rect.y() + _dy); int w = qRound(_m11*rect.width()); int h = qRound(_m22*rect.height()); if (w < 0) { w = -w; x -= w; } if (h < 0) { h = -h; y -= h; } result = QRect(x, y, w, h); } else { // see mapToPolygon for explanations of the algorithm. qreal x0, y0; qreal x, y; MAPDOUBLE(rect.left(), rect.top(), x0, y0); qreal xmin = x0; qreal ymin = y0; qreal xmax = x0; qreal ymax = y0; MAPDOUBLE(rect.right() + 1, rect.top(), x, y); xmin = qMin(xmin, x); ymin = qMin(ymin, y); xmax = qMax(xmax, x); ymax = qMax(ymax, y); MAPDOUBLE(rect.right() + 1, rect.bottom() + 1, x, y); xmin = qMin(xmin, x); ymin = qMin(ymin, y); xmax = qMax(xmax, x); ymax = qMax(ymax, y); MAPDOUBLE(rect.left(), rect.bottom() + 1, x, y); xmin = qMin(xmin, x); ymin = qMin(ymin, y); xmax = qMax(xmax, x); ymax = qMax(ymax, y); qreal w = xmax - xmin; qreal h = ymax - ymin; xmin -= (xmin - x0) / w; ymin -= (ymin - y0) / h; xmax -= (xmax - x0) / w; ymax -= (ymax - y0) / h; result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin)+1, qRound(ymax)-qRound(ymin)+1); } return result;}/*! \fn QRectF QMatrix::mapRect(const QRectF &rectangle) const Creates and returns a QRectF object that is a copy of the given \a rectangle, mapped into the coordinate system defined by this matrix.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -