qvector2d.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QVECTOR2D_H
00043 #define QVECTOR2D_H
00044 
00045 #include <QtCore/qpoint.h>
00046 #include <QtCore/qmetatype.h>
00047 
00048 QT_BEGIN_HEADER
00049 
00050 QT_BEGIN_NAMESPACE
00051 
00052 QT_MODULE(Gui)
00053 
00054 class QVector3D;
00055 class QVector4D;
00056 class QVariant;
00057 
00058 #ifndef QT_NO_VECTOR2D
00059 
00060 class Q_GUI_EXPORT QVector2D
00061 {
00062 public:
00063     QVector2D();
00064     QVector2D(qreal xpos, qreal ypos);
00065     explicit QVector2D(const QPoint& point);
00066     explicit QVector2D(const QPointF& point);
00067 #ifndef QT_NO_VECTOR3D
00068     explicit QVector2D(const QVector3D& vector);
00069 #endif
00070 #ifndef QT_NO_VECTOR4D
00071     explicit QVector2D(const QVector4D& vector);
00072 #endif
00073 
00074     bool isNull() const;
00075 
00076     qreal x() const;
00077     qreal y() const;
00078 
00079     void setX(qreal x);
00080     void setY(qreal y);
00081 
00082     qreal length() const;
00083     qreal lengthSquared() const;
00084 
00085     QVector2D normalized() const;
00086     void normalize();
00087 
00088     QVector2D &operator+=(const QVector2D &vector);
00089     QVector2D &operator-=(const QVector2D &vector);
00090     QVector2D &operator*=(qreal factor);
00091     QVector2D &operator*=(const QVector2D &vector);
00092     QVector2D &operator/=(qreal divisor);
00093 
00094     static qreal dotProduct(const QVector2D& v1, const QVector2D& v2);
00095 
00096     friend inline bool operator==(const QVector2D &v1, const QVector2D &v2);
00097     friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2);
00098     friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2);
00099     friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2);
00100     friend inline const QVector2D operator*(qreal factor, const QVector2D &vector);
00101     friend inline const QVector2D operator*(const QVector2D &vector, qreal factor);
00102     friend inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2);
00103     friend inline const QVector2D operator-(const QVector2D &vector);
00104     friend inline const QVector2D operator/(const QVector2D &vector, qreal divisor);
00105 
00106     friend inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2);
00107 
00108 #ifndef QT_NO_VECTOR3D
00109     QVector3D toVector3D() const;
00110 #endif
00111 #ifndef QT_NO_VECTOR4D
00112     QVector4D toVector4D() const;
00113 #endif
00114 
00115     QPoint toPoint() const;
00116     QPointF toPointF() const;
00117 
00118     operator QVariant() const;
00119 
00120 private:
00121     float xp, yp;
00122 
00123     QVector2D(float xpos, float ypos, int dummy);
00124 
00125     friend class QVector3D;
00126     friend class QVector4D;
00127 };
00128 
00129 Q_DECLARE_TYPEINFO(QVector2D, Q_MOVABLE_TYPE);
00130 
00131 inline QVector2D::QVector2D() : xp(0.0f), yp(0.0f) {}
00132 
00133 inline QVector2D::QVector2D(float xpos, float ypos, int) : xp(xpos), yp(ypos) {}
00134 
00135 inline QVector2D::QVector2D(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) {}
00136 
00137 inline QVector2D::QVector2D(const QPoint& point) : xp(point.x()), yp(point.y()) {}
00138 
00139 inline QVector2D::QVector2D(const QPointF& point) : xp(point.x()), yp(point.y()) {}
00140 
00141 inline bool QVector2D::isNull() const
00142 {
00143     return qIsNull(xp) && qIsNull(yp);
00144 }
00145 
00146 inline qreal QVector2D::x() const { return qreal(xp); }
00147 inline qreal QVector2D::y() const { return qreal(yp); }
00148 
00149 inline void QVector2D::setX(qreal aX) { xp = aX; }
00150 inline void QVector2D::setY(qreal aY) { yp = aY; }
00151 
00152 inline QVector2D &QVector2D::operator+=(const QVector2D &vector)
00153 {
00154     xp += vector.xp;
00155     yp += vector.yp;
00156     return *this;
00157 }
00158 
00159 inline QVector2D &QVector2D::operator-=(const QVector2D &vector)
00160 {
00161     xp -= vector.xp;
00162     yp -= vector.yp;
00163     return *this;
00164 }
00165 
00166 inline QVector2D &QVector2D::operator*=(qreal factor)
00167 {
00168     xp *= factor;
00169     yp *= factor;
00170     return *this;
00171 }
00172 
00173 inline QVector2D &QVector2D::operator*=(const QVector2D &vector)
00174 {
00175     xp *= vector.xp;
00176     yp *= vector.yp;
00177     return *this;
00178 }
00179 
00180 inline QVector2D &QVector2D::operator/=(qreal divisor)
00181 {
00182     xp /= divisor;
00183     yp /= divisor;
00184     return *this;
00185 }
00186 
00187 inline bool operator==(const QVector2D &v1, const QVector2D &v2)
00188 {
00189     return v1.xp == v2.xp && v1.yp == v2.yp;
00190 }
00191 
00192 inline bool operator!=(const QVector2D &v1, const QVector2D &v2)
00193 {
00194     return v1.xp != v2.xp || v1.yp != v2.yp;
00195 }
00196 
00197 inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
00198 {
00199     return QVector2D(v1.xp + v2.xp, v1.yp + v2.yp, 1);
00200 }
00201 
00202 inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)
00203 {
00204     return QVector2D(v1.xp - v2.xp, v1.yp - v2.yp, 1);
00205 }
00206 
00207 inline const QVector2D operator*(qreal factor, const QVector2D &vector)
00208 {
00209     return QVector2D(vector.xp * factor, vector.yp * factor, 1);
00210 }
00211 
00212 inline const QVector2D operator*(const QVector2D &vector, qreal factor)
00213 {
00214     return QVector2D(vector.xp * factor, vector.yp * factor, 1);
00215 }
00216 
00217 inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)
00218 {
00219     return QVector2D(v1.xp * v2.xp, v1.yp * v2.yp, 1);
00220 }
00221 
00222 inline const QVector2D operator-(const QVector2D &vector)
00223 {
00224     return QVector2D(-vector.xp, -vector.yp, 1);
00225 }
00226 
00227 inline const QVector2D operator/(const QVector2D &vector, qreal divisor)
00228 {
00229     return QVector2D(vector.xp / divisor, vector.yp / divisor, 1);
00230 }
00231 
00232 inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)
00233 {
00234     return qFuzzyCompare(v1.xp, v2.xp) && qFuzzyCompare(v1.yp, v2.yp);
00235 }
00236 
00237 inline QPoint QVector2D::toPoint() const
00238 {
00239     return QPoint(qRound(xp), qRound(yp));
00240 }
00241 
00242 inline QPointF QVector2D::toPointF() const
00243 {
00244     return QPointF(qreal(xp), qreal(yp));
00245 }
00246 
00247 #ifndef QT_NO_DEBUG_STREAM
00248 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector);
00249 #endif
00250 
00251 #ifndef QT_NO_DATASTREAM
00252 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &);
00253 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &);
00254 #endif
00255 
00256 #endif
00257 
00258 QT_END_NAMESPACE
00259 
00260 QT_END_HEADER
00261 
00262 #endif