File indexing completed on 2026-05-25 08:19:32
0001
0002
0003
0004 #ifndef QPOINT_H
0005 #define QPOINT_H
0006
0007 #include <QtCore/qcheckedint_impl.h>
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qnamespace.h>
0010 #include <QtCore/qnumeric.h>
0011
0012 #include <QtCore/q20type_traits.h>
0013 #include <QtCore/q23utility.h>
0014
0015 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0016 struct CGPoint;
0017 #endif
0018
0019 QT_BEGIN_NAMESPACE
0020
0021 QT_ENABLE_P0846_SEMANTICS_FOR(get)
0022
0023 class QDataStream;
0024 class QLine;
0025 class QPointF;
0026 class QRect;
0027
0028 class QPoint
0029 {
0030 public:
0031 constexpr QPoint() noexcept;
0032 constexpr QPoint(int xpos, int ypos) noexcept;
0033
0034 constexpr inline bool isNull() const noexcept;
0035
0036 constexpr inline int x() const noexcept;
0037 constexpr inline int y() const noexcept;
0038 constexpr inline void setX(int x) noexcept;
0039 constexpr inline void setY(int y) noexcept;
0040
0041 constexpr inline int manhattanLength() const;
0042
0043 constexpr QPoint transposed() const noexcept { return {yp, xp}; }
0044
0045 constexpr inline int &rx() noexcept;
0046 constexpr inline int &ry() noexcept;
0047
0048 constexpr inline QPoint &operator+=(const QPoint &p);
0049 constexpr inline QPoint &operator-=(const QPoint &p);
0050
0051 constexpr inline QPoint &operator*=(float factor);
0052 constexpr inline QPoint &operator*=(double factor);
0053 constexpr inline QPoint &operator*=(int factor);
0054
0055 constexpr inline QPoint &operator/=(qreal divisor);
0056
0057 constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2)
0058 { return int(p1.xp * p2.xp + p1.yp * p2.yp); }
0059
0060 private:
0061 friend constexpr bool comparesEqual(const QPoint &p1, const QPoint &p2) noexcept
0062 { return p1.xp == p2.xp && p1.yp == p2.yp; }
0063 Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPoint)
0064 friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept
0065 { return QPoint(p1.xp + p2.xp, p1.yp + p2.yp); }
0066 friend constexpr inline QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept
0067 { return QPoint(p1.xp - p2.xp, p1.yp - p2.yp); }
0068 friend constexpr inline QPoint operator*(const QPoint &p, float factor)
0069 { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0070 friend constexpr inline QPoint operator*(const QPoint &p, double factor)
0071 { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0072 friend constexpr inline QPoint operator*(const QPoint &p, int factor) noexcept
0073 { return QPoint(p.xp * factor, p.yp * factor); }
0074 friend constexpr inline QPoint operator*(float factor, const QPoint &p)
0075 { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0076 friend constexpr inline QPoint operator*(double factor, const QPoint &p)
0077 { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0078 friend constexpr inline QPoint operator*(int factor, const QPoint &p) noexcept
0079 { return QPoint(p.xp * factor, p.yp * factor); }
0080 friend constexpr inline QPoint operator+(const QPoint &p) noexcept
0081 { return p; }
0082 friend constexpr inline QPoint operator-(const QPoint &p) noexcept
0083 { return QPoint(-p.xp, -p.yp); }
0084 friend constexpr inline QPoint operator/(const QPoint &p, qreal c)
0085 {
0086 Q_ASSERT(!qFuzzyIsNull(c));
0087 return QPoint(QtPrivate::qSaturateRound(p.x() / c), QtPrivate::qSaturateRound(p.y() / c));
0088 }
0089
0090 public:
0091 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0092 [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
0093 #endif
0094 [[nodiscard]] constexpr inline QPointF toPointF() const noexcept;
0095
0096 private:
0097 using Representation = QtPrivate::QCheckedIntegers::QCheckedInt<int>;
0098
0099 friend class QRect;
0100 friend class QLine;
0101 constexpr QPoint(Representation xpos, Representation ypos) noexcept
0102 : xp(xpos), yp(ypos) {}
0103
0104 Representation xp;
0105 Representation yp;
0106
0107 template <std::size_t I,
0108 typename P,
0109 std::enable_if_t<(I < 2), bool> = true,
0110 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPoint>, bool> = true>
0111 friend constexpr decltype(auto) get(P &&p) noexcept
0112 {
0113 if constexpr (I == 0)
0114 return q23::forward_like<P>(p.xp).as_underlying();
0115 else if constexpr (I == 1)
0116 return q23::forward_like<P>(p.yp).as_underlying();
0117 }
0118 };
0119
0120 Q_DECLARE_TYPEINFO(QPoint, Q_PRIMITIVE_TYPE);
0121
0122
0123
0124
0125 #ifndef QT_NO_DATASTREAM
0126 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
0127 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
0128 #endif
0129
0130
0131
0132
0133
0134 constexpr inline QPoint::QPoint() noexcept : xp(0), yp(0) {}
0135
0136 constexpr inline QPoint::QPoint(int xpos, int ypos) noexcept : xp(xpos), yp(ypos) {}
0137
0138 constexpr inline bool QPoint::isNull() const noexcept
0139 {
0140 return xp == 0 && yp == 0;
0141 }
0142
0143 constexpr inline int QPoint::x() const noexcept
0144 {
0145 return xp.value();
0146 }
0147
0148 constexpr inline int QPoint::y() const noexcept
0149 {
0150 return yp.value();
0151 }
0152
0153 constexpr inline void QPoint::setX(int xpos) noexcept
0154 {
0155 xp.setValue(xpos);
0156 }
0157
0158 constexpr inline void QPoint::setY(int ypos) noexcept
0159 {
0160 yp.setValue(ypos);
0161 }
0162
0163 inline int constexpr QPoint::manhattanLength() const
0164 {
0165 return (qAbs(xp) + qAbs(yp)).value();
0166 }
0167
0168 constexpr inline int &QPoint::rx() noexcept
0169 {
0170 return xp.as_underlying();
0171 }
0172
0173 constexpr inline int &QPoint::ry() noexcept
0174 {
0175 return yp.as_underlying();
0176 }
0177
0178 constexpr inline QPoint &QPoint::operator+=(const QPoint &p)
0179 {
0180 xp += p.xp;
0181 yp += p.yp;
0182 return *this;
0183 }
0184
0185 constexpr inline QPoint &QPoint::operator-=(const QPoint &p)
0186 {
0187 xp -= p.xp;
0188 yp -= p.yp;
0189 return *this;
0190 }
0191
0192 constexpr inline QPoint &QPoint::operator*=(float factor)
0193 {
0194 xp.setValue(QtPrivate::qSaturateRound(x() * factor));
0195 yp.setValue(QtPrivate::qSaturateRound(y() * factor));
0196 return *this;
0197 }
0198
0199 constexpr inline QPoint &QPoint::operator*=(double factor)
0200 {
0201 xp.setValue(QtPrivate::qSaturateRound(x() * factor));
0202 yp.setValue(QtPrivate::qSaturateRound(y() * factor));
0203 return *this;
0204 }
0205
0206 constexpr inline QPoint &QPoint::operator*=(int factor)
0207 {
0208 xp = xp * factor;
0209 yp = yp * factor;
0210 return *this;
0211 }
0212
0213 constexpr inline QPoint &QPoint::operator/=(qreal c)
0214 {
0215 Q_ASSERT(!qFuzzyIsNull(c));
0216 xp.setValue(qRound(int(xp) / c));
0217 yp.setValue(qRound(int(yp) / c));
0218 return *this;
0219 }
0220
0221 #ifndef QT_NO_DEBUG_STREAM
0222 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
0223 #endif
0224
0225 Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
0226
0227
0228
0229
0230 class QPointF
0231 {
0232 public:
0233 constexpr QPointF() noexcept;
0234 constexpr QPointF(const QPoint &p) noexcept;
0235 constexpr QPointF(qreal xpos, qreal ypos) noexcept;
0236
0237 constexpr inline qreal manhattanLength() const;
0238
0239 inline bool isNull() const noexcept;
0240
0241 constexpr inline qreal x() const noexcept;
0242 constexpr inline qreal y() const noexcept;
0243 constexpr inline void setX(qreal x) noexcept;
0244 constexpr inline void setY(qreal y) noexcept;
0245
0246 constexpr QPointF transposed() const noexcept { return {yp, xp}; }
0247
0248 constexpr inline qreal &rx() noexcept;
0249 constexpr inline qreal &ry() noexcept;
0250
0251 constexpr inline QPointF &operator+=(const QPointF &p);
0252 constexpr inline QPointF &operator-=(const QPointF &p);
0253 constexpr inline QPointF &operator*=(qreal c);
0254 constexpr inline QPointF &operator/=(qreal c);
0255
0256 constexpr static inline qreal dotProduct(const QPointF &p1, const QPointF &p2)
0257 {
0258 return p1.xp * p2.xp + p1.yp * p2.yp;
0259 }
0260
0261 private:
0262 friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept
0263 {
0264 return QtPrivate::fuzzyCompare(p1.xp, p2.xp)
0265 && QtPrivate::fuzzyCompare(p1.yp, p2.yp);
0266 }
0267 friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept
0268 {
0269 return qFuzzyIsNull(point.xp) && qFuzzyIsNull(point.yp);
0270 }
0271 friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept
0272 { return qFuzzyCompare(p1, p2); }
0273 Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF)
0274 friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept
0275 { return comparesEqual(p1, p2.toPointF()); }
0276 Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF, QPoint)
0277 friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2)
0278 { return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); }
0279 friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2)
0280 { return QPointF(p1.xp - p2.xp, p1.yp - p2.yp); }
0281 friend constexpr inline QPointF operator*(const QPointF &p, qreal c)
0282 { return QPointF(p.xp * c, p.yp * c); }
0283 friend constexpr inline QPointF operator*(qreal c, const QPointF &p)
0284 { return QPointF(p.xp * c, p.yp * c); }
0285 friend constexpr inline QPointF operator+(const QPointF &p)
0286 { return p; }
0287 friend constexpr inline QPointF operator-(const QPointF &p)
0288 { return QPointF(-p.xp, -p.yp); }
0289 friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor)
0290 {
0291 Q_ASSERT(divisor < 0 || divisor > 0);
0292 return QPointF(p.xp / divisor, p.yp / divisor);
0293 }
0294
0295 public:
0296 constexpr QPoint toPoint() const;
0297
0298 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0299 [[nodiscard]] Q_CORE_EXPORT static QPointF fromCGPoint(CGPoint point) noexcept;
0300 [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
0301 #endif
0302
0303 private:
0304 friend class QTransform;
0305
0306 qreal xp;
0307 qreal yp;
0308
0309 template <std::size_t I,
0310 typename P,
0311 std::enable_if_t<(I < 2), bool> = true,
0312 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPointF>, bool> = true>
0313 friend constexpr decltype(auto) get(P &&p) noexcept
0314 {
0315 if constexpr (I == 0)
0316 return q23::forward_like<P>(p.xp);
0317 else if constexpr (I == 1)
0318 return q23::forward_like<P>(p.yp);
0319 }
0320 };
0321
0322 Q_DECLARE_TYPEINFO(QPointF, Q_PRIMITIVE_TYPE);
0323
0324 size_t qHash(QPointF, size_t seed = 0) = delete;
0325
0326
0327
0328
0329 #ifndef QT_NO_DATASTREAM
0330 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
0331 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
0332 #endif
0333
0334
0335
0336
0337
0338 constexpr inline QPointF::QPointF() noexcept : xp(0), yp(0) { }
0339
0340 constexpr inline QPointF::QPointF(qreal xpos, qreal ypos) noexcept : xp(xpos), yp(ypos) { }
0341
0342 constexpr inline QPointF::QPointF(const QPoint &p) noexcept : xp(p.x()), yp(p.y()) { }
0343
0344 constexpr inline qreal QPointF::manhattanLength() const
0345 {
0346 return qAbs(x()) + qAbs(y());
0347 }
0348
0349 inline bool QPointF::isNull() const noexcept
0350 {
0351 return qIsNull(xp) && qIsNull(yp);
0352 }
0353
0354 constexpr inline qreal QPointF::x() const noexcept
0355 {
0356 return xp;
0357 }
0358
0359 constexpr inline qreal QPointF::y() const noexcept
0360 {
0361 return yp;
0362 }
0363
0364 constexpr inline void QPointF::setX(qreal xpos) noexcept
0365 {
0366 xp = xpos;
0367 }
0368
0369 constexpr inline void QPointF::setY(qreal ypos) noexcept
0370 {
0371 yp = ypos;
0372 }
0373
0374 constexpr inline qreal &QPointF::rx() noexcept
0375 {
0376 return xp;
0377 }
0378
0379 constexpr inline qreal &QPointF::ry() noexcept
0380 {
0381 return yp;
0382 }
0383
0384 constexpr inline QPointF &QPointF::operator+=(const QPointF &p)
0385 {
0386 xp += p.xp;
0387 yp += p.yp;
0388 return *this;
0389 }
0390
0391 constexpr inline QPointF &QPointF::operator-=(const QPointF &p)
0392 {
0393 xp -= p.xp;
0394 yp -= p.yp;
0395 return *this;
0396 }
0397
0398 constexpr inline QPointF &QPointF::operator*=(qreal c)
0399 {
0400 xp *= c;
0401 yp *= c;
0402 return *this;
0403 }
0404
0405 constexpr inline QPointF &QPointF::operator/=(qreal divisor)
0406 {
0407 Q_ASSERT(divisor > 0 || divisor < 0);
0408 xp /= divisor;
0409 yp /= divisor;
0410 return *this;
0411 }
0412
0413 constexpr QPointF QPoint::toPointF() const noexcept { return *this; }
0414
0415 constexpr inline QPoint QPointF::toPoint() const
0416 {
0417 return QPoint(QtPrivate::qSaturateRound(xp), QtPrivate::qSaturateRound(yp));
0418 }
0419
0420 #ifndef QT_NO_DEBUG_STREAM
0421 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
0422 #endif
0423
0424 QT_END_NAMESPACE
0425
0426
0427
0428
0429
0430 namespace std {
0431 template <>
0432 class tuple_size<QT_PREPEND_NAMESPACE(QPoint)> : public integral_constant<size_t, 2> {};
0433 template <>
0434 class tuple_element<0, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
0435 template <>
0436 class tuple_element<1, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
0437
0438 template <>
0439 class tuple_size<QT_PREPEND_NAMESPACE(QPointF)> : public integral_constant<size_t, 2> {};
0440 template <>
0441 class tuple_element<0, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
0442 template <>
0443 class tuple_element<1, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
0444 }
0445
0446 #endif