<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/src/kernel/qwmatrix.cpp:49 --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>QWMatrix Class</title> <style type="text/css"><!-- h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } body { background: #ffffff; color: black; } --></style> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr bgcolor="#E5E5E5"> <td valign=center> <a href="index.html"> <font color="#004faf">Home</font></a> | <a href="classes.html"> <font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"> <font color="#004faf">Main Classes</font></a> | <a href="annotated.html"> <font color="#004faf">Annotated</font></a> | <a href="groups.html"> <font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"> <font color="#004faf">Functions</font></a> </td> <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QWMatrix Class Reference</h1> <p>The QWMatrix class specifies 2D transformations of a coordinate system. <a href="#details">More...</a> <p><tt>#include <<a href="qwmatrix-h.html">qwmatrix.h</a>></tt> <p><a href="qwmatrix-members.html">List of all member functions.</a> <h2>Public Members</h2> <ul> <li><div class=fn><a href="#QWMatrix"><b>QWMatrix</b></a> ()</div></li> <li><div class=fn><a href="#QWMatrix-2"><b>QWMatrix</b></a> ( double m11, double m12, double m21, double m22, double dx, double dy )</div></li> <li><div class=fn>void <a href="#setMatrix"><b>setMatrix</b></a> ( double m11, double m12, double m21, double m22, double dx, double dy )</div></li> <li><div class=fn>double <a href="#m11"><b>m11</b></a> () const</div></li> <li><div class=fn>double <a href="#m12"><b>m12</b></a> () const</div></li> <li><div class=fn>double <a href="#m21"><b>m21</b></a> () const</div></li> <li><div class=fn>double <a href="#m22"><b>m22</b></a> () const</div></li> <li><div class=fn>double <a href="#dx"><b>dx</b></a> () const</div></li> <li><div class=fn>double <a href="#dy"><b>dy</b></a> () const</div></li> <li><div class=fn>void <a href="#map"><b>map</b></a> ( int x, int y, int * tx, int * ty ) const</div></li> <li><div class=fn>void <a href="#map-2"><b>map</b></a> ( double x, double y, double * tx, double * ty ) const</div></li> <li><div class=fn>QRect <a href="#mapRect"><b>mapRect</b></a> ( const QRect & rect ) const</div></li> <li><div class=fn>QPoint map ( const QPoint & p ) const <em>(obsolete)</em></div></li> <li><div class=fn>QRect map ( const QRect & r ) const <em>(obsolete)</em></div></li> <li><div class=fn>QPointArray map ( const QPointArray & a ) const <em>(obsolete)</em></div></li> <li><div class=fn>void <a href="#reset"><b>reset</b></a> ()</div></li> <li><div class=fn>bool <a href="#isIdentity"><b>isIdentity</b></a> () const</div></li> <li><div class=fn>QWMatrix & <a href="#translate"><b>translate</b></a> ( double dx, double dy )</div></li> <li><div class=fn>QWMatrix & <a href="#scale"><b>scale</b></a> ( double sx, double sy )</div></li> <li><div class=fn>QWMatrix & <a href="#shear"><b>shear</b></a> ( double sh, double sv )</div></li> <li><div class=fn>QWMatrix & <a href="#rotate"><b>rotate</b></a> ( double a )</div></li> <li><div class=fn>bool <a href="#isInvertible"><b>isInvertible</b></a> () const</div></li> <li><div class=fn>QWMatrix <a href="#invert"><b>invert</b></a> ( bool * invertible = 0 ) const</div></li> <li><div class=fn>bool <a href="#operator-eq-eq"><b>operator==</b></a> ( const QWMatrix & m ) const</div></li> <li><div class=fn>bool <a href="#operator!-eq"><b>operator!=</b></a> ( const QWMatrix & m ) const</div></li> <li><div class=fn>QWMatrix & <a href="#operator*-eq"><b>operator*=</b></a> ( const QWMatrix & m )</div></li> <li><div class=fn>QPoint <a href="#operator*"><b>operator*</b></a> ( const QPoint & p ) const</div></li> <li><div class=fn>QRegion <a href="#operator*-2"><b>operator*</b></a> ( const QRect & r ) const</div></li> <li><div class=fn>QRegion <a href="#operator*-3"><b>operator*</b></a> ( const QRegion & r ) const</div></li> <li><div class=fn>QPointArray <a href="#operator*-4"><b>operator*</b></a> ( const QPointArray & a ) const</div></li> </ul> <h2>Related Functions</h2> <ul> <li><div class=fn>QWMatrix <a href="#operator*-5"><b>operator*</b></a> ( const QWMatrix & m1, const QWMatrix & m2 )</div></li> <li><div class=fn>QDataStream & <a href="#operator-lt-lt"><b>operator<<</b></a> ( QDataStream & s, const QWMatrix & m )</div></li> <li><div class=fn>QDataStream & <a href="#operator-gt-gt"><b>operator>></b></a> ( QDataStream & s, QWMatrix & m )</div></li> </ul> <hr><a name="details"></a><h2>Detailed Description</h2> The QWMatrix class specifies 2D transformations of a coordinate system. <p> The standard coordinate system of a <a href="qpaintdevice.html">paint device</a> has the origin located at the top-left position. X values increase to the right; Y values increase downward. <p> This coordinate system is default for the <a href="qpainter.html">QPainter</a>, which renders graphics in a paint device. A user-defined coordinate system can be specified by setting a QWMatrix for the painter. <p> Example: <pre> MyWidget::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * ) { <a href="qpainter.html">QPainter</a> p; // our painter QWMatrix m; // our transformation matrix m.<a href="#rotate">rotate</a>( 22.5 ); // rotated coordinate system p.<a href="qpainter.html#begin">begin</a>( this ); // start painting p.<a href="qpainter.html#setWorldMatrix">setWorldMatrix</a>( m ); // use rotated coordinate system p.<a href="qpainter.html#drawText">drawText</a>( 30,20, "detator" ); // draw rotated text at 30,20 p.<a href="qpainter.html#end">end</a>(); // painting done } </pre> <p> A matrix specifies how to translate, scale, shear or rotate the graphics; the actual transformation is performed by the drawing routines in <a href="qpainter.html">QPainter</a> and by <a href="qpixmap.html#xForm">QPixmap::xForm</a>(). <p> The QWMatrix class contains a 3*3 matrix of the form: <pre> m11 m12 0 m21 m22 0 dx dy 1 </pre> <p> A matrix transforms a point in the plane to another point: <pre> x' = m11*x + m21*y + dx y' = m22*y + m12*x + dy </pre> <p> The point <em>(x, y)</em> is the original point, and <em>(x', y')</em> is the transformed point. <em>(x', y')</em> can be transformed back to <em>(x, y)</em> by performing the same operation on the <a href="#invert">inverted matrix</a>. <p> The elements <em>dx</em> and <em>dy</em> specify horizontal and vertical translation. The elements <em>m11</em> and <em>m22</em> specify horizontal and vertical scaling. The elements <em>m12</em> and <em>m21</em> specify horizontal and vertical shearing. <p> The identity matrix has <em>m11</em> and <em>m22</em> set to 1; all others are set to 0. This matrix maps a point to itself. <p> Translation is the simplest transformation. Setting <em>dx</em> and <em>dy</em> will move the coordinate system <em>dx</em> units along the X axis and <em>dy</em> units along the Y axis. <p> Scaling can be done by setting <em>m11</em> and <em>m22</em>. For example, setting <em>m11</em> to 2 and <em>m22</em> to 1.5 will double the height and increase the width by 50%. <p> Shearing is controlled by <em>m12</em> and <em>m21</em>. Setting these elements to values different from zero will twist the coordinate system. <p> Rotation is achieved by carefully setting both the shearing factors and the scaling factors. The QWMatrix has a function that sets <a href="#rotate">rotation</a> directly. <p> QWMatrix lets you combine transformations like this: <pre> QWMatrix m; // identity matrix m.<a href="#translate">translate</a>(10, -20); // first translate (10,-20) m.<a href="#rotate">rotate</a>(25); // then rotate 25 degrees m.<a href="#scale">scale</a>(1.2, 0.7); // finally scale it </pre> <p> Here's the same example using basic matrix operations: <pre> double a = pi/180 * 25; // convert 25 to radians double sina = sin(a); double cosa = cos(a); QWMatrix m1(0, 0, 0, 0, 10, -20); // translation matrix QWMatrix m2( cosa, sina, // rotation matrix -sina, cosa, 0, 0 ); QWMatrix m3(1.2, 0, 0, 0.7, 0, 0); // scaling matrix QWMatrix m; m = m3 * m2 * m1; // combine all transformations </pre> <p> <a href="qpainter.html">QPainter</a> has functions to translate, scale, shear and rotate the coordinate system without using a QWMatrix. Although these functions are very convenient, it can be more efficient to build a QWMatrix and call <a href="qpainter.html#setWorldMatrix">QPainter::setWorldMatrix</a>() if you want to perform more than a single transform operation. <p> <p>See also <a href="qpainter.html#setWorldMatrix">QPainter::setWorldMatrix</a>(), <a href="qpixmap.html#xForm">QPixmap::xForm</a>(), <a href="graphics.html">Graphics Classes</a> and <a href="images.html">Image Processing Classes</a>. <hr><h2>Member Function Documentation</h2> <h3 class=fn><a name="QWMatrix"></a>QWMatrix::QWMatrix () </h3> Constructs an identity matrix. All elements are set to zero except <em>m11</em> and <em>m22</em> (scaling), which are set to 1. <h3 class=fn><a name="QWMatrix-2"></a>QWMatrix::QWMatrix ( double m11, double m12, double m21, double m22, double dx, double dy ) </h3> Constructs a matrix with the elements, <em>m11</em>, <em>m12</em>, <em>m21</em>, <em>m22</em>, <em>dx</em> and <em>dy</em>. <h3 class=fn>double <a name="dx"></a>QWMatrix::dx () const </h3> <p> Returns the horizontal translation. <h3 class=fn>double <a name="dy"></a>QWMatrix::dy () const </h3> <p> Returns the vertical translation. <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> <a name="invert"></a>QWMatrix::invert ( bool * invertible = 0 ) const </h3> Returns the inverted matrix. <p> If the matrix is singular (not invertible), the identity matrix is returned. <p> If <em>invertible</em> is not null, the value of <em>*invertible</em> is set to TRUE if the matrix is invertible or to FALSE if the matrix is not invertible. <p> <p>See also <a href="#isInvertible">isInvertible</a>(). <p>Example: <a href="t14.html#x2246">t14/cannon.cpp</a>. <h3 class=fn>bool <a name="isIdentity"></a>QWMatrix::isIdentity () const </h3> Returns TRUE if the matrix is the identity matrix; otherwise returns FALSE. <p> <p>See also <a href="#reset">reset</a>(). <h3 class=fn>bool <a name="isInvertible"></a>QWMatrix::isInvertible () const </h3> <p> Returns TRUE if the matrix is invertible; otherwise returns FALSE. <p> <p>See also <a href="#invert">invert</a>(). <h3 class=fn>double <a name="m11"></a>QWMatrix::m11 () const </h3> <p> Returns the X scaling factor. <h3 class=fn>double <a name="m12"></a>QWMatrix::m12 () const </h3> <p> Returns the vertical shearing factor. <h3 class=fn>double <a name="m21"></a>QWMatrix::m21 () const </h3> <p> Returns the horizontal shearing factor. <h3 class=fn>double <a name="m22"></a>QWMatrix::m22 () const </h3> <p> Returns the Y scaling factor. <h3 class=fn>void <a name="map"></a>QWMatrix::map ( int x, int y, int * tx, int * ty ) const </h3> Transforms ( <em>x</em>, <em>y</em> ) to ( <em>*tx</em>, <em>*ty</em> ) using the formulae: <p> <pre> *tx = m11*x + m21*y + dx (rounded to the nearest integer) *ty = m22*y + m12*x + dy (rounded to the nearest integer) </pre> <p>Examples: <a href="t14.html#x2247">t14/cannon.cpp</a> and <a href="xform-example.html#x134">xform/xform.cpp</a>. <h3 class=fn>void <a name="map-2"></a>QWMatrix::map ( double x, double y, double * tx, double * ty ) const </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Transforms ( <em>x</em>, <em>y</em> ) to ( <em>*tx</em>, <em>*ty</em> ) using the following formulae: <p> <pre> *tx = m11*x + m21*y + dx *ty = m22*y + m12*x + dy </pre> <h3 class=fn><a href="qpoint.html">QPoint</a> <a name="map-3"></a>QWMatrix::map ( const <a href="qpoint.html">QPoint</a> & p ) const </h3> <p> <b>This function is obsolete.</b> It is provided to keep old source working. We strongly advise against using it in new code. <p> Does the same as operator *( const <a href="qpoint.html">QPoint</a> &) <h3 class=fn><a href="qrect.html">QRect</a> <a name="map-4"></a>QWMatrix::map ( const <a href="qrect.html">QRect</a> & r ) const </h3> <p> <b>This function is obsolete.</b> It is provided to keep old source working. We strongly advise against using it in new code. <p> Please use <a href="#mapRect">QWMatrix::mapRect</a>() instead. <p> Note that this method does return the bounding rectangle of the <em>r</em>, when shearing or rotations are used. <h3 class=fn><a href="qpointarray.html">QPointArray</a> <a name="map-5"></a>QWMatrix::map ( const <a href="qpointarray.html">QPointArray</a> & a ) const </h3> <p> <b>This function is obsolete.</b> It is provided to keep old source working. We strongly advise against using it in new code. <p> Does the same as operator *( const <a href="qpointarray.html">QPointArray</a> &) <h3 class=fn><a href="qrect.html">QRect</a> <a name="mapRect"></a>QWMatrix::mapRect ( const <a href="qrect.html">QRect</a> & rect ) const </h3> Returns the transformed rectangle <em>rect</em>. <p> The bounding rectangle is returned if rotation or shearing has been specified. <p> If you need to know the exact region <em>rect</em> maps to use <a href="#operator*">operator*</a>(). <p> <p>See also <a href="#operator*">operator*</a>(). <h3 class=fn>bool <a name="operator!-eq"></a>QWMatrix::operator!= ( const <a href="qwmatrix.html">QWMatrix</a> & m ) const </h3> Returns TRUE if this matrix is not equal to <em>m</em>; otherwise returns FALSE. <h3 class=fn><a href="qpoint.html">QPoint</a> <a name="operator*"></a>QWMatrix::operator* ( const <a href="qpoint.html">QPoint</a> & p ) const </h3> Transforms <em>p</em> to using the formulae: <p> <pre> retx = m11*px + m21*py + dx (rounded to the nearest integer) rety = m22*py + m12*px + dy (rounded to the nearest integer) </pre> <h3 class=fn><a href="qregion.html">QRegion</a> <a name="operator*-2"></a>QWMatrix::operator* ( const <a href="qrect.html">QRect</a> & r ) const </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Transforms the rectangle <em>r</em>. <p> Rotation and shearing a rectangle results in a more general region, which is returned here. <p> Calling this method can be rather expensive, if rotations or shearing are used. If you just need to know the bounding rectangle of the returned region, use <a href="#mapRect">mapRect</a>() which is a lot faster than this function. <p> <p>See also <a href="#mapRect">QWMatrix::mapRect</a>(). <h3 class=fn><a href="qregion.html">QRegion</a> <a name="operator*-3"></a>QWMatrix::operator* ( const <a href="qregion.html">QRegion</a> & r ) const </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Transforms the region <em>r</em>. <p> Calling this method can be rather expensive, if rotations or shearing are used. <h3 class=fn><a href="qpointarray.html">QPointArray</a> <a name="operator*-4"></a>QWMatrix::operator* ( const <a href="qpointarray.html">QPointArray</a> & a ) const </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Returns the point array <em>a</em> transformed by calling map for each point. <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> & <a name="operator*-eq"></a>QWMatrix::operator*= ( const <a href="qwmatrix.html">QWMatrix</a> & m ) </h3> Returns the result of multiplying this matrix with matrix <em>m</em>. <h3 class=fn>bool <a name="operator-eq-eq"></a>QWMatrix::operator== ( const <a href="qwmatrix.html">QWMatrix</a> & m ) const </h3> Returns TRUE if this matrix is equal to <em>m</em>; otherwise returns FALSE. <h3 class=fn>void <a name="reset"></a>QWMatrix::reset () </h3> Resets the matrix to an identity matrix. <p> All elements are set to zero, except <em>m11</em> and <em>m22</em> (scaling) that are set to 1. <p> <p>See also <a href="#isIdentity">isIdentity</a>(). <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> & <a name="rotate"></a>QWMatrix::rotate ( double a ) </h3> Rotates the coordinate system <em>a</em> degrees counterclockwise. <p> Returns a reference to the matrix. <p> <p>See also <a href="#translate">translate</a>(), <a href="#scale">scale</a>() and <a href="#shear">shear</a>(). <p>Examples: <a href="canvas-example.html#x2555">canvas/canvas.cpp</a>, <a href="desktop-example.html#x1759">desktop/desktop.cpp</a>, <a href="drawdemo-example.html#x1660">drawdemo/drawdemo.cpp</a>, <a href="t14.html#x2248">t14/cannon.cpp</a> and <a href="xform-example.html#x135">xform/xform.cpp</a>. <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> & <a name="scale"></a>QWMatrix::scale ( double sx, double sy ) </h3> Scales the coordinate system unit by <em>sx</em> horizontally and <em>sy</em> vertically. <p> Returns a reference to the matrix. <p> <p>See also <a href="#translate">translate</a>(), <a href="#shear">shear</a>() and <a href="#rotate">rotate</a>(). <p>Examples: <a href="canvas-example.html#x2556">canvas/canvas.cpp</a>, <a href="fileiconview-example.html#x1589">fileiconview/qfileiconview.cpp</a>, <a href="movies-example.html#x1111">movies/main.cpp</a>, <a href="qmag-example.html#x728">qmag/qmag.cpp</a>, <a href="qtimage-nsplugin-example.html#x2449">qtimage/qtimage.cpp</a>, <a href="showimg-example.html#x457">showimg/showimg.cpp</a> and <a href="xform-example.html#x136">xform/xform.cpp</a>. <h3 class=fn>void <a name="setMatrix"></a>QWMatrix::setMatrix ( double m11, double m12, double m21, double m22, double dx, double dy ) </h3> Sets the matrix elements to the specified values, <em>m11</em>, <em>m12</em>, <em>m21</em>, <em>m22</em>, <em>dx</em> and <em>dy</em>. <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> & <a name="shear"></a>QWMatrix::shear ( double sh, double sv ) </h3> Shears the coordinate system by <em>sh</em> horizontally and <em>sv</em> vertically. <p> Returns a reference to the matrix. <p> <p>See also <a href="#translate">translate</a>(), <a href="#scale">scale</a>() and <a href="#rotate">rotate</a>(). <p>Examples: <a href="drawdemo-example.html#x1661">drawdemo/drawdemo.cpp</a> and <a href="xform-example.html#x137">xform/xform.cpp</a>. <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> & <a name="translate"></a>QWMatrix::translate ( double dx, double dy ) </h3> Moves the coordinate system <em>dx</em> along the X-axis and <em>dy</em> along the Y-axis. <p> Returns a reference to the matrix. <p> <p>See also <a href="#scale">scale</a>(), <a href="#shear">shear</a>() and <a href="#rotate">rotate</a>(). <p>Examples: <a href="canvas-example.html#x2557">canvas/canvas.cpp</a>, <a href="drawdemo-example.html#x1662">drawdemo/drawdemo.cpp</a>, <a href="t14.html#x2249">t14/cannon.cpp</a> and <a href="xform-example.html#x138">xform/xform.cpp</a>. <hr><h2>Related Functions</h2> <h3 class=fn><a href="qwmatrix.html">QWMatrix</a> <a name="operator*-5"></a>operator* ( const <a href="qwmatrix.html">QWMatrix</a> & m1, const <a href="qwmatrix.html">QWMatrix</a> & m2 ) </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Returns the product of <em>m1</em> * <em>m2</em>. <p> Note that matrix multiplication is not commutative, i.e. a*b != b*a. <h3 class=fn><a href="qdatastream.html">QDataStream</a> & <a name="operator-lt-lt"></a>operator<< ( <a href="qdatastream.html">QDataStream</a> & s, const <a href="qwmatrix.html">QWMatrix</a> & m ) </h3> Writes the matrix <em>m</em> to the stream <em>s</em> and returns a reference to the stream. <p> <p>See also <a href="datastreamformat.html">Format of the QDataStream operators</a>. <h3 class=fn><a href="qdatastream.html">QDataStream</a> & <a name="operator-gt-gt"></a>operator>> ( <a href="qdatastream.html">QDataStream</a> & s, <a href="qwmatrix.html">QWMatrix</a> & m ) </h3> Reads the matrix <em>m</em> from the stream <em>s</em> and returns a reference to the stream. <p> <p>See also <a href="datastreamformat.html">Format of the QDataStream operators</a>. <!-- eof --> <hr><p> This file is part of the <a href="index.html">Qt toolkit</a>. Copyright © 1995-2001 <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center> <table width=100% cellspacing=0 border=0><tr> <td>Copyright © 2001 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a> <td align=right><div align=right>Qt version 3.0.2</div> </table></div></address></body> </html>