Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:09:28

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