Q2DMatrix Class Reference


The Q2DMatrix class specifies two-dimensional transformations of a coordinate system. (details) (complete member list)

#include <q2matrix.h>

Public Members

Related Functions

(Note that these are not member functions.)

Detailed Description

The Q2DMatrix class specifies two-dimensional transformations of a coordinate system.

The standard coordinate system of a paint device (see QPaintDevice) has its origin located at the top left position. X values increase to the left, and Y values increase to the bottom. This coordinate system can be replaced by a user-defined coordinate system as specified by the Q2DMatrix class.

The QPainter class provides functions for drawing graphics. Setting a matrix will transform the graphics output:

  MyWidget::paintEvent( QPaintEvent *e ) {
    QPainter  p;                        // our painter
    Q2DMatrix m;                        // our transformation matrix
    p.begin( this );                    // start painting
    m.rotate( 22.5 );                   // rotated coordinate system
    p.setMatrix( m );                   // use it for painting
    p.drawText( 30,20, "detator" );     // draw rotated text at 30,20
    p.end();                            // painting done
  }

A matrix specifies how to translate, scale, shear or rotate the graphics, and the actual transformation is performed by the drawing routines in QPainter and by QPixmap::xForm().

The Q2DMatrix class contains a 3*3 matrix of the form:

    m11  m12  0
    m21  m22  0
    dx   dy   1
  

A matrix transforms a point in the plane to another point:

    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 elements dx and dy specify horisontal and vertical translation. The elements m11 and m22 specify horisontal and vertical scaling. The elements m12 and m21 specify horisontal and vertical shearing.

The identify matrix has m11 and m22 set to 1, all others set to 0. This matrix maps a point to itself.

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%.

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. The Q2DMatrix has a function that sets rotation directly.

Transformations can be combined by creating one matrix for each primitive transformation, and then multiply the matrices together in reversed transformation order.

Q2DMatrix lets you combine transformations more easily:

    Q2DMatrix m;                        // identity matrix
    m.translate(10, -20);               // first translate (10,-20)
    m.rotate(25);                       // then rotate 25 degrees
    m.scale(1.2, 0.7);                  // finally scale it

The same transformation matrix can be constructed using basic matrix operations:

    float a    = pi/180 * 25;           // convert 25 to radians
    float sina = sin(a);
    float cosa = cos(a);
    Q2DMatrix m1(0, 0, 0, 0, 10, -20);  // translation matrix
    Q2DMatrix m2(cosa, sina, -sina, cosa, // rotation matrix
                 0, 0 );
    Q2DMatrix m3(1.2, 0, 0, 0.7, 0, 0); // scaling matrix
    Q2DMatrix m;
    m = m3 * m2 * m1;                   // combine all transformations

Member Function Documentation

Q2DMatrix::Q2DMatrix ()

Constructs an identify matrix. All elements are set to zero, except m11 and m22 (scaling) which are set to 1.

Q2DMatrix::Q2DMatrix (float m11, float m12, float m21, float m22, float dx, float dy)

Constructs a matrix with the specified elements.

QRect Q2DMatrix::boundingRect (const QRect &r) const

Returns the bounding rect of the parallelogram formed when the four corner points of r are transformed by map().

float Q2DMatrix::dx() const

Returns the horizontal translation (displacement).

float Q2DMatrix::dy() const

Returns the vertical translation (displacement).

Q2DMatrix Q2DMatrix::invert (bool *invertible) const

Returns the inverted matrix.

If the matrix is singular (not invertible), then the identity matrix is returned.

If *invertible is not null, then the value of *invertible will be set to TRUE or FALSE to tell if the matrix is invertible or not.

See also: QPixmap::trueMatrix().

float Q2DMatrix::m11() const

Returns the X scaling factor.

float Q2DMatrix::m12() const

Returns the vertical shearing factor.

float Q2DMatrix::m21() const

Returns the horizontal shearing factor.

float Q2DMatrix::m22() const

Returns the Y scaling factor.

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

Transforms (x,y) to (*tx,*ty), using the formulae:

    *tx = m11*x + m21*y + dx  --  (rounded to the nearest integer)
    *ty = m22*y + m12*x + dy  --  (rounded to the nearest integer)

void Q2DMatrix::map(float x, float y, float *tx, float *ty) const

Transforms (x,y) to (*tx,*ty), using the formulae:

    *tx = m11*x + m21*y + dx
    *ty = m22*y + m12*x + dy

QPointArray Q2DMatrix::map(const QPointArray &a) const

Returns the point array av transformed by calling map for each point.

bool Q2DMatrix::operator!= (const Q2DMatrix &m) const

Returns TRUE if this matrix is not equal to m.

Q2DMatrix & Q2DMatrix::operator*= (const Q2DMatrix &m)

Returns the result of multiplying this matrix with m.

bool Q2DMatrix::operator== (const Q2DMatrix &m) const

Returns TRUE if this matrix is equal to m.

void Q2DMatrix::reset()

Resets the matrix to an identify matrix.

All elements are set to zero, except m11 and m22 (scaling) that are set to 1.

Q2DMatrix & Q2DMatrix::rotate (float a)

Rotates the coordinate system a degrees counterclockwise.

Returns a reference to the matrix.

See also: translate(), scale(), shear(), QPixmap::trueMatrix().

Q2DMatrix & Q2DMatrix::scale (float sx, float sy)

Scales the coordinate system unit by sx horizontally and sy vertically.

Returns a reference to the matrix.

See also: translate(), shear(), rotate(), QPixmap::trueMatrix().

void Q2DMatrix::setMatrix(float m11, float m12, float m21, float m22, float dx, float dy)

Sets the elements of the matrix to the specified values.

Q2DMatrix & Q2DMatrix::shear (float sh, float sv)

Shears the coordinate system by sh horizontally and sv vertically.

Returns a reference to the matrix.

See also: translate(), scale(), rotate(), QPixmap::trueMatrix().

Q2DMatrix & Q2DMatrix::translate(float dx, float dy)

Moves the coordinate system dx along the X-axis and dy along the Y-axis.

Returns a reference to the matrix.

See also: scale(), shear(), rotate(), QPixmap::trueMatrix().


Related Functions

Q2DMatrix operator* (const Q2DMatrix &m1, const Q2DMatrix &m2)

Returns the product m1 * m2.

Remember that matrix multiplication is not commutative, thus a*b != b*a.

QDataStream & operator<< (QDataStream &s, const Q2DMatrix &m)

Writes a matrix to the stream as 6 float values: m11, m12, m21, m22, dx, dy.

QDataStream & operator>> (QDataStream &s, Q2DMatrix &m)

Reads a matrix from the stream.


This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.

It was generated from the following files:


Generated at 16:17, 1995/06/30 by the webmaster at Troll Tech