qvector2d.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtGui module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QVECTOR2D_H
  40. #define QVECTOR2D_H
  41. #include <QtCore/qpoint.h>
  42. #include <QtCore/qmetatype.h>
  43. QT_BEGIN_NAMESPACE
  44. class QVector3D;
  45. class QVector4D;
  46. class QVariant;
  47. #ifndef QT_NO_VECTOR2D
  48. class Q_GUI_EXPORT QVector2D
  49. {
  50. public:
  51. Q_DECL_CONSTEXPR QVector2D();
  52. explicit QVector2D(Qt::Initialization) {}
  53. Q_DECL_CONSTEXPR QVector2D(float xpos, float ypos);
  54. Q_DECL_CONSTEXPR explicit QVector2D(const QPoint& point);
  55. Q_DECL_CONSTEXPR explicit QVector2D(const QPointF& point);
  56. #ifndef QT_NO_VECTOR3D
  57. explicit QVector2D(const QVector3D& vector);
  58. #endif
  59. #ifndef QT_NO_VECTOR4D
  60. explicit QVector2D(const QVector4D& vector);
  61. #endif
  62. bool isNull() const;
  63. Q_DECL_CONSTEXPR float x() const;
  64. Q_DECL_CONSTEXPR float y() const;
  65. void setX(float x);
  66. void setY(float y);
  67. float &operator[](int i);
  68. float operator[](int i) const;
  69. float length() const;
  70. float lengthSquared() const; //In Qt 6 convert to inline and constexpr
  71. QVector2D normalized() const Q_REQUIRED_RESULT;
  72. void normalize();
  73. float distanceToPoint(const QVector2D &point) const;
  74. float distanceToLine(const QVector2D& point, const QVector2D& direction) const;
  75. QVector2D &operator+=(const QVector2D &vector);
  76. QVector2D &operator-=(const QVector2D &vector);
  77. QVector2D &operator*=(float factor);
  78. QVector2D &operator*=(const QVector2D &vector);
  79. QVector2D &operator/=(float divisor);
  80. inline QVector2D &operator/=(const QVector2D &vector);
  81. static float dotProduct(const QVector2D& v1, const QVector2D& v2); //In Qt 6 convert to inline and constexpr
  82. Q_DECL_CONSTEXPR friend inline bool operator==(const QVector2D &v1, const QVector2D &v2);
  83. Q_DECL_CONSTEXPR friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2);
  84. Q_DECL_CONSTEXPR friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2);
  85. Q_DECL_CONSTEXPR friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2);
  86. Q_DECL_CONSTEXPR friend inline const QVector2D operator*(float factor, const QVector2D &vector);
  87. Q_DECL_CONSTEXPR friend inline const QVector2D operator*(const QVector2D &vector, float factor);
  88. Q_DECL_CONSTEXPR friend inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2);
  89. Q_DECL_CONSTEXPR friend inline const QVector2D operator-(const QVector2D &vector);
  90. Q_DECL_CONSTEXPR friend inline const QVector2D operator/(const QVector2D &vector, float divisor);
  91. Q_DECL_CONSTEXPR friend inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor);
  92. Q_DECL_CONSTEXPR friend inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2);
  93. #ifndef QT_NO_VECTOR3D
  94. QVector3D toVector3D() const;
  95. #endif
  96. #ifndef QT_NO_VECTOR4D
  97. QVector4D toVector4D() const;
  98. #endif
  99. Q_DECL_CONSTEXPR QPoint toPoint() const;
  100. Q_DECL_CONSTEXPR QPointF toPointF() const;
  101. operator QVariant() const;
  102. private:
  103. float xp, yp;
  104. friend class QVector3D;
  105. friend class QVector4D;
  106. };
  107. Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE);
  108. Q_DECL_CONSTEXPR inline QVector2D::QVector2D() : xp(0.0f), yp(0.0f) {}
  109. Q_DECL_CONSTEXPR inline QVector2D::QVector2D(float xpos, float ypos) : xp(xpos), yp(ypos) {}
  110. Q_DECL_CONSTEXPR inline QVector2D::QVector2D(const QPoint& point) : xp(point.x()), yp(point.y()) {}
  111. Q_DECL_CONSTEXPR inline QVector2D::QVector2D(const QPointF& point) : xp(point.x()), yp(point.y()) {}
  112. inline bool QVector2D::isNull() const
  113. {
  114. return qIsNull(xp) && qIsNull(yp);
  115. }
  116. Q_DECL_CONSTEXPR inline float QVector2D::x() const { return xp; }
  117. Q_DECL_CONSTEXPR inline float QVector2D::y() const { return yp; }
  118. inline void QVector2D::setX(float aX) { xp = aX; }
  119. inline void QVector2D::setY(float aY) { yp = aY; }
  120. inline float &QVector2D::operator[](int i)
  121. {
  122. Q_ASSERT(uint(i) < 2u);
  123. return *(&xp + i);
  124. }
  125. inline float QVector2D::operator[](int i) const
  126. {
  127. Q_ASSERT(uint(i) < 2u);
  128. return *(&xp + i);
  129. }
  130. inline QVector2D &QVector2D::operator+=(const QVector2D &vector)
  131. {
  132. xp += vector.xp;
  133. yp += vector.yp;
  134. return *this;
  135. }
  136. inline QVector2D &QVector2D::operator-=(const QVector2D &vector)
  137. {
  138. xp -= vector.xp;
  139. yp -= vector.yp;
  140. return *this;
  141. }
  142. inline QVector2D &QVector2D::operator*=(float factor)
  143. {
  144. xp *= factor;
  145. yp *= factor;
  146. return *this;
  147. }
  148. inline QVector2D &QVector2D::operator*=(const QVector2D &vector)
  149. {
  150. xp *= vector.xp;
  151. yp *= vector.yp;
  152. return *this;
  153. }
  154. inline QVector2D &QVector2D::operator/=(float divisor)
  155. {
  156. xp /= divisor;
  157. yp /= divisor;
  158. return *this;
  159. }
  160. inline QVector2D &QVector2D::operator/=(const QVector2D &vector)
  161. {
  162. xp /= vector.xp;
  163. yp /= vector.yp;
  164. return *this;
  165. }
  166. Q_DECL_CONSTEXPR inline bool operator==(const QVector2D &v1, const QVector2D &v2)
  167. {
  168. return v1.xp == v2.xp && v1.yp == v2.yp;
  169. }
  170. Q_DECL_CONSTEXPR inline bool operator!=(const QVector2D &v1, const QVector2D &v2)
  171. {
  172. return v1.xp != v2.xp || v1.yp != v2.yp;
  173. }
  174. Q_DECL_CONSTEXPR inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
  175. {
  176. return QVector2D(v1.xp + v2.xp, v1.yp + v2.yp);
  177. }
  178. Q_DECL_CONSTEXPR inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)
  179. {
  180. return QVector2D(v1.xp - v2.xp, v1.yp - v2.yp);
  181. }
  182. Q_DECL_CONSTEXPR inline const QVector2D operator*(float factor, const QVector2D &vector)
  183. {
  184. return QVector2D(vector.xp * factor, vector.yp * factor);
  185. }
  186. Q_DECL_CONSTEXPR inline const QVector2D operator*(const QVector2D &vector, float factor)
  187. {
  188. return QVector2D(vector.xp * factor, vector.yp * factor);
  189. }
  190. Q_DECL_CONSTEXPR inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)
  191. {
  192. return QVector2D(v1.xp * v2.xp, v1.yp * v2.yp);
  193. }
  194. Q_DECL_CONSTEXPR inline const QVector2D operator-(const QVector2D &vector)
  195. {
  196. return QVector2D(-vector.xp, -vector.yp);
  197. }
  198. Q_DECL_CONSTEXPR inline const QVector2D operator/(const QVector2D &vector, float divisor)
  199. {
  200. return QVector2D(vector.xp / divisor, vector.yp / divisor);
  201. }
  202. Q_DECL_CONSTEXPR inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor)
  203. {
  204. return QVector2D(vector.xp / divisor.xp, vector.yp / divisor.yp);
  205. }
  206. Q_DECL_CONSTEXPR inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)
  207. {
  208. return qFuzzyCompare(v1.xp, v2.xp) && qFuzzyCompare(v1.yp, v2.yp);
  209. }
  210. Q_DECL_CONSTEXPR inline QPoint QVector2D::toPoint() const
  211. {
  212. return QPoint(qRound(xp), qRound(yp));
  213. }
  214. Q_DECL_CONSTEXPR inline QPointF QVector2D::toPointF() const
  215. {
  216. return QPointF(qreal(xp), qreal(yp));
  217. }
  218. #ifndef QT_NO_DEBUG_STREAM
  219. Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector);
  220. #endif
  221. #ifndef QT_NO_DATASTREAM
  222. Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &);
  223. Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &);
  224. #endif
  225. #endif
  226. QT_END_NAMESPACE
  227. #endif