返回介绍

QWMatrix Class

发布于 2019-10-04 15:04:07 字数 14393 浏览 838 评论 0 收藏 0

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

#include <qwmatrix.h>

List of all member functions.

Public Members

  • QWMatrix ()
  • QWMatrix ( doublem11, doublem12, doublem21, doublem22, doubledx, doubledy )
  • void setMatrix ( doublem11, doublem12, doublem21, doublem22, doubledx, doubledy )
  • double m11 () const
  • double m12 () const
  • double m21 () const
  • double m22 () const
  • double dx () const
  • double dy () const
  • void map ( intx, inty, int*tx, int*ty ) const
  • void map ( doublex, doubley, double*tx, double*ty ) const
  • QRect mapRect ( constQRect&rect ) const
  • QPoint map ( constQPoint&p ) const (obsolete)
  • QRect map ( constQRect&r ) const (obsolete)
  • QPointArray map ( constQPointArray&a ) const (obsolete)
  • void reset ()
  • bool isIdentity () const
  • QWMatrix & translate ( doubledx, doubledy )
  • QWMatrix & scale ( doublesx, doublesy )
  • QWMatrix & shear ( doublesh, doublesv )
  • QWMatrix & rotate ( doublea )
  • bool isInvertible () const
  • QWMatrix invert ( bool*invertible = 0 ) const
  • bool operator== ( constQWMatrix&m ) const
  • bool operator!= ( constQWMatrix&m ) const
  • QWMatrix & operator*= ( constQWMatrix&m )
  • QPoint operator* ( constQPoint&p ) const
  • QRegion operator* ( constQRect&r ) const
  • QRegion operator* ( constQRegion&r ) const
  • QPointArray operator* ( constQPointArray&a ) const

Related Functions

  • QWMatrix operator* ( constQWMatrix&m1, constQWMatrix&m2 )
  • QDataStream & operator<< ( QDataStream&s, constQWMatrix&m )
  • QDataStream & operator>> ( QDataStream&s, QWMatrix&m )

Detailed Description

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

The standard coordinate system of a paint device has the origin located at the top-left position. X values increase to the right; Y values increase downward.

This coordinate system is default for the QPainter, which renders graphics in a paint device. A user-defined coordinate system can be specified by setting a QWMatrix for the painter.

Example:

    MyWidget::paintEvent( QPaintEvent * )
    {
      QPainter p;                       // our painter
      QWMatrix m;                       // our transformation matrix
      m.rotate( 22.5 );                 // rotated coordinate system
      p.begin( this );                  // start painting
      p.setWorldMatrix( m );            // use rotated coordinate system
      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; the actual transformation is performed by the drawing routines in QPainter and by QPixmap::xForm().

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

The identity matrix has m11 and m22 set to 1; all others are 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 QWMatrix has a function that sets rotation directly.

QWMatrix lets you combine transformations like this:

    QWMatrix 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
  

Here's the same example using basic matrix operations:

    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
  

QPainter 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 QPainter::setWorldMatrix() if you want to perform more than a single transform operation.

See also QPainter::setWorldMatrix(), QPixmap::xForm(), Graphics Classes and Image Processing Classes.


Member Function Documentation

QWMatrix::QWMatrix ()

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

QWMatrix::QWMatrix ( doublem11, doublem12, doublem21, doublem22, doubledx, doubledy )

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

double QWMatrix::dx () const

Returns the horizontal translation.

double QWMatrix::dy () const

Returns the vertical translation.

QWMatrix QWMatrix::invert ( bool*invertible = 0 ) const

Returns the inverted matrix.

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

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

See also isInvertible().

Example: t14/cannon.cpp.

bool QWMatrix::isIdentity () const

Returns TRUE if the matrix is the identity matrix; otherwise returns FALSE.

See also reset().

bool QWMatrix::isInvertible () const

Returns TRUE if the matrix is invertible; otherwise returns FALSE.

See also invert().

double QWMatrix::m11 () const

Returns the X scaling factor.

double QWMatrix::m12 () const

Returns the vertical shearing factor.

double QWMatrix::m21 () const

Returns the horizontal shearing factor.

double QWMatrix::m22 () const

Returns the Y scaling factor.

void QWMatrix::map ( intx, inty, 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)
  

Examples: t14/cannon.cpp and xform/xform.cpp.

void QWMatrix::map ( doublex, doubley, double*tx, double*ty ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

QPoint QWMatrix::map ( constQPoint&p ) const

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Does the same as operator *( const QPoint &)

QRect QWMatrix::map ( constQRect&r ) const

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Please use QWMatrix::mapRect() instead.

Note that this method does return the bounding rectangle of the r, when shearing or rotations are used.

QPointArray QWMatrix::map ( constQPointArray&a ) const

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Does the same as operator *( const QPointArray &)

QRect QWMatrix::mapRect ( constQRect&rect ) const

Returns the transformed rectangle rect.

The bounding rectangle is returned if rotation or shearing has been specified.

If you need to know the exact region rect maps to use operator*().

See also operator*().

bool QWMatrix::operator!= ( constQWMatrix&m ) const

Returns TRUE if this matrix is not equal to m; otherwise returns FALSE.

QPoint QWMatrix::operator* ( constQPoint&p ) const

Transforms p to using the formulae:

    retx = m11*px + m21*py + dx  (rounded to the nearest integer)
    rety = m22*py + m12*px + dy  (rounded to the nearest integer)
  

QRegion QWMatrix::operator* ( constQRect&r ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms the rectangle r.

Rotation and shearing a rectangle results in a more general region, which is returned here.

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 mapRect() which is a lot faster than this function.

See also QWMatrix::mapRect().

QRegion QWMatrix::operator* ( constQRegion&r ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms the region r.

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

QPointArray QWMatrix::operator* ( constQPointArray&a ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

QWMatrix& QWMatrix::operator*= ( constQWMatrix&m )

Returns the result of multiplying this matrix with matrix m.

bool QWMatrix::operator== ( constQWMatrix&m ) const

Returns TRUE if this matrix is equal to m; otherwise returns FALSE.

void QWMatrix::reset ()

Resets the matrix to an identity matrix.

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

See also isIdentity().

QWMatrix& QWMatrix::rotate ( doublea )

Rotates the coordinate system a degrees counterclockwise.

Returns a reference to the matrix.

See also translate(), scale() and shear().

Examples: canvas/canvas.cpp, desktop/desktop.cpp, drawdemo/drawdemo.cpp, t14/cannon.cpp and xform/xform.cpp.

QWMatrix& QWMatrix::scale ( doublesx, doublesy )

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

Returns a reference to the matrix.

See also translate(), shear() and rotate().

Examples: canvas/canvas.cpp, fileiconview/qfileiconview.cpp, movies/main.cpp, qmag/qmag.cpp, qtimage/qtimage.cpp, showimg/showimg.cpp and xform/xform.cpp.

void QWMatrix::setMatrix ( doublem11, doublem12, doublem21, doublem22, doubledx, doubledy )

Sets the matrix elements to the specified values, m11, m12, m21, m22, dx and dy.

QWMatrix& QWMatrix::shear ( doublesh, doublesv )

Shears the coordinate system by sh horizontally and sv vertically.

Returns a reference to the matrix.

See also translate(), scale() and rotate().

Examples: drawdemo/drawdemo.cpp and xform/xform.cpp.

QWMatrix& QWMatrix::translate ( doubledx, doubledy )

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() and rotate().

Examples: canvas/canvas.cpp, drawdemo/drawdemo.cpp, t14/cannon.cpp and xform/xform.cpp.


Related Functions

QWMatrix operator* ( constQWMatrix&m1, constQWMatrix&m2 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the product of m1 * m2.

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

QDataStream& operator<< ( QDataStream&s, constQWMatrix&m )

Writes the matrix m to the stream s and returns a reference to the stream.

See also Format of the QDataStream operators.

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

Reads the matrix m from the stream s and returns a reference to the stream.

See also Format of the QDataStream operators.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文