Qt 4.8
Public Functions | Private Functions | Properties | Friends | Related Functions | List of all members
QMatrix Class Reference

The QMatrix class specifies 2D transformations of a coordinate system. More...

#include <qmatrix.h>

Public Functions

QT_DEPRECATED qreal det () const
 Returns the matrix's determinant. More...
 
qreal determinant () const
 Returns the matrix's determinant. More...
 
qreal dx () const
 Returns the horizontal translation factor. More...
 
qreal dy () const
 Returns the vertical translation factor. More...
 
QMatrix inverted (bool *invertible=0) const
 Returns an inverted copy of this matrix. More...
 
bool isIdentity () const
 Returns true if the matrix is the identity matrix, otherwise returns false. More...
 
bool isInvertible () const
 Returns true if the matrix is invertible, otherwise returns false. More...
 
qreal m11 () const
 Returns the horizontal scaling factor. More...
 
qreal m12 () const
 Returns the vertical shearing factor. More...
 
qreal m21 () const
 Returns the horizontal shearing factor. More...
 
qreal m22 () const
 Returns the vertical scaling factor. More...
 
void map (int x, int y, int *tx, int *ty) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Maps the given coordinates x and y into the coordinate system defined by this matrix. More...
 
void map (qreal x, qreal y, qreal *tx, qreal *ty) const
 Maps the given coordinates x and y into the coordinate system defined by this matrix. More...
 
QPoint map (const QPoint &p) const
 Creates and returns a QPoint object that is a copy of the given point, mapped into the coordinate system defined by this matrix. More...
 
QPointF map (const QPointF &p) const
 Creates and returns a QPointF object that is a copy of the given point, mapped into the coordinate system defined by this matrix. More...
 
QLine map (const QLine &l) const
 Creates and returns a QLine object that is a copy of the given line, mapped into the coordinate system defined by this matrix. More...
 
QLineF map (const QLineF &l) const
 Creates and returns a QLineF object that is a copy of the given line, mapped into the coordinate system defined by this matrix. More...
 
QPolygonF map (const QPolygonF &a) const
 Creates and returns a QPolygonF object that is a copy of the given polygon, mapped into the coordinate system defined by this matrix. More...
 
QPolygon map (const QPolygon &a) const
 Creates and returns a QPolygon object that is a copy of the given polygon, mapped into the coordinate system defined by this matrix. More...
 
QRegion map (const QRegion &r) const
 Creates and returns a QRegion object that is a copy of the given region, mapped into the coordinate system defined by this matrix. More...
 
QPainterPath map (const QPainterPath &p) const
 Creates and returns a QPainterPath object that is a copy of the given path, mapped into the coordinate system defined by this matrix. More...
 
QRect mapRect (const QRect &) const
 Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate system defined by this matrix. More...
 
QRectF mapRect (const QRectF &) const
 Creates and returns a QRectF object that is a copy of the given rectangle, mapped into the coordinate system defined by this matrix. More...
 
QPolygon mapToPolygon (const QRect &r) const
 Creates and returns a QPolygon representation of the given rectangle, mapped into the coordinate system defined by this matrix. More...
 
 operator QVariant () const
 Returns the matrix as a QVariant. More...
 
bool operator!= (const QMatrix &) const
 Returns true if this matrix is not equal to the given matrix, otherwise returns false. More...
 
QMatrix operator* (const QMatrix &o) const
 Returns the result of multiplying this matrix by the given matrix. More...
 
QMatrixoperator*= (const QMatrix &)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the result of multiplying this matrix by the given matrix. More...
 
QMatrixoperator= (const QMatrix &)
 Assigns the given matrix's values to this matrix. More...
 
bool operator== (const QMatrix &) const
 Returns true if this matrix is equal to the given matrix, otherwise returns false. More...
 
 QMatrix (Qt::Initialization)
 
 QMatrix ()
 Constructs an identity matrix. More...
 
 QMatrix (qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
 Constructs a matrix with the elements, m11, m12, m21, m22, dx and dy. More...
 
 QMatrix (const QMatrix &matrix)
 Constructs a matrix that is a copy of the given matrix. More...
 
void reset ()
 Resets the matrix to an identity matrix, i.e. More...
 
QMatrixrotate (qreal a)
 Rotates the coordinate system the given degrees counterclockwise. More...
 
QMatrixscale (qreal sx, qreal sy)
 Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the matrix. More...
 
void setMatrix (qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
 Sets the matrix elements to the specified values, m11, m12, m21, m22, dx and dy. More...
 
QMatrixshear (qreal sh, qreal sv)
 Shears the coordinate system by sh horizontally and sv vertically, and returns a reference to the matrix. More...
 
QMatrixtranslate (qreal dx, qreal dy)
 Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to the matrix. More...
 

Private Functions

 QMatrix (bool)
 
 QMatrix (qreal am11, qreal am12, qreal am21, qreal am22, qreal adx, qreal ady, bool)
 

Properties

qreal _dx
 
qreal _dy
 
qreal _m11
 
qreal _m12
 
qreal _m21
 
qreal _m22
 

Friends

class QTransform
 

Related Functions

(Note that these are not member functions.)

QPoint operator* (const QPoint &point, const QMatrix &matrix)
 
QPointF operator* (const QPointF &point, const QMatrix &matrix)
 
QLineF operator* (const QLineF &line, const QMatrix &matrix)
 
QLine operator* (const QLine &line, const QMatrix &matrix)
 
QPolygonF operator* (const QPolygonF &polygon, const QMatrix &matrix)
 
QPolygon operator* (const QPolygon &polygon, const QMatrix &matrix)
 
QRegion operator* (const QRegion &region, const QMatrix &matrix)
 
QPainterPath operator* (const QPainterPath &path, const QMatrix &matrix)
 
QDataStreamoperator<< (QDataStream &stream, const QMatrix &matrix)
 Writes the given matrix to the given stream and returns a reference to the stream. More...
 
QDataStreamoperator>> (QDataStream &stream, QMatrix &matrix)
 Reads the given matrix from the given stream and returns a reference to the stream. More...
 
bool qFuzzyCompare (const QMatrix &m1, const QMatrix &m2)
 The qFuzzyCompare function is for comparing two matrices using a fuzziness factor. More...
 

Detailed Description

The QMatrix class specifies 2D transformations of a coordinate system.

A matrix specifies how to translate, scale, shear or rotate the coordinate system, and is typically used when rendering graphics. QMatrix, in contrast to QTransform, does not allow perspective transformations. QTransform is the recommended transformation class in Qt.

A QMatrix object can be built using the setMatrix(), scale(), rotate(), translate() and shear() functions. Alternatively, it can be built by applying 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 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 polygon (mapped to the coordinate system defined by 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 this matrix if it is invertible (otherwise it returns the identity matrix). In addition, QMatrix provides the determinant() 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.

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 x values increase to the right; y values increase downward. For a complete description, see the Coordinate System documentation.

QPainter has functions to translate, scale, shear and rotate the coordinate system without using a QMatrix. For example:

qmatrix-simpletransformation.png
void SimpleTransformation::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
painter.drawRect(0, 0, 100, 100);
painter.rotate(45);
painter.setFont(QFont("Helvetica", 24));
painter.setPen(QPen(Qt::black, 1));
painter.drawText(20, 10, "QMatrix");
}

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:

qmatrix-combinedtransformation.png
void CombinedTransformation::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
painter.drawRect(0, 0, 100, 100);
QMatrix matrix;
matrix.translate(50, 50);
matrix.rotate(45);
matrix.scale(0.5, 1.0);
painter.setMatrix(matrix);
painter.setFont(QFont("Helvetica", 24));
painter.setPen(QPen(Qt::black, 1));
painter.drawText(20, 10, "QMatrix");
}

Basic Matrix Operations

qmatrix-representation.png

A QMatrix object contains a 3 x 3 matrix. The dx and dy elements specify horizontal and vertical translation. The m11 and m22 elements specify horizontal and vertical scaling. And finally, the m21 and m12 elements specify horizontal and vertical shearing.

QMatrix transforms a point in the plane to another point using the following formulas:

x' = m11*x + m21*y + dx
y' = m22*y + m12*x + dy

The point (x, y) is the original point, and (x', y') is the transformed point. (x', y') can be transformed back to (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 can 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 dx and dy will move the coordinate system dx units along the X axis and dy units along the Y axis. Scaling can be done by setting m11 and m22. For example, setting m11 to 2 and m22 to 1.5 will double the height and increase the width by 50%. The identity matrix has m11 and m22 set to 1 (all others are set to 0) mapping a point to itself. Shearing is controlled by m12 and 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:

qmatrix-combinedtransformation.png
void BasicOperations::paintEvent(QPaintEvent *)
{
double pi = 3.14;
double a = pi/180 * 45.0;
double sina = sin(a);
double cosa = cos(a);
QMatrix translationMatrix(1, 0, 0, 1, 50.0, 50.0);
QMatrix rotationMatrix(cosa, sina, -sina, cosa, 0, 0);
QMatrix scalingMatrix(0.5, 0, 0, 1.0, 0, 0);
QMatrix matrix;
matrix = scalingMatrix * rotationMatrix * translationMatrix;
QPainter painter(this);
painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
painter.drawRect(0, 0, 100, 100);
painter.setMatrix(matrix);
painter.setFont(QFont("Helvetica", 24));
painter.setPen(QPen(Qt::black, 1));
painter.drawText(20, 10, "QMatrix");
}
See also
QPainter, QTransform, {Coordinate System}, {demos/affine}{Affine Transformations Demo}, {Transformations Example}

Definition at line 61 of file qmatrix.h.

Constructors and Destructors

◆ QMatrix() [1/6]

QMatrix::QMatrix ( Qt::Initialization  )
inlineexplicit
Warning
This function is not part of the public interface.

Definition at line 64 of file qmatrix.h.

64 {}

◆ QMatrix() [2/6]

QMatrix::QMatrix ( )

Constructs an identity matrix.

All elements are set to zero except m11 and m22 (specifying the scale), which are set to 1.

See also
reset()

Definition at line 216 of file qmatrix.cpp.

Referenced by inverted(), and operator*().

217  : _m11(1.)
218  , _m12(0.)
219  , _m21(0.)
220  , _m22(1.)
221  , _dx(0.)
222  , _dy(0.)
223 {
224 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ QMatrix() [3/6]

QMatrix::QMatrix ( qreal  m11,
qreal  m12,
qreal  m21,
qreal  m22,
qreal  dx,
qreal  dy 
)

Constructs a matrix with the elements, m11, m12, m21, m22, dx and dy.

See also
setMatrix()

Definition at line 233 of file qmatrix.cpp.

234  : _m11(m11)
235  , _m12(m12)
236  , _m21(m21)
237  , _m22(m22)
238  , _dx(dx)
239  , _dy(dy)
240 {
241 }
qreal dx() const
Returns the horizontal translation factor.
Definition: qmatrix.h:77
qreal m12() const
Returns the vertical shearing factor.
Definition: qmatrix.h:74
qreal _m22
Definition: qmatrix.h:144
qreal m11() const
Returns the horizontal scaling factor.
Definition: qmatrix.h:73
qreal m21() const
Returns the horizontal shearing factor.
Definition: qmatrix.h:75
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal dy() const
Returns the vertical translation factor.
Definition: qmatrix.h:78
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
qreal m22() const
Returns the vertical scaling factor.
Definition: qmatrix.h:76

◆ QMatrix() [4/6]

QMatrix::QMatrix ( const QMatrix matrix)

Constructs a matrix that is a copy of the given matrix.

Definition at line 247 of file qmatrix.cpp.

248  : _m11(matrix._m11)
249  , _m12(matrix._m12)
250  , _m21(matrix._m21)
251  , _m22(matrix._m22)
252  , _dx(matrix._dx)
253  , _dy(matrix._dy)
254 {
255 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ QMatrix() [5/6]

QMatrix::QMatrix ( bool  )
inlineprivate

Definition at line 128 of file qmatrix.h.

129  : _m11(1.)
130  , _m12(0.)
131  , _m21(0.)
132  , _m22(1.)
133  , _dx(0.)
134  , _dy(0.) {}
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ QMatrix() [6/6]

QMatrix::QMatrix ( qreal  am11,
qreal  am12,
qreal  am21,
qreal  am22,
qreal  adx,
qreal  ady,
bool   
)
inlineprivate

Definition at line 135 of file qmatrix.h.

136  : _m11(am11)
137  , _m12(am12)
138  , _m21(am21)
139  , _m22(am22)
140  , _dx(adx)
141  , _dy(ady) {}
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

Functions

◆ det()

qreal QMatrix::det ( ) const
inline

Returns the matrix's determinant.

See also
determinant()

Definition at line 106 of file qmatrix.h.

106 { return _m11*_m22 - _m12*_m21; }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ determinant()

qreal QMatrix::determinant ( ) const
inline

Returns the matrix's determinant.

Since
4.6

Definition at line 104 of file qmatrix.h.

Referenced by inverted().

104 { return _m11*_m22 - _m12*_m21; }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ dx()

qreal QMatrix::dx ( ) const
inline

Returns the horizontal translation factor.

See also
translate(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 77 of file qmatrix.h.

Referenced by QWindowsXPStylePrivate::drawBackgroundDirectly(), QWin32PrintEngine::drawPixmap(), map(), operator<<(), operator>>(), qFuzzyCompare(), QMatrix4x4::QMatrix4x4(), setMatrix(), and QPdfEnginePrivate::~QPdfEnginePrivate().

77 { return _dx; }
qreal _dx
Definition: qmatrix.h:145

◆ dy()

qreal QMatrix::dy ( ) const
inline

Returns the vertical translation factor.

See also
translate(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 78 of file qmatrix.h.

Referenced by QWindowsXPStylePrivate::drawBackgroundDirectly(), map(), operator<<(), operator>>(), qFuzzyCompare(), QMatrix4x4::QMatrix4x4(), setMatrix(), and QPdfEnginePrivate::~QPdfEnginePrivate().

78 { return _dy; }
qreal _dy
Definition: qmatrix.h:145

◆ inverted()

QMatrix QMatrix::inverted ( bool *  invertible = 0) const

Returns an inverted copy of this matrix.

If the matrix is singular (not invertible), the returned matrix is the identity matrix. If invertible is valid (i.e. not 0), its value is set to true if the matrix is invertible, otherwise it is set to false.

See also
isInvertible()

Definition at line 1066 of file qmatrix.cpp.

Referenced by QTransform::inverted(), QGraphicsViewPrivate::mapToScene(), QPainter::setViewTransformEnabled(), and QPdfEnginePrivate::~QPdfEnginePrivate().

1067 {
1068  qreal dtr = determinant();
1069  if (dtr == 0.0) {
1070  if (invertible)
1071  *invertible = false; // singular matrix
1072  return QMatrix(true);
1073  }
1074  else { // invertible matrix
1075  if (invertible)
1076  *invertible = true;
1077  qreal dinv = 1.0/dtr;
1078  return QMatrix((_m22*dinv), (-_m12*dinv),
1079  (-_m21*dinv), (_m11*dinv),
1080  ((_m21*_dy - _m22*_dx)*dinv),
1081  ((_m12*_dx - _m11*_dy)*dinv),
1082  true);
1083  }
1084 }
QMatrix()
Constructs an identity matrix.
Definition: qmatrix.cpp:216
double qreal
Definition: qglobal.h:1193
qreal _m22
Definition: qmatrix.h:144
qreal determinant() const
Returns the matrix&#39;s determinant.
Definition: qmatrix.h:104
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ isIdentity()

bool QMatrix::isIdentity ( ) const
inline

Returns true if the matrix is the identity matrix, otherwise returns false.

See also
reset()

Definition at line 166 of file qmatrix.h.

Referenced by QSvgGradientStyle::brush(), map(), parseBaseGradient(), parseTransform(), and qt_plastique_draw_gradient().

167 {
168  return qFuzzyIsNull(_m11 - 1) && qFuzzyIsNull(_m22 - 1) && qFuzzyIsNull(_m12)
170 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
qreal _m11
Definition: qmatrix.h:143

◆ isInvertible()

bool QMatrix::isInvertible ( ) const
inline

Returns true if the matrix is invertible, otherwise returns false.

See also
inverted()

Definition at line 103 of file qmatrix.h.

103 { return !qFuzzyIsNull(_m11*_m22 - _m12*_m21); }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _m21
Definition: qmatrix.h:144
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
qreal _m11
Definition: qmatrix.h:143

◆ m11()

qreal QMatrix::m11 ( ) const
inline

Returns the horizontal scaling factor.

See also
scale(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 73 of file qmatrix.h.

Referenced by map(), operator<<(), operator>>(), qFuzzyCompare(), QMatrix4x4::QMatrix4x4(), setMatrix(), and QPdfEnginePrivate::~QPdfEnginePrivate().

73 { return _m11; }
qreal _m11
Definition: qmatrix.h:143

◆ m12()

qreal QMatrix::m12 ( ) const
inline

Returns the vertical shearing factor.

See also
shear(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 74 of file qmatrix.h.

Referenced by map(), operator<<(), operator>>(), qFuzzyCompare(), QMatrix4x4::QMatrix4x4(), setMatrix(), and QPdfEnginePrivate::~QPdfEnginePrivate().

74 { return _m12; }
qreal _m12
Definition: qmatrix.h:143

◆ m21()

qreal QMatrix::m21 ( ) const
inline

Returns the horizontal shearing factor.

See also
shear(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 75 of file qmatrix.h.

Referenced by map(), operator<<(), operator>>(), qFuzzyCompare(), QMatrix4x4::QMatrix4x4(), setMatrix(), and QPdfEnginePrivate::~QPdfEnginePrivate().

75 { return _m21; }
qreal _m21
Definition: qmatrix.h:144

◆ m22()

qreal QMatrix::m22 ( ) const
inline

Returns the vertical scaling factor.

See also
scale(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 76 of file qmatrix.h.

Referenced by map(), operator<<(), operator>>(), qFuzzyCompare(), QMatrix4x4::QMatrix4x4(), setMatrix(), and QPdfEnginePrivate::~QPdfEnginePrivate().

76 { return _m22; }
qreal _m22
Definition: qmatrix.h:144

◆ map() [1/10]

void QMatrix::map ( int  x,
int  y,
int *  tx,
int *  ty 
) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Maps the given coordinates x and y into the coordinate system defined by this matrix.

The resulting values are put in *tx and *ty, respectively. Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 384 of file qmatrix.cpp.

Referenced by map(), QGraphicsViewPrivate::mapToScene(), operator*(), and QTextControl::processEvent().

385 {
386  MAPINT(x, y, *tx, *ty);
387 }
#define MAPINT(x, y, nx, ny)
Definition: qmatrix.cpp:191

◆ map() [2/10]

void QMatrix::map ( qreal  x,
qreal  y,
qreal tx,
qreal ty 
) const

Maps the given coordinates x and y into the coordinate system defined by this matrix.

The resulting values are put in *tx and *ty, respectively.

The coordinates are transformed using the following formulas:

x' = m11*x + m21*y + dx
y' = m22*y + m12*x + dy

The point (x, y) is the original point, and (x', y') is the transformed point.

See also
{QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 368 of file qmatrix.cpp.

369 {
370  MAPDOUBLE(x, y, *tx, *ty);
371 }
#define MAPDOUBLE(x, y, nx, ny)
Definition: qmatrix.cpp:183

◆ map() [3/10]

QPoint QMatrix::map ( const QPoint point) const

Creates and returns a QPoint object that is a copy of the given point, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 525 of file qmatrix.cpp.

526 {
527  qreal fx = p.x();
528  qreal fy = p.y();
529  return QPoint(qRound(_m11*fx + _m21*fy + _dx),
530  qRound(_m12*fx + _m22*fy + _dy));
531 }
double qreal
Definition: qglobal.h:1193
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ map() [4/10]

QPointF QMatrix::map ( const QPointF point) const

Creates and returns a QPointF object that is a copy of the given point, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 552 of file qmatrix.cpp.

553 {
554  qreal fx = point.x();
555  qreal fy = point.y();
556  return QPointF(_m11*fx + _m21*fy + _dx, _m12*fx + _m22*fy + _dy);
557 }
double qreal
Definition: qglobal.h:1193
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal _m22
Definition: qmatrix.h:144
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ map() [5/10]

QLine QMatrix::map ( const QLine line) const

Creates and returns a QLine object that is a copy of the given line, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 615 of file qmatrix.cpp.

616 {
617  return QLine(map(line.p1()), map(line.p2()));
618 }
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
QPoint p1() const
Returns the line&#39;s start point.
Definition: qline.h:132
QPoint p2() const
Returns the line&#39;s end point.
Definition: qline.h:137
void map(int x, int y, int *tx, int *ty) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qmatrix.cpp:384

◆ map() [6/10]

QLineF QMatrix::map ( const QLineF line) const

Creates and returns a QLineF object that is a copy of the given line, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 599 of file qmatrix.cpp.

600 {
601  return QLineF(map(line.p1()), map(line.p2()));
602 }
QPointF p1() const
Returns the line&#39;s start point.
Definition: qline.h:314
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
void map(int x, int y, int *tx, int *ty) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qmatrix.cpp:384
QPointF p2() const
Returns the line&#39;s end point.
Definition: qline.h:319

◆ map() [7/10]

QPolygonF QMatrix::map ( const QPolygonF polygon) const

Creates and returns a QPolygonF object that is a copy of the given polygon, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 662 of file qmatrix.cpp.

663 {
664  int size = a.size();
665  int i;
666  QPolygonF p(size);
667  const QPointF *da = a.constData();
668  QPointF *dp = p.data();
669  for(i = 0; i < size; i++) {
670  MAPDOUBLE(da[i].xp, da[i].yp, dp[i].xp, dp[i].yp);
671  }
672  return p;
673 }
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
#define MAPDOUBLE(x, y, nx, ny)
Definition: qmatrix.cpp:183
long ASN1_INTEGER_get ASN1_INTEGER * a
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134

◆ map() [8/10]

QPolygon QMatrix::map ( const QPolygon polygon) const

Creates and returns a QPolygon object that is a copy of the given polygon, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 638 of file qmatrix.cpp.

639 {
640  int size = a.size();
641  int i;
642  QPolygon p(size);
643  const QPoint *da = a.constData();
644  QPoint *dp = p.data();
645  for(i = 0; i < size; i++) {
646  MAPINT(da[i].x(), da[i].y(), dp[i].rx(), dp[i].ry());
647  }
648  return p;
649 }
long ASN1_INTEGER_get ASN1_INTEGER * a
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
#define MAPINT(x, y, nx, ny)
Definition: qmatrix.cpp:191
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53

◆ map() [9/10]

QRegion QMatrix::map ( const QRegion region) const

Creates and returns a QRegion object that is a copy of the given region, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Calling this method can be rather expensive if rotations or shearing are used.

Definition at line 712 of file qmatrix.cpp.

713 {
714  if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) { // translate or identity
715  if (_dx == 0.0 && _dy == 0.0) // Identity
716  return r;
717  QRegion copy(r);
718  copy.translate(qRound(_dx), qRound(_dy));
719  return copy;
720  }
721 
723  return p.toFillPolygon().toPolygon();
724 }
Q_AUTOTEST_EXPORT QPainterPath qt_regionToPath(const QRegion &region)
Definition: qregion.cpp:1160
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
qreal _m22
Definition: qmatrix.h:144
QPolygon toPolygon() const
Creates and returns a QPolygon by converting each QPointF to a QPoint.
Definition: qpolygon.cpp:772
QPolygonF toFillPolygon(const QMatrix &matrix=QMatrix()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
void map(int x, int y, int *tx, int *ty) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qmatrix.cpp:384
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ map() [10/10]

QPainterPath QMatrix::map ( const QPainterPath path) const

Creates and returns a QPainterPath object that is a copy of the given path, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 745 of file qmatrix.cpp.

746 {
747  if (path.isEmpty())
748  return QPainterPath();
749 
750  QPainterPath copy = path;
751 
752  // Translate or identity
753  if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {
754 
755  // Translate
756  if (_dx != 0.0 || _dy != 0.0) {
757  copy.detach();
758  for (int i=0; i<path.elementCount(); ++i) {
759  QPainterPath::Element &e = copy.d_ptr->elements[i];
760  e.x += _dx;
761  e.y += _dy;
762  }
763  }
764 
765  // Full xform
766  } else {
767  copy.detach();
768  for (int i=0; i<path.elementCount(); ++i) {
769  QPainterPath::Element &e = copy.d_ptr->elements[i];
770  qreal fx = e.x, fy = e.y;
771  e.x = _m11*fx + _m21*fy + _dx;
772  e.y = _m12*fx + _m22*fy + _dy;
773  }
774  }
775 
776  return copy;
777 }
The QPainterPath::Element class specifies the position and type of a subpath.
Definition: qpainterpath.h:77
bool isEmpty() const
Returns true if either there are no elements in this path, or if the only element is a MoveToElement;...
Definition: qpainterpath.h:392
double qreal
Definition: qglobal.h:1193
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
qreal y
the y coordinate of the element&#39;s position.
Definition: qpainterpath.h:80
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
QVector< QPainterPath::Element > elements
Definition: qpainterpath.h:254
qreal x
the x coordinate of the element&#39;s position.
Definition: qpainterpath.h:79
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
int elementCount() const
Returns the number of path elements in the painter path.
Definition: qpainterpath.h:397
QScopedPointer< QPainterPathPrivate, QPainterPathPrivateDeleter > d_ptr
Definition: qpainterpath.h:211

◆ mapRect() [1/2]

QRect QMatrix::mapRect ( const QRect rectangle) const

Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate system defined by this matrix.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 389 of file qmatrix.cpp.

Referenced by QCommonStyle::drawControl(), QWindowsStyle::drawControl(), and QPainter::setViewTransformEnabled().

390 {
391  QRect result;
392  if (_m12 == 0.0F && _m21 == 0.0F) {
393  int x = qRound(_m11*rect.x() + _dx);
394  int y = qRound(_m22*rect.y() + _dy);
395  int w = qRound(_m11*rect.width());
396  int h = qRound(_m22*rect.height());
397  if (w < 0) {
398  w = -w;
399  x -= w;
400  }
401  if (h < 0) {
402  h = -h;
403  y -= h;
404  }
405  result = QRect(x, y, w, h);
406  } else {
407  // see mapToPolygon for explanations of the algorithm.
408  qreal x0, y0;
409  qreal x, y;
410  MAPDOUBLE(rect.left(), rect.top(), x0, y0);
411  qreal xmin = x0;
412  qreal ymin = y0;
413  qreal xmax = x0;
414  qreal ymax = y0;
415  MAPDOUBLE(rect.right() + 1, rect.top(), x, y);
416  xmin = qMin(xmin, x);
417  ymin = qMin(ymin, y);
418  xmax = qMax(xmax, x);
419  ymax = qMax(ymax, y);
420  MAPDOUBLE(rect.right() + 1, rect.bottom() + 1, x, y);
421  xmin = qMin(xmin, x);
422  ymin = qMin(ymin, y);
423  xmax = qMax(xmax, x);
424  ymax = qMax(ymax, y);
425  MAPDOUBLE(rect.left(), rect.bottom() + 1, x, y);
426  xmin = qMin(xmin, x);
427  ymin = qMin(ymin, y);
428  xmax = qMax(xmax, x);
429  ymax = qMax(ymax, y);
430  result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin), qRound(ymax)-qRound(ymin));
431  }
432  return result;
433 }
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define MAPDOUBLE(x, y, nx, ny)
Definition: qmatrix.cpp:183
qreal _m22
Definition: qmatrix.h:144
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ mapRect() [2/2]

QRectF QMatrix::mapRect ( const QRectF rectangle) const

Creates and returns a QRectF object that is a copy of the given rectangle, mapped into the coordinate system defined by this matrix.

The rectangle's coordinates are transformed using the following formulas:

x' = m11*x + m21*y + dx
y' = m22*y + m12*x + dy

If rotation or shearing has been specified, this function returns the bounding rectangle. To retrieve the exact region the given rectangle maps to, use the mapToPolygon() function instead.

See also
mapToPolygon(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 457 of file qmatrix.cpp.

458 {
459  QRectF result;
460  if (_m12 == 0.0F && _m21 == 0.0F) {
461  qreal x = _m11*rect.x() + _dx;
462  qreal y = _m22*rect.y() + _dy;
463  qreal w = _m11*rect.width();
464  qreal h = _m22*rect.height();
465  if (w < 0) {
466  w = -w;
467  x -= w;
468  }
469  if (h < 0) {
470  h = -h;
471  y -= h;
472  }
473  result = QRectF(x, y, w, h);
474  } else {
475  qreal x0, y0;
476  qreal x, y;
477  MAPDOUBLE(rect.x(), rect.y(), x0, y0);
478  qreal xmin = x0;
479  qreal ymin = y0;
480  qreal xmax = x0;
481  qreal ymax = y0;
482  MAPDOUBLE(rect.x() + rect.width(), rect.y(), x, y);
483  xmin = qMin(xmin, x);
484  ymin = qMin(ymin, y);
485  xmax = qMax(xmax, x);
486  ymax = qMax(ymax, y);
487  MAPDOUBLE(rect.x() + rect.width(), rect.y() + rect.height(), x, y);
488  xmin = qMin(xmin, x);
489  ymin = qMin(ymin, y);
490  xmax = qMax(xmax, x);
491  ymax = qMax(ymax, y);
492  MAPDOUBLE(rect.x(), rect.y() + rect.height(), x, y);
493  xmin = qMin(xmin, x);
494  ymin = qMin(ymin, y);
495  xmax = qMax(xmax, x);
496  ymax = qMax(ymax, y);
497  result = QRectF(xmin, ymin, xmax-xmin, ymax - ymin);
498  }
499  return result;
500 }
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define MAPDOUBLE(x, y, nx, ny)
Definition: qmatrix.cpp:183
qreal _m22
Definition: qmatrix.h:144
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ mapToPolygon()

QPolygon QMatrix::mapToPolygon ( const QRect rectangle) const

Creates and returns a QPolygon representation of the given rectangle, mapped into the coordinate system defined by this matrix.

The rectangle's coordinates are transformed using the following formulas:

x' = m11*x + m21*y + dx
y' = m22*y + m12*x + dy

Polygons and rectangles behave slightly differently when transformed (due to integer rounding), so matrix.map(QPolygon(rectangle)) is not always the same as matrix.mapToPolygon(rectangle).

See also
mapRect(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 841 of file qmatrix.cpp.

Referenced by map().

842 {
843  QPolygon a(4);
844  qreal x[4], y[4];
845  if (_m12 == 0.0F && _m21 == 0.0F) {
846  x[0] = _m11*rect.x() + _dx;
847  y[0] = _m22*rect.y() + _dy;
848  qreal w = _m11*rect.width();
849  qreal h = _m22*rect.height();
850  if (w < 0) {
851  w = -w;
852  x[0] -= w;
853  }
854  if (h < 0) {
855  h = -h;
856  y[0] -= h;
857  }
858  x[1] = x[0]+w;
859  x[2] = x[1];
860  x[3] = x[0];
861  y[1] = y[0];
862  y[2] = y[0]+h;
863  y[3] = y[2];
864  } else {
865  qreal right = rect.x() + rect.width();
866  qreal bottom = rect.y() + rect.height();
867  MAPDOUBLE(rect.x(), rect.y(), x[0], y[0]);
868  MAPDOUBLE(right, rect.y(), x[1], y[1]);
869  MAPDOUBLE(right, bottom, x[2], y[2]);
870  MAPDOUBLE(rect.x(), bottom, x[3], y[3]);
871  }
872 #if 0
873  int i;
874  for(i = 0; i< 4; i++)
875  qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i]));
876  qDebug("width=%f, height=%f", qSqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0])),
877  qSqrt((x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3])));
878 #endif
879  // all coordinates are correctly, tranform to a pointarray
880  // (rounding to the next integer)
881  a.setPoints(4, qRound(x[0]), qRound(y[0]),
882  qRound(x[1]), qRound(y[1]),
883  qRound(x[2]), qRound(y[2]),
884  qRound(x[3]), qRound(y[3]));
885  return a;
886 }
double qreal
Definition: qglobal.h:1193
#define MAPDOUBLE(x, y, nx, ny)
Definition: qmatrix.cpp:183
long ASN1_INTEGER_get ASN1_INTEGER * a
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
qreal _m22
Definition: qmatrix.h:144
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
Q_CORE_EXPORT void qDebug(const char *,...)
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
qreal qSqrt(qreal v)
Definition: qmath.h:205
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ operator QVariant()

QMatrix::operator QVariant ( ) const

Returns the matrix as a QVariant.

Since
4.2

Definition at line 1195 of file qmatrix.cpp.

1196 {
1197  return QVariant(QVariant::Matrix, this);
1198 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92

◆ operator!=()

bool QMatrix::operator!= ( const QMatrix matrix) const

Returns true if this matrix is not equal to the given matrix, otherwise returns false.

Definition at line 1117 of file qmatrix.cpp.

1118 {
1119  return _m11 != m._m11 ||
1120  _m12 != m._m12 ||
1121  _m21 != m._m21 ||
1122  _m22 != m._m22 ||
1123  _dx != m._dx ||
1124  _dy != m._dy;
1125 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ operator*()

QMatrix QMatrix::operator* ( const QMatrix matrix) const

Returns the result of multiplying this matrix by the given matrix.

Note that matrix multiplication is not commutative, i.e. a*b != b*a.

Definition at line 1161 of file qmatrix.cpp.

1162 {
1163  qreal tm11 = _m11*m._m11 + _m12*m._m21;
1164  qreal tm12 = _m11*m._m12 + _m12*m._m22;
1165  qreal tm21 = _m21*m._m11 + _m22*m._m21;
1166  qreal tm22 = _m21*m._m12 + _m22*m._m22;
1167 
1168  qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1169  qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1170  return QMatrix(tm11, tm12, tm21, tm22, tdx, tdy, true);
1171 }
QMatrix()
Constructs an identity matrix.
Definition: qmatrix.cpp:216
double qreal
Definition: qglobal.h:1193
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ operator*=()

QMatrix & QMatrix::operator*= ( const QMatrix matrix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the result of multiplying this matrix by the given matrix.

Definition at line 1135 of file qmatrix.cpp.

1136 {
1137  qreal tm11 = _m11*m._m11 + _m12*m._m21;
1138  qreal tm12 = _m11*m._m12 + _m12*m._m22;
1139  qreal tm21 = _m21*m._m11 + _m22*m._m21;
1140  qreal tm22 = _m21*m._m12 + _m22*m._m22;
1141 
1142  qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;
1143  qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;
1144 
1145  _m11 = tm11; _m12 = tm12;
1146  _m21 = tm21; _m22 = tm22;
1147  _dx = tdx; _dy = tdy;
1148  return *this;
1149 }
double qreal
Definition: qglobal.h:1193
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ operator=()

QMatrix & QMatrix::operator= ( const QMatrix matrix)

Assigns the given matrix's values to this matrix.

Definition at line 1176 of file qmatrix.cpp.

1177 {
1178  _m11 = matrix._m11;
1179  _m12 = matrix._m12;
1180  _m21 = matrix._m21;
1181  _m22 = matrix._m22;
1182  _dx = matrix._dx;
1183  _dy = matrix._dy;
1184  return *this;
1185 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ operator==()

bool QMatrix::operator== ( const QMatrix matrix) const

Returns true if this matrix is equal to the given matrix, otherwise returns false.

Definition at line 1097 of file qmatrix.cpp.

1098 {
1099  return _m11 == m._m11 &&
1100  _m12 == m._m12 &&
1101  _m21 == m._m21 &&
1102  _m22 == m._m22 &&
1103  _dx == m._dx &&
1104  _dy == m._dy;
1105 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ reset()

void QMatrix::reset ( )

Resets the matrix to an identity matrix, i.e.

all elements are set to zero, except m11 and m22 (specifying the scale) which are set to 1.

See also
QMatrix(), isIdentity(), {QMatrix::Basic Matrix Operations}{Basic Matrix Operations}

Definition at line 897 of file qmatrix.cpp.

Referenced by QPainterState::init().

898 {
899  _m11 = _m22 = 1.0;
900  _m12 = _m21 = _dx = _dy = 0.0;
901 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ rotate()

QMatrix & QMatrix::rotate ( qreal  degrees)

Rotates the coordinate system the given degrees counterclockwise.

Note that if you apply a QMatrix to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards.

Returns a reference to the matrix.

See also
setMatrix()

Definition at line 990 of file qmatrix.cpp.

Referenced by QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QCommonStyle::drawControl(), QWindowsStyle::drawControl(), QMacStyle::drawPrimitive(), QGraphicsItemAnimation::matrixAt(), parseTransformationMatrix(), QTiffHandler::read(), and rotateTabPainter().

991 {
992  qreal sina = 0;
993  qreal cosa = 0;
994  if (a == 90. || a == -270.)
995  sina = 1.;
996  else if (a == 270. || a == -90.)
997  sina = -1.;
998  else if (a == 180.)
999  cosa = -1.;
1000  else{
1001  qreal b = deg2rad*a; // convert to radians
1002  sina = qSin(b); // fast and convenient
1003  cosa = qCos(b);
1004  }
1005  qreal tm11 = cosa*_m11 + sina*_m21;
1006  qreal tm12 = cosa*_m12 + sina*_m22;
1007  qreal tm21 = -sina*_m11 + cosa*_m21;
1008  qreal tm22 = -sina*_m12 + cosa*_m22;
1009  _m11 = tm11; _m12 = tm12;
1010  _m21 = tm21; _m22 = tm22;
1011  return *this;
1012 }
double qreal
Definition: qglobal.h:1193
long ASN1_INTEGER_get ASN1_INTEGER * a
qreal _m22
Definition: qmatrix.h:144
qreal qSin(qreal v)
Definition: qmath.h:93
qreal _m12
Definition: qmatrix.h:143
const qreal deg2rad
Definition: qmatrix.cpp:970
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
qreal qCos(qreal v)
Definition: qmath.h:109

◆ scale()

QMatrix & QMatrix::scale ( qreal  sx,
qreal  sy 
)

Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the matrix.

See also
setMatrix()

Definition at line 941 of file qmatrix.cpp.

Referenced by QGraphicsItemAnimation::matrixAt(), and parseTransformationMatrix().

942 {
943  _m11 *= sx;
944  _m12 *= sx;
945  _m21 *= sy;
946  _m22 *= sy;
947  return *this;
948 }
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ setMatrix()

void QMatrix::setMatrix ( qreal  m11,
qreal  m12,
qreal  m21,
qreal  m22,
qreal  dx,
qreal  dy 
)

Sets the matrix elements to the specified values, m11, m12, m21, m22, dx and 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.

See also
QMatrix()

Definition at line 269 of file qmatrix.cpp.

Referenced by operator>>().

270 {
271  _m11 = m11;
272  _m12 = m12;
273  _m21 = m21;
274  _m22 = m22;
275  _dx = dx;
276  _dy = dy;
277 }
qreal dx() const
Returns the horizontal translation factor.
Definition: qmatrix.h:77
qreal m12() const
Returns the vertical shearing factor.
Definition: qmatrix.h:74
qreal _m22
Definition: qmatrix.h:144
qreal m11() const
Returns the horizontal scaling factor.
Definition: qmatrix.h:73
qreal m21() const
Returns the horizontal shearing factor.
Definition: qmatrix.h:75
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal dy() const
Returns the vertical translation factor.
Definition: qmatrix.h:78
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143
qreal m22() const
Returns the vertical scaling factor.
Definition: qmatrix.h:76

◆ shear()

QMatrix & QMatrix::shear ( qreal  sh,
qreal  sv 
)

Shears the coordinate system by sh horizontally and sv vertically, and returns a reference to the matrix.

See also
setMatrix()

Definition at line 957 of file qmatrix.cpp.

Referenced by QGraphicsItemAnimation::matrixAt(), and parseTransformationMatrix().

958 {
959  qreal tm11 = sv*_m21;
960  qreal tm12 = sv*_m22;
961  qreal tm21 = sh*_m11;
962  qreal tm22 = sh*_m12;
963  _m11 += tm11;
964  _m12 += tm12;
965  _m21 += tm21;
966  _m22 += tm22;
967  return *this;
968 }
double qreal
Definition: qglobal.h:1193
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

◆ translate()

QMatrix & QMatrix::translate ( qreal  dx,
qreal  dy 
)

Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to the matrix.

See also
setMatrix()

Definition at line 922 of file qmatrix.cpp.

Referenced by QCommonStyle::drawControl(), QWindowsStyle::drawControl(), QMacStyle::drawPrimitive(), QGraphicsItemAnimation::matrixAt(), parseTransformationMatrix(), QTextControl::processEvent(), and rotateTabPainter().

923 {
924  _dx += dx*_m11 + dy*_m21;
925  _dy += dy*_m22 + dx*_m12;
926  return *this;
927 }
qreal dx() const
Returns the horizontal translation factor.
Definition: qmatrix.h:77
qreal _m22
Definition: qmatrix.h:144
qreal _m12
Definition: qmatrix.h:143
qreal _dx
Definition: qmatrix.h:145
qreal dy() const
Returns the vertical translation factor.
Definition: qmatrix.h:78
qreal _dy
Definition: qmatrix.h:145
qreal _m21
Definition: qmatrix.h:144
qreal _m11
Definition: qmatrix.h:143

Friends and Related Functions

◆ operator*() [1/8]

QPoint operator* ( const QPoint point,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({point}).

See also
QMatrix::map()

Definition at line 150 of file qmatrix.h.

151 { return m.map(p); }

◆ operator*() [2/8]

QPointF operator* ( const QPointF point,
const QMatrix matrix 
)
related

Same as {matrix}.map({point}).

See also
QMatrix::map()

Definition at line 152 of file qmatrix.h.

153 { return m.map(p); }

◆ operator*() [3/8]

QLineF operator* ( const QLineF line,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({line}).

See also
QMatrix::map()

Definition at line 154 of file qmatrix.h.

155 { return m.map(l); }
QFactoryLoader * l

◆ operator*() [4/8]

QLine operator* ( const QLine line,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({line}).

See also
QMatrix::map()

Definition at line 156 of file qmatrix.h.

157 { return m.map(l); }
QFactoryLoader * l

◆ operator*() [5/8]

QPolygonF operator* ( const QPolygonF polygon,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({polygon}).

See also
QMatrix::map()

Definition at line 160 of file qmatrix.h.

161 { return m.map(a); }
long ASN1_INTEGER_get ASN1_INTEGER * a

◆ operator*() [6/8]

QPolygon operator* ( const QPolygon polygon,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({polygon}).

See also
QMatrix::map()

Definition at line 158 of file qmatrix.h.

159 { return m.map(a); }
long ASN1_INTEGER_get ASN1_INTEGER * a

◆ operator*() [7/8]

QRegion operator* ( const QRegion region,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({region}).

See also
QMatrix::map()

Definition at line 162 of file qmatrix.h.

163 { return m.map(r); }

◆ operator*() [8/8]

QPainterPath operator* ( const QPainterPath path,
const QMatrix matrix 
)
related

This is the same as {matrix}.map({path}).

See also
QMatrix::map()

Definition at line 1200 of file qmatrix.cpp.

1201 {
1202  return m.map(p);
1203 }

◆ operator<<()

QDataStream & operator<< ( QDataStream stream,
const QMatrix matrix 
)
related

Writes the given matrix to the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 1223 of file qmatrix.cpp.

1224 {
1225  if (s.version() == 1) {
1226  s << (float)m.m11() << (float)m.m12() << (float)m.m21()
1227  << (float)m.m22() << (float)m.dx() << (float)m.dy();
1228  } else {
1229  s << double(m.m11())
1230  << double(m.m12())
1231  << double(m.m21())
1232  << double(m.m22())
1233  << double(m.dx())
1234  << double(m.dy());
1235  }
1236  return s;
1237 }

◆ operator>>()

QDataStream & operator>> ( QDataStream stream,
QMatrix matrix 
)
related

Reads the given matrix from the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 1252 of file qmatrix.cpp.

1253 {
1254  if (s.version() == 1) {
1255  float m11, m12, m21, m22, dx, dy;
1256  s >> m11; s >> m12; s >> m21; s >> m22;
1257  s >> dx; s >> dy;
1258  m.setMatrix(m11, m12, m21, m22, dx, dy);
1259  }
1260  else {
1261  double m11, m12, m21, m22, dx, dy;
1262  s >> m11;
1263  s >> m12;
1264  s >> m21;
1265  s >> m22;
1266  s >> dx;
1267  s >> dy;
1268  m.setMatrix(m11, m12, m21, m22, dx, dy);
1269  }
1270  return s;
1271 }
qreal dx() const
Returns the horizontal translation factor.
Definition: qmatrix.h:77
qreal m12() const
Returns the vertical shearing factor.
Definition: qmatrix.h:74
qreal m11() const
Returns the horizontal scaling factor.
Definition: qmatrix.h:73
qreal m21() const
Returns the horizontal shearing factor.
Definition: qmatrix.h:75
qreal dy() const
Returns the vertical translation factor.
Definition: qmatrix.h:78
qreal m22() const
Returns the vertical scaling factor.
Definition: qmatrix.h:76

◆ qFuzzyCompare()

bool qFuzzyCompare ( const QMatrix m1,
const QMatrix m2 
)
related

The qFuzzyCompare function is for comparing two matrices using a fuzziness factor.

Returns true if m1 and m2 are equal, allowing for a small fuzziness factor for floating-point comparisons; false otherwise.

Since
4.6

Definition at line 172 of file qmatrix.h.

Referenced by qt_polygon_isect_line().

173 {
174  return qFuzzyCompare(m1.m11(), m2.m11())
175  && qFuzzyCompare(m1.m12(), m2.m12())
176  && qFuzzyCompare(m1.m21(), m2.m21())
177  && qFuzzyCompare(m1.m22(), m2.m22())
178  && qFuzzyCompare(m1.dx(), m2.dx())
179  && qFuzzyCompare(m1.dy(), m2.dy());
180 }
qreal dx() const
Returns the horizontal translation factor.
Definition: qmatrix.h:77
qreal m12() const
Returns the vertical shearing factor.
Definition: qmatrix.h:74
qreal m11() const
Returns the horizontal scaling factor.
Definition: qmatrix.h:73
qreal m21() const
Returns the horizontal shearing factor.
Definition: qmatrix.h:75
qreal dy() const
Returns the vertical translation factor.
Definition: qmatrix.h:78
bool qFuzzyCompare(const QMatrix &m1, const QMatrix &m2)
The qFuzzyCompare function is for comparing two matrices using a fuzziness factor.
Definition: qmatrix.h:172
qreal m22() const
Returns the vertical scaling factor.
Definition: qmatrix.h:76

◆ QTransform

friend class QTransform
friend

Definition at line 142 of file qmatrix.h.

Properties

◆ _dx

qreal QMatrix::_dx
private

◆ _dy

qreal QMatrix::_dy
private

◆ _m11

qreal QMatrix::_m11
private

◆ _m12

qreal QMatrix::_m12
private

◆ _m21

qreal QMatrix::_m21
private

◆ _m22

qreal QMatrix::_m22
private

The documentation for this class was generated from the following files: