QMatrix Class Reference

#include <qmatrix.h>

List of all members.


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.

A QMatrix object can be built using the setMatrix(), scale(), rotate(), translate() and shear() functions. Alternatively, it can be built by applying {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 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 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.

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 \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 @section 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} Definition at line 41 of file qmatrix.h.

Public Member Functions

 QMatrix ()
 QMatrix (qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
 QMatrix (const QMatrix &matrix)
void setMatrix (qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
qreal m11 () const
qreal m12 () const
qreal m21 () const
qreal m22 () const
qreal dx () const
qreal dy () const
void map (int x, int y, int *tx, int *ty) const
void map (qreal x, qreal y, qreal *tx, qreal *ty) const
QRect mapRect (const QRect &) const
QRectF mapRect (const QRectF &) const
QPoint map (const QPoint &p) const
QPointF map (const QPointF &p) const
QLine map (const QLine &l) const
QLineF map (const QLineF &l) const
QPolygonF map (const QPolygonF &a) const
QPolygon map (const QPolygon &a) const
QRegion map (const QRegion &r) const
QPainterPath map (const QPainterPath &p) const
QPolygon mapToPolygon (const QRect &r) const
void reset ()
bool isIdentity () const
QMatrixtranslate (qreal dx, qreal dy)
QMatrixscale (qreal sx, qreal sy)
QMatrixshear (qreal sh, qreal sv)
QMatrixrotate (qreal a)
bool isInvertible () const
qreal det () const
QMatrix inverted (bool *invertible=0) const
bool operator== (const QMatrix &) const
bool operator!= (const QMatrix &) const
QMatrixoperator *= (const QMatrix &)
QMatrix operator * (const QMatrix &o) const
QMatrixoperator= (const QMatrix &)
 operator QVariant () const

Private Attributes

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

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)
QDataStreamoperator>> (QDataStream &stream, QMatrix &matrix)


Constructor & Destructor Documentation

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 199 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, and _m22.

00200 {
00201     _m11 = _m22 = 1.0;
00202     _m12 = _m21 = _dx = _dy = 0.0;
00203 }

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 212 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, and _m22.

00214 {
00215     _m11 = m11;         _m12 = m12;
00216     _m21 = m21;         _m22 = m22;
00217     _dx         = dx;         _dy  = dy;
00218 }

QMatrix::QMatrix ( const QMatrix matrix  ) 

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

Definition at line 224 of file qmatrix.cpp.

00225 {
00226     *this = matrix;
00227 }


Member Function Documentation

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 241 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, and _m22.

Referenced by QPainter::restore(), and QImage::trueMatrix().

00243 {
00244     _m11 = m11;         _m12 = m12;
00245     _m21 = m21;         _m22 = m22;
00246     _dx         = dx;         _dy  = dy;
00247 }

qreal QMatrix::m11 (  )  const [inline]

Returns the horizontal scaling factor.

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

Definition at line 52 of file qmatrix.h.

Referenced by QPdfEnginePrivate::addBrushPattern(), Q3SVGPaintEnginePrivate::applyStyle(), QPainterPrivate::draw_helper(), Q3Canvas::drawViewArea(), QPdf::generateMatrix(), QFontEngine::getGlyphPositions(), Q3SVGPaintEnginePrivate::play(), qt_xForm_helper(), QSpanData::setupMatrix(), QImage::transformed(), QImage::trueMatrix(), QX11PaintEngine::updateMatrix(), QRasterPaintEngine::updateMatrix(), and QPainterPrivate::updateMatrix().

00052 { return _m11; }

qreal QMatrix::m12 (  )  const [inline]

Returns the vertical shearing factor.

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

Definition at line 53 of file qmatrix.h.

Referenced by QPdfEnginePrivate::addBrushPattern(), Q3Canvas::drawViewArea(), QPdf::generateMatrix(), QFontEngine::getGlyphPositions(), qt_xForm_helper(), QSpanData::setupMatrix(), QImage::transformed(), QImage::trueMatrix(), QX11PaintEngine::updateMatrix(), QRasterPaintEngine::updateMatrix(), and QPainterPrivate::updateMatrix().

00053 { return _m12; }

qreal QMatrix::m21 (  )  const [inline]

Returns the horizontal shearing factor.

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

Definition at line 54 of file qmatrix.h.

Referenced by QPdfEnginePrivate::addBrushPattern(), Q3Canvas::drawViewArea(), QPdf::generateMatrix(), QFontEngine::getGlyphPositions(), qt_xForm_helper(), QSpanData::setupMatrix(), QImage::transformed(), QImage::trueMatrix(), QX11PaintEngine::updateMatrix(), QRasterPaintEngine::updateMatrix(), and QPainterPrivate::updateMatrix().

00054 { return _m21; }

qreal QMatrix::m22 (  )  const [inline]

Returns the vertical scaling factor.

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

Definition at line 55 of file qmatrix.h.

Referenced by QPdfEnginePrivate::addBrushPattern(), Q3SVGPaintEnginePrivate::applyStyle(), QPainterPrivate::draw_helper(), Q3Canvas::drawViewArea(), QPdf::generateMatrix(), QFontEngine::getGlyphPositions(), Q3SVGPaintEnginePrivate::play(), qt_xForm_helper(), QSpanData::setupMatrix(), QImage::transformed(), QImage::trueMatrix(), QX11PaintEngine::updateMatrix(), QRasterPaintEngine::updateMatrix(), and QPainterPrivate::updateMatrix().

00055 { return _m22; }

qreal QMatrix::dx (  )  const [inline]

Returns the horizontal translation factor.

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

Definition at line 56 of file qmatrix.h.

Referenced by QPdfEnginePrivate::addBrushPattern(), QWindowsXPStylePrivate::drawBackgroundDirectly(), QX11PaintEnginePrivate::fillPolygon_translated(), QPdf::generateMatrix(), QFontEngine::getGlyphPositions(), qt_xForm_helper(), QSpanData::setupMatrix(), QX11PaintEnginePrivate::strokePolygon_translated(), QX11PaintEngine::updateMatrix(), and QPainterPrivate::updateMatrix().

00056 { return _dx; }

qreal QMatrix::dy (  )  const [inline]

Returns the vertical translation factor.

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

Definition at line 57 of file qmatrix.h.

Referenced by QPdfEnginePrivate::addBrushPattern(), QWindowsXPStylePrivate::drawBackgroundDirectly(), QX11PaintEnginePrivate::fillPolygon_translated(), QPdf::generateMatrix(), QFontEngine::getGlyphPositions(), qt_xForm_helper(), QSpanData::setupMatrix(), QX11PaintEnginePrivate::strokePolygon_translated(), QX11PaintEngine::updateMatrix(), and QPainterPrivate::updateMatrix().

00057 { return _dy; }

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 339 of file qmatrix.cpp.

References MAPINT.

Referenced by CannonField::barrelHit(), Q3Canvas::drawViewArea(), QGraphicsView::fitInView(), QPdf::generatePath(), QGraphicsScene::inputMethodQuery(), map(), QGraphicsItem::mapFromParent(), QGraphicsItem::mapFromScene(), QGraphicsView::mapFromScene(), QGraphicsItem::mapToParent(), QGraphicsItem::mapToScene(), QGraphicsItem::mouseMoveEvent(), QTextControl::processEvent(), QImage::transformed(), QImage::trueMatrix(), Q3Canvas::update(), and Q3CanvasView::updateContentsSize().

00340 {
00341     MAPINT(x, y, *tx, *ty);
00342 }

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 323 of file qmatrix.cpp.

References MAPDOUBLE.

00324 {
00325     MAPDOUBLE(x, y, *tx, *ty);
00326 }

QRect QMatrix::mapRect ( const QRect rectangle  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 344 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, QRect::bottom(), F, h, QRect::height(), QRect::left(), MAPDOUBLE, qMax(), qMin(), qRound(), QPixmap::rect(), QRect::right(), QRect::top(), w, QRect::width(), QRect::x(), x, QRect::y(), and y.

Referenced by QSvgTinyDocument::adjustWindowBounds(), QGraphicsItem::childrenBoundingRect(), QGraphicsItem::ensureVisible(), QGraphicsScene::inputMethodQuery(), QGraphicsScene::itemUpdated(), MandelbrotWidget::paintEvent(), QGraphicsView::paintEvent(), QGraphicsViewPrivate::recalculateContentSize(), QGraphicsView::render(), QGraphicsScene::render(), GraphWidget::scaleView(), QGraphicsItem::sceneBoundingRect(), and QPicturePaintEngine::writeCmdLength().

00345 {
00346     QRect result;
00347     if (_m12 == 0.0F && _m21 == 0.0F) {
00348         int x = qRound(_m11*rect.x() + _dx);
00349         int y = qRound(_m22*rect.y() + _dy);
00350         int w = qRound(_m11*rect.width());
00351         int h = qRound(_m22*rect.height());
00352         if (w < 0) {
00353             w = -w;
00354             x -= w;
00355         }
00356         if (h < 0) {
00357             h = -h;
00358             y -= h;
00359         }
00360         result = QRect(x, y, w, h);
00361     } else {
00362         // see mapToPolygon for explanations of the algorithm.
00363         qreal x0, y0;
00364         qreal x, y;
00365         MAPDOUBLE(rect.left(), rect.top(), x0, y0);
00366         qreal xmin = x0;
00367         qreal ymin = y0;
00368         qreal xmax = x0;
00369         qreal ymax = y0;
00370         MAPDOUBLE(rect.right() + 1, rect.top(), x, y);
00371         xmin = qMin(xmin, x);
00372         ymin = qMin(ymin, y);
00373         xmax = qMax(xmax, x);
00374         ymax = qMax(ymax, y);
00375         MAPDOUBLE(rect.right() + 1, rect.bottom() + 1, x, y);
00376         xmin = qMin(xmin, x);
00377         ymin = qMin(ymin, y);
00378         xmax = qMax(xmax, x);
00379         ymax = qMax(ymax, y);
00380         MAPDOUBLE(rect.left(), rect.bottom() + 1, x, y);
00381         xmin = qMin(xmin, x);
00382         ymin = qMin(ymin, y);
00383         xmax = qMax(xmax, x);
00384         ymax = qMax(ymax, y);
00385         qreal w = xmax - xmin;
00386         qreal h = ymax - ymin;
00387         xmin -= (xmin - x0) / w;
00388         ymin -= (ymin - y0) / h;
00389         xmax -= (xmax - x0) / w;
00390         ymax -= (ymax - y0) / h;
00391         result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin)+1, qRound(ymax)-qRound(ymin)+1);
00392     }
00393     return result;
00394 }

Here is the call graph for this function:

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 418 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, F, h, QRect::height(), MAPDOUBLE, qMax(), qMin(), QPixmap::rect(), w, QRect::width(), QRect::x(), x, QRect::y(), and y.

00419 {
00420     QRectF result;
00421     if (_m12 == 0.0F && _m21 == 0.0F) {
00422         qreal x = _m11*rect.x() + _dx;
00423         qreal y = _m22*rect.y() + _dy;
00424         qreal w = _m11*rect.width();
00425         qreal h = _m22*rect.height();
00426         if (w < 0) {
00427             w = -w;
00428             x -= w;
00429         }
00430         if (h < 0) {
00431             h = -h;
00432             y -= h;
00433         }
00434         result = QRectF(x, y, w, h);
00435     } else {
00436         qreal x0, y0;
00437         qreal x, y;
00438         MAPDOUBLE(rect.x(), rect.y(), x0, y0);
00439         qreal xmin = x0;
00440         qreal ymin = y0;
00441         qreal xmax = x0;
00442         qreal ymax = y0;
00443         MAPDOUBLE(rect.x() + rect.width(), rect.y(), x, y);
00444         xmin = qMin(xmin, x);
00445         ymin = qMin(ymin, y);
00446         xmax = qMax(xmax, x);
00447         ymax = qMax(ymax, y);
00448         MAPDOUBLE(rect.x() + rect.width(), rect.y() + rect.height(), x, y);
00449         xmin = qMin(xmin, x);
00450         ymin = qMin(ymin, y);
00451         xmax = qMax(xmax, x);
00452         ymax = qMax(ymax, y);
00453         MAPDOUBLE(rect.x(), rect.y() + rect.height(), x, y);
00454         xmin = qMin(xmin, x);
00455         ymin = qMin(ymin, y);
00456         xmax = qMax(xmax, x);
00457         ymax = qMax(ymax, y);
00458         result = QRectF(xmin, ymin, xmax-xmin, ymax - ymin);
00459     }
00460     return result;
00461 }

Here is the call graph for this function:

QPoint QMatrix::map ( const QPoint point  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QPoint object that is a copy of the given point, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 483 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, p, and qRound().

00484 {
00485     qreal fx = p.x();
00486     qreal fy = p.y();
00487     return QPoint(qRound(_m11*fx + _m21*fy + _dx),
00488                    qRound(_m12*fx + _m22*fy + _dy));
00489 }

Here is the call graph for this function:

QPointF QMatrix::map ( const QPointF point  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QPointF object that is a copy of the given point, mapped into the coordinate system defined by this matrix.

Definition at line 507 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, QPointF::x(), and QPointF::y().

00508 {
00509     qreal fx = point.x();
00510     qreal fy = point.y();
00511     return QPointF(_m11*fx + _m21*fy + _dx, _m12*fx + _m22*fy + _dy);
00512 }

Here is the call graph for this function:

QLine QMatrix::map ( const QLine line  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QLine object that is a copy of the given line, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 561 of file qmatrix.cpp.

References map(), QLine::p1(), and QLine::p2().

00562 {
00563     return QLine(map(line.p1()), map(line.p2()));
00564 }

Here is the call graph for this function:

QLineF QMatrix::map ( const QLineF line  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QLineF object that is a copy of the given line, mapped into the coordinate system defined by this matrix.

Definition at line 548 of file qmatrix.cpp.

References map(), QLineF::p1(), and QLineF::p2().

00549 {
00550     return QLineF(map(line.p1()), map(line.p2()));
00551 }

Here is the call graph for this function:

QPolygonF QMatrix::map ( const QPolygonF polygon  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QPolygonF object that is a copy of the given polygon, mapped into the coordinate system defined by this matrix.

Definition at line 650 of file qmatrix.cpp.

References a, i, MAPDOUBLE, p, and size.

00651 {
00652     int size = a.size();
00653     int i;
00654     QPolygonF p(size);
00655     const QPointF *da = a.constData();
00656     QPointF *dp = p.data();
00657     for(i = 0; i < size; i++) {
00658         MAPDOUBLE(da[i].xp, da[i].yp, dp[i].xp, dp[i].yp);
00659     }
00660     return p;
00661 }

QPolygon QMatrix::map ( const QPolygon polygon  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QPolygon object that is a copy of the given polygon, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

Definition at line 584 of file qmatrix.cpp.

References a, QVector< T >::data(), h, i, MAPDOUBLE, p, qMax(), qRound(), QPoint::setY(), size, w, x, QPointF::xp, QPoint::y(), and QPointF::yp.

00585 {
00586     int size = a.size();
00587     int i;
00588     QPolygonF p(size);
00589     const QPoint *da = a.constData();
00590     QPointF *dp = p.data();
00591     qreal xmin = qreal(INT_MAX);
00592     qreal ymin = xmin;
00593     qreal xmax = qreal(INT_MIN);
00594     qreal ymax = xmax;
00595     int xminp = 0;
00596     int yminp = 0;
00597     for(i = 0; i < size; i++) {
00598         dp[i].xp = da[i].x();
00599         dp[i].yp = da[i].y();
00600         if (dp[i].xp < xmin) {
00601             xmin = dp[i].xp;
00602             xminp = i;
00603         }
00604         if (dp[i].yp < ymin) {
00605             ymin = dp[i].yp;
00606             yminp = i;
00607         }
00608         xmax = qMax(xmax, dp[i].xp);
00609         ymax = qMax(ymax, dp[i].yp);
00610     }
00611     qreal w = qMax<qreal>(xmax - xmin, 1.);
00612     qreal h = qMax<qreal>(ymax - ymin, 1.);
00613     for(i = 0; i < size; i++) {
00614         dp[i].xp += (dp[i].xp - xmin)/w;
00615         dp[i].yp += (dp[i].yp - ymin)/h;
00616         MAPDOUBLE(dp[i].xp, dp[i].yp, dp[i].xp, dp[i].yp);
00617     }
00618 
00619     // now apply correction back for transformed values...
00620     xmin = qreal(INT_MAX/256);
00621     ymin = xmin;
00622     xmax = qreal(INT_MIN/256);
00623     ymax = xmax;
00624     for(i = 0; i < size; i++) {
00625         xmin = qMin<qreal>(xmin, dp[i].xp);
00626         ymin = qMin<qreal>(ymin, dp[i].yp);
00627         xmax = qMax<qreal>(xmax, dp[i].xp);
00628         ymax = qMax<qreal>(ymax, dp[i].yp);
00629     }
00630     w = qMax<qreal>(xmax - xmin, 1.);
00631     h = qMax<qreal>(ymax - ymin, 1.);
00632 
00633     QPolygon result(size);
00634     QPoint *dr = result.data();
00635     for(i = 0; i < size; i++) {
00636         dr[i].setX(qRound(dp[i].xp - (dp[i].xp - dp[xminp].xp)/w));
00637         dr[i].setY(qRound(dp[i].yp - (dp[i].yp - dp[yminp].yp)/h));
00638     }
00639     return result;
00640 }

Here is the call graph for this function:

QRegion QMatrix::map ( const QRegion region  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QRegion object that is a copy of the given region, mapped into the coordinate system defined by this matrix.

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

Definition at line 692 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, TokenEngine::copy(), map(), p, and qRound().

00693 {
00694     if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) { // translate or identity
00695         if (_dx == 0.0 && _dy == 0.0) // Identity
00696             return r;
00697         QRegion copy(r);
00698         copy.translate(qRound(_dx), qRound(_dy));
00699         return copy;
00700     }
00701 
00702     QPainterPath p;
00703     p.addRegion(r);
00704     p = map(p);
00705     return p.toFillPolygon().toPolygon();
00706 }

Here is the call graph for this function:

QPainterPath QMatrix::map ( const QPainterPath path  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates and returns a QPainterPath object that is a copy of the given path, mapped into the coordinate system defined by this matrix.

Definition at line 724 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, TokenEngine::copy(), i, and path.

00725 {
00726     if (path.isEmpty())
00727         return QPainterPath();
00728 
00729     QPainterPath copy = path;
00730 
00731     // Translate or identity
00732     if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {
00733 
00734         // Translate
00735         if (_dx != 0.0 || _dy != 0.0) {
00736             copy.detach();
00737             for (int i=0; i<path.elementCount(); ++i) {
00738                 QPainterPath::Element &e = copy.d_ptr->elements[i];
00739                 e.x += _dx;
00740                 e.y += _dy;
00741             }
00742         }
00743 
00744     // Full xform
00745     } else {
00746         copy.detach();
00747         for (int i=0; i<path.elementCount(); ++i) {
00748             QPainterPath::Element &e = copy.d_ptr->elements[i];
00749             qreal fx = e.x, fy = e.y;
00750             e.x = _m11*fx + _m21*fy + _dx;
00751             e.y =  _m12*fx + _m22*fy + _dy;
00752         }
00753     }
00754 
00755     return copy;
00756 }

Here is the call graph for this function:

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 817 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, a, F, QRect::height(), i, MAPDOUBLE, qDebug(), qRound(), QPixmap::rect(), right(), QRect::width(), QRect::x(), and QRect::y().

00818 {
00819     QPolygon a(4);
00820     qreal x[4], y[4];
00821     if (_m12 == 0.0F && _m21 == 0.0F) {
00822         x[0] = _m11*rect.x() + _dx;
00823         y[0] = _m22*rect.y() + _dy;
00824         qreal w = _m11*rect.width();
00825         qreal h = _m22*rect.height();
00826         if (w < 0) {
00827             w = -w;
00828             x[0] -= w;
00829         }
00830         if (h < 0) {
00831             h = -h;
00832             y[0] -= h;
00833         }
00834         x[1] = x[0]+w;
00835         x[2] = x[1];
00836         x[3] = x[0];
00837         y[1] = y[0];
00838         y[2] = y[0]+h;
00839         y[3] = y[2];
00840     } else {
00841         qreal right = rect.x() + rect.width();
00842         qreal bottom = rect.y() + rect.height();
00843         MAPDOUBLE(rect.x(), rect.y(), x[0], y[0]);
00844         MAPDOUBLE(right, rect.y(), x[1], y[1]);
00845         MAPDOUBLE(right, bottom, x[2], y[2]);
00846         MAPDOUBLE(rect.x(), bottom, x[3], y[3]);
00847     }
00848 #if 0
00849     int i;
00850     for(i = 0; i< 4; i++)
00851         qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i]));
00852     qDebug("width=%f, height=%f", sqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0])),
00853             sqrt((x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3])));
00854 #endif
00855     // all coordinates are correctly, tranform to a pointarray
00856     // (rounding to the next integer)
00857     a.setPoints(4, qRound(x[0]), qRound(y[0]),
00858                  qRound(x[1]), qRound(y[1]),
00859                  qRound(x[2]), qRound(y[2]),
00860                  qRound(x[3]), qRound(y[3]));
00861     return a;
00862 }

Here is the call graph for this function:

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 873 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, and _m22.

Referenced by QPainterState::init().

00874 {
00875     _m11 = _m22 = 1.0;
00876     _m12 = _m21 = _dx = _dy = 0.0;
00877 }

bool QMatrix::isIdentity (  )  const [inline]

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

See also:
reset()

Definition at line 127 of file qmatrix.h.

References _dx, _dy, _m11, _m12, _m21, and _m22.

Referenced by QSvgGradientStyle::apply(), parseBaseGradient(), parseTransform(), QGraphicsItem::setMatrix(), QStrokerOps::strokeEllipse(), QStrokerOps::strokePath(), and QStrokerOps::strokePolygon().

00128 {
00129     return qFuzzyCompare(_m11, 1) && qFuzzyCompare(_m22, 1) && qFuzzyCompare(_m12, 0)
00130            && qFuzzyCompare(_m21, 0) && qFuzzyCompare(_dx, 0) && qFuzzyCompare(_dy, 0);
00131 }

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 895 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, and _m22.

Referenced by QPdfEnginePrivate::addBrushPattern(), QFontEngineBox::addOutlineToPath(), QFontEngine::addOutlineToPath(), QGraphicsItemGroup::addToGroup(), CannonField::barrelHit(), QGraphicsItem::childrenBoundingRect(), QX11PaintEngine::drawBox(), QCleanlooksStyle::drawControl(), QX11PaintEngine::drawFreetype(), QOpenGLPaintEngine::drawTextItem(), Q3Canvas::drawViewArea(), QX11PaintEngine::drawXLFD(), QGraphicsView::mapFromScene(), QGraphicsView::mapToScene(), QGraphicsItemAnimation::matrixAt(), QPdfEnginePrivate::pageMatrix(), QGraphicsView::paintEvent(), parseTransformationMatrix(), QGraphicsView::render(), QSvgAnimateTransform::resolveMatrix(), QGraphicsItem::sceneMatrix(), and QGraphicsView::translate().

00896 {
00897     _dx += dx*_m11 + dy*_m21;
00898     _dy += dy*_m22 + dx*_m12;
00899     return *this;
00900 }

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 911 of file qmatrix.cpp.

References _m11, _m12, _m21, and _m22.

Referenced by QSvgTinyDocument::adjustWindowBounds(), QCleanlooksStyle::drawControl(), QPicture::exec(), PathStrokeRenderer::initializePoints(), QGraphicsItemAnimation::matrixAt(), parseTransformationMatrix(), QSvgAnimateTransform::resolveMatrix(), QGraphicsView::scale(), QImage::scaled(), QPixmap::scaled(), QImage::scaledToHeight(), QPixmap::scaledToHeight(), QImage::scaledToWidth(), QPixmap::scaledToWidth(), GraphWidget::scaleView(), and View::setupMatrix().

00912 {
00913     _m11 *= sx;
00914     _m12 *= sx;
00915     _m21 *= sy;
00916     _m22 *= sy;
00917     return *this;
00918 }

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 927 of file qmatrix.cpp.

References _m11, _m12, _m21, and _m22.

Referenced by PathStrokeRenderer::initializePoints(), QGraphicsItemAnimation::matrixAt(), parseTransformationMatrix(), QSvgAnimateTransform::resolveMatrix(), and QGraphicsView::shear().

00928 {
00929     qreal tm11 = sv*_m21;
00930     qreal tm12 = sv*_m22;
00931     qreal tm21 = sh*_m11;
00932     qreal tm22 = sh*_m12;
00933     _m11 += tm11;
00934     _m12 += tm12;
00935     _m21 += tm21;
00936     _m22 += tm22;
00937     return *this;
00938 }

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 957 of file qmatrix.cpp.

References _m11, _m12, _m21, _m22, and b.

Referenced by CannonField::barrelHit(), QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QCleanlooksStyle::drawControl(), QGraphicsItemAnimation::matrixAt(), parseTransformationMatrix(), QSvgAnimateTransform::resolveMatrix(), QGraphicsView::rotate(), and View::setupMatrix().

00958 {
00959     qreal sina = 0;
00960     qreal cosa = 0;
00961     if (a == 90. || a == -270.)
00962         sina = 1.;
00963     else if (a == 270. || a == -90.)
00964         sina = -1.;
00965     else if (a == 180.)
00966         cosa = -1.;
00967     else{
00968         qreal b = deg2rad*a;                        // convert to radians
00969         sina = sin(b);                // fast and convenient
00970         cosa = cos(b);
00971     }
00972     qreal tm11 = cosa*_m11 + sina*_m21;
00973     qreal tm12 = cosa*_m12 + sina*_m22;
00974     qreal tm21 = -sina*_m11 + cosa*_m21;
00975     qreal tm22 = -sina*_m12 + cosa*_m22;
00976     _m11 = tm11; _m12 = tm12;
00977     _m21 = tm21; _m22 = tm22;
00978     return *this;
00979 }

bool QMatrix::isInvertible (  )  const [inline]

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

See also:
inverted()

Definition at line 82 of file qmatrix.h.

Referenced by Q3CanvasView::setWorldMatrix().

00082 { return !qFuzzyCompare(_m11*_m22 - _m12*_m21, 0); }

qreal QMatrix::det (  )  const [inline]

Returns the matrix's determinant.

Definition at line 83 of file qmatrix.h.

Referenced by inverted().

00083 { return _m11*_m22 - _m12*_m21; }

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 1014 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, det(), and determinant().

Referenced by CannonField::barrelHit(), QGraphicsItem::mapFromParent(), QGraphicsItem::mapFromScene(), MandelbrotWidget::paintEvent(), QGraphicsView::paintEvent(), QGraphicsScene::render(), QSpanData::setupMatrix(), and QImage::transformed().

01015 {
01016     qreal determinant = det();
01017     if (determinant == 0.0) {
01018         if (invertible)
01019             *invertible = false;                // singular matrix
01020         QMatrix defaultMatrix;
01021         return defaultMatrix;
01022     }
01023     else {                                        // invertible matrix
01024         if (invertible)
01025             *invertible = true;
01026         qreal dinv = 1.0/determinant;
01027         QMatrix imatrix((_m22*dinv),        (-_m12*dinv),
01028                           (-_m21*dinv), (_m11*dinv),
01029                           ((_m21*_dy - _m22*_dx)*dinv),
01030                           ((_m12*_dx - _m11*_dy)*dinv));
01031         return imatrix;
01032     }
01033 }

Here is the call graph for this function:

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

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

Definition at line 1043 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, and m.

01044 {
01045     return _m11 == m._m11 &&
01046            _m12 == m._m12 &&
01047            _m21 == m._m21 &&
01048            _m22 == m._m22 &&
01049            _dx == m._dx &&
01050            _dy == m._dy;
01051 }

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 1060 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, and m.

01061 {
01062     return _m11 != m._m11 ||
01063            _m12 != m._m12 ||
01064            _m21 != m._m21 ||
01065            _m22 != m._m22 ||
01066            _dx != m._dx ||
01067            _dy != m._dy;
01068 }

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 1078 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, _m22, and m.

01079 {
01080     qreal tm11 = _m11*m._m11 + _m12*m._m21;
01081     qreal tm12 = _m11*m._m12 + _m12*m._m22;
01082     qreal tm21 = _m21*m._m11 + _m22*m._m21;
01083     qreal tm22 = _m21*m._m12 + _m22*m._m22;
01084 
01085     qreal tdx  = _dx*m._m11  + _dy*m._m21 + m._dx;
01086     qreal tdy =  _dx*m._m12  + _dy*m._m22 + m._dy;
01087 
01088     _m11 = tm11; _m12 = tm12;
01089     _m21 = tm21; _m22 = tm22;
01090     _dx = tdx; _dy = tdy;
01091     return *this;
01092 }

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 1104 of file qmatrix.cpp.

References m.

01105 {
01106     QMatrix result = *this;
01107     result *= m;
01108     return result;
01109 }

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

Assigns the given matrix's values to this matrix.

Definition at line 1114 of file qmatrix.cpp.

References _dx, _dy, _m11, _m12, _m21, and _m22.

01115 {
01116     _m11 = matrix._m11;
01117     _m12 = matrix._m12;
01118     _m21 = matrix._m21;
01119     _m22 = matrix._m22;
01120     _dx  = matrix._dx;
01121     _dy  = matrix._dy;
01122     return *this;
01123 }

QMatrix::operator QVariant (  )  const

Since:
4.2
Returns the matrix as a QVariant.

Definition at line 1130 of file qmatrix.cpp.

References QVariant::Matrix.

01131 {
01132     return QVariant(QVariant::Matrix, this);
01133 }


Friends And Related Function Documentation

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

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

See also:
QMatrix::map()

Definition at line 111 of file qmatrix.h.

00112 { return m.map(p); }

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

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

See also:
QMatrix::map()

Definition at line 113 of file qmatrix.h.

00114 { return m.map(p); }

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

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

See also:
QMatrix::map()

Definition at line 115 of file qmatrix.h.

00116 { return m.map(l); }

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

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

See also:
QMatrix::map()

Definition at line 117 of file qmatrix.h.

00118 { return m.map(l); }

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

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

See also:
QMatrix::map()

Definition at line 121 of file qmatrix.h.

00122 { return m.map(a); }

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

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

See also:
QMatrix::map()

Definition at line 119 of file qmatrix.h.

00120 { return m.map(a); }

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

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

See also:
QMatrix::map()

Definition at line 123 of file qmatrix.h.

00124 { return m.map(r); }

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

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

See also:
QMatrix::map()

Definition at line 1135 of file qmatrix.cpp.

01136 {
01137     return m.map(p);
01138 }

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:
{Format of the QDataStream Operators}

Definition at line 1155 of file qmatrix.cpp.

01156 {
01157     if (s.version() == 1) {
01158         s << (float)m.m11() << (float)m.m12() << (float)m.m21()
01159           << (float)m.m22() << (float)m.dx()  << (float)m.dy();
01160     } else {
01161         s << double(m.m11())
01162           << double(m.m12())
01163           << double(m.m21())
01164           << double(m.m22())
01165           << double(m.dx())
01166           << double(m.dy());
01167     }
01168     return s;
01169 }

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

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

See also:
{Format of the QDataStream Operators}

Definition at line 1181 of file qmatrix.cpp.

01182 {
01183     if (s.version() == 1) {
01184         float m11, m12, m21, m22, dx, dy;
01185         s >> m11;  s >> m12;  s >> m21;  s >> m22;
01186         s >> dx;   s >> dy;
01187         m.setMatrix(m11, m12, m21, m22, dx, dy);
01188     }
01189     else {
01190         double m11, m12, m21, m22, dx, dy;
01191         s >> m11;
01192         s >> m12;
01193         s >> m21;
01194         s >> m22;
01195         s >> dx;
01196         s >> dy;
01197         m.setMatrix(m11, m12, m21, m22, dx, dy);
01198     }
01199     return s;
01200 }


Member Data Documentation

qreal QMatrix::_m11 [private]

Definition at line 104 of file qmatrix.h.

Referenced by inverted(), isIdentity(), map(), mapRect(), mapToPolygon(), operator *=(), operator!=(), operator=(), operator==(), QMatrix(), reset(), rotate(), scale(), setMatrix(), shear(), and translate().

qreal QMatrix::_m12 [private]

Definition at line 104 of file qmatrix.h.

Referenced by inverted(), isIdentity(), map(), mapRect(), mapToPolygon(), operator *=(), operator!=(), operator=(), operator==(), QMatrix(), reset(), rotate(), scale(), setMatrix(), shear(), and translate().

qreal QMatrix::_m21 [private]

Definition at line 105 of file qmatrix.h.

Referenced by inverted(), isIdentity(), map(), mapRect(), mapToPolygon(), operator *=(), operator!=(), operator=(), operator==(), QMatrix(), reset(), rotate(), scale(), setMatrix(), shear(), and translate().

qreal QMatrix::_m22 [private]

Definition at line 105 of file qmatrix.h.

Referenced by inverted(), isIdentity(), map(), mapRect(), mapToPolygon(), operator *=(), operator!=(), operator=(), operator==(), QMatrix(), reset(), rotate(), scale(), setMatrix(), shear(), and translate().

qreal QMatrix::_dx [private]

Definition at line 106 of file qmatrix.h.

Referenced by inverted(), isIdentity(), map(), mapRect(), mapToPolygon(), operator *=(), operator!=(), operator=(), operator==(), QMatrix(), reset(), setMatrix(), and translate().

qreal QMatrix::_dy [private]

Definition at line 106 of file qmatrix.h.

Referenced by inverted(), isIdentity(), map(), mapRect(), mapToPolygon(), operator *=(), operator!=(), operator=(), operator==(), QMatrix(), reset(), setMatrix(), and translate().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:24:15 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1