📄 qmatrix.cpp
字号:
*/QPolygon QMatrix::mapToPolygon(const QRect &rect) const{ QPolygon a(4); qreal x[4], y[4]; if (_m12 == 0.0F && _m21 == 0.0F) { x[0] = _m11*rect.x() + _dx; y[0] = _m22*rect.y() + _dy; qreal w = _m11*rect.width(); qreal h = _m22*rect.height(); if (w < 0) { w = -w; x[0] -= w; } if (h < 0) { h = -h; y[0] -= h; } x[1] = x[0]+w; x[2] = x[1]; x[3] = x[0]; y[1] = y[0]; y[2] = y[0]+h; y[3] = y[2]; } else { qreal right = rect.x() + rect.width(); qreal bottom = rect.y() + rect.height(); MAPDOUBLE(rect.x(), rect.y(), x[0], y[0]); MAPDOUBLE(right, rect.y(), x[1], y[1]); MAPDOUBLE(right, bottom, x[2], y[2]); MAPDOUBLE(rect.x(), bottom, x[3], y[3]); }#if 0 int i; for(i = 0; i< 4; i++) qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i])); qDebug("width=%f, height=%f", sqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0])), sqrt((x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3])));#endif // all coordinates are correctly, tranform to a pointarray // (rounding to the next integer) a.setPoints(4, qRound(x[0]), qRound(y[0]), qRound(x[1]), qRound(y[1]), qRound(x[2]), qRound(y[2]), qRound(x[3]), qRound(y[3])); return a;}/*! Resets the matrix to an identity matrix, i.e. all elements are set to zero, except \c m11 and \c m22 (specifying the scale) which are set to 1. \sa QMatrix(), isIdentity(), {QMatrix#Basic Matrix Operations}{Basic Matrix Operations}*/void QMatrix::reset(){ _m11 = _m22 = 1.0; _m12 = _m21 = _dx = _dy = 0.0;}/*! \fn bool QMatrix::isIdentity() const Returns true if the matrix is the identity matrix, otherwise returns false. \sa reset()*//*! Moves the coordinate system \a dx along the x axis and \a dy along the y axis, and returns a reference to the matrix. \sa setMatrix()*/QMatrix &QMatrix::translate(qreal dx, qreal dy){ _dx += dx*_m11 + dy*_m21; _dy += dy*_m22 + dx*_m12; return *this;}/*! \fn QMatrix &QMatrix::scale(qreal sx, qreal sy) Scales the coordinate system by \a sx horizontally and \a sy vertically, and returns a reference to the matrix. \sa setMatrix()*/QMatrix &QMatrix::scale(qreal sx, qreal sy){ _m11 *= sx; _m12 *= sx; _m21 *= sy; _m22 *= sy; return *this;}/*! Shears the coordinate system by \a sh horizontally and \a sv vertically, and returns a reference to the matrix. \sa setMatrix()*/QMatrix &QMatrix::shear(qreal sh, qreal sv){ qreal tm11 = sv*_m21; qreal tm12 = sv*_m22; qreal tm21 = sh*_m11; qreal tm22 = sh*_m12; _m11 += tm11; _m12 += tm12; _m21 += tm21; _m22 += tm22; return *this;}const qreal deg2rad = qreal(0.017453292519943295769); // pi/180/*! \fn QMatrix &QMatrix::rotate(qreal degrees) Rotates the coordinate system the given \a 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. \sa setMatrix()*/QMatrix &QMatrix::rotate(qreal a){ qreal sina = 0; qreal cosa = 0; if (a == 90. || a == -270.) sina = 1.; else if (a == 270. || a == -90.) sina = -1.; else if (a == 180.) cosa = -1.; else{ qreal b = deg2rad*a; // convert to radians sina = qSin(b); // fast and convenient cosa = qCos(b); } qreal tm11 = cosa*_m11 + sina*_m21; qreal tm12 = cosa*_m12 + sina*_m22; qreal tm21 = -sina*_m11 + cosa*_m21; qreal tm22 = -sina*_m12 + cosa*_m22; _m11 = tm11; _m12 = tm12; _m21 = tm21; _m22 = tm22; return *this;}/*! \fn bool QMatrix::isInvertible() const Returns true if the matrix is invertible, otherwise returns false. \sa inverted()*//*! \fn qreal QMatrix::det() const Returns the matrix's determinant.*//*! \fn QMatrix QMatrix::invert(bool *invertible) const Returns an inverted copy of this matrix. Use the inverted() function instead.*//*! Returns an inverted copy of this matrix. If the matrix is singular (not invertible), the returned matrix is the identity matrix. If \a invertible is valid (i.e. not 0), its value is set to true if the matrix is invertible, otherwise it is set to false. \sa isInvertible()*/QMatrix QMatrix::inverted(bool *invertible) const{ qreal determinant = det(); if (determinant == 0.0) { if (invertible) *invertible = false; // singular matrix QMatrix defaultMatrix; return defaultMatrix; } else { // invertible matrix if (invertible) *invertible = true; qreal dinv = 1.0/determinant; QMatrix imatrix((_m22*dinv), (-_m12*dinv), (-_m21*dinv), (_m11*dinv), ((_m21*_dy - _m22*_dx)*dinv), ((_m12*_dx - _m11*_dy)*dinv)); return imatrix; }}/*! \fn bool QMatrix::operator==(const QMatrix &matrix) const Returns true if this matrix is equal to the given \a matrix, otherwise returns false.*/bool QMatrix::operator==(const QMatrix &m) const{ return _m11 == m._m11 && _m12 == m._m12 && _m21 == m._m21 && _m22 == m._m22 && _dx == m._dx && _dy == m._dy;}/*! \fn bool QMatrix::operator!=(const QMatrix &matrix) const Returns true if this matrix is not equal to the given \a matrix, otherwise returns false.*/bool QMatrix::operator!=(const QMatrix &m) const{ return _m11 != m._m11 || _m12 != m._m12 || _m21 != m._m21 || _m22 != m._m22 || _dx != m._dx || _dy != m._dy;}/*! \fn QMatrix &QMatrix::operator *=(const QMatrix &matrix) \overload Returns the result of multiplying this matrix by the given \a matrix.*/QMatrix &QMatrix::operator *=(const QMatrix &m){ qreal tm11 = _m11*m._m11 + _m12*m._m21; qreal tm12 = _m11*m._m12 + _m12*m._m22; qreal tm21 = _m21*m._m11 + _m22*m._m21; qreal tm22 = _m21*m._m12 + _m22*m._m22; qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx; qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy; _m11 = tm11; _m12 = tm12; _m21 = tm21; _m22 = tm22; _dx = tdx; _dy = tdy; return *this;}/*! \fn QMatrix QMatrix::operator *(const QMatrix &matrix) const Returns the result of multiplying this matrix by the given \a matrix. Note that matrix multiplication is not commutative, i.e. a*b != b*a.*/QMatrix QMatrix::operator *(const QMatrix &m) const{ QMatrix result = *this; result *= m; return result;}/*! Assigns the given \a matrix's values to this matrix.*/QMatrix &QMatrix::operator=(const QMatrix &matrix){ _m11 = matrix._m11; _m12 = matrix._m12; _m21 = matrix._m21; _m22 = matrix._m22; _dx = matrix._dx; _dy = matrix._dy; return *this;}/*! \since 4.2 Returns the matrix as a QVariant.*/QMatrix::operator QVariant() const{ return QVariant(QVariant::Matrix, this);}Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m){ return m.map(p);}/***************************************************************************** QMatrix stream functions *****************************************************************************/#ifndef QT_NO_DATASTREAM/*! \fn QDataStream &operator<<(QDataStream &stream, const QMatrix &matrix) \relates QMatrix Writes the given \a matrix to the given \a stream and returns a reference to the stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &s, const QMatrix &m){ if (s.version() == 1) { s << (float)m.m11() << (float)m.m12() << (float)m.m21() << (float)m.m22() << (float)m.dx() << (float)m.dy(); } else { s << double(m.m11()) << double(m.m12()) << double(m.m21()) << double(m.m22()) << double(m.dx()) << double(m.dy()); } return s;}/*! \fn QDataStream &operator>>(QDataStream &stream, QMatrix &matrix) \relates QMatrix Reads the given \a matrix from the given \a stream and returns a reference to the stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &s, QMatrix &m){ if (s.version() == 1) { float m11, m12, m21, m22, dx, dy; s >> m11; s >> m12; s >> m21; s >> m22; s >> dx; s >> dy; m.setMatrix(m11, m12, m21, m22, dx, dy); } else { double m11, m12, m21, m22, dx, dy; s >> m11; s >> m12; s >> m21; s >> m22; s >> dx; s >> dy; m.setMatrix(m11, m12, m21, m22, dx, dy); } return s;}#endif // QT_NO_DATASTREAM#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug dbg, const QMatrix &m){ dbg.nospace() << "QMatrix(" << "11=" << m.m11() << " 12=" << m.m12() << " 21=" << m.m21() << " 22=" << m.m22() << " dx=" << m.dx() << " dy=" << m.dy() << ")"; return dbg.space();}#endif/*! \fn QRect QMatrix::map(const QRect &rect) const \compat Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate system defined by this matrix. Use the mapRect() function instead.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -