Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qline.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 QLINE_H
0005 #define QLINE_H
0006 
0007 #include <QtCore/qpoint.h>
0008 
0009 QT_BEGIN_NAMESPACE
0010 
0011 class QDataStream;
0012 class QLineF;
0013 
0014 /*******************************************************************************
0015  * class QLine
0016  *******************************************************************************/
0017 
0018 class Q_CORE_EXPORT QLine
0019 {
0020 public:
0021     constexpr inline QLine();
0022     constexpr inline QLine(const QPoint &pt1, const QPoint &pt2);
0023     constexpr inline QLine(int x1, int y1, int x2, int y2);
0024 
0025     constexpr inline bool isNull() const;
0026 
0027     constexpr inline QPoint p1() const;
0028     constexpr inline QPoint p2() const;
0029 
0030     constexpr inline int x1() const;
0031     constexpr inline int y1() const;
0032 
0033     constexpr inline int x2() const;
0034     constexpr inline int y2() const;
0035 
0036     constexpr inline int dx() const;
0037     constexpr inline int dy() const;
0038 
0039     inline void translate(const QPoint &p);
0040     inline void translate(int dx, int dy);
0041 
0042     [[nodiscard]] constexpr inline QLine translated(const QPoint &p) const;
0043     [[nodiscard]] constexpr inline QLine translated(int dx, int dy) const;
0044 
0045     [[nodiscard]] constexpr inline QPoint center() const;
0046 
0047     inline void setP1(const QPoint &p1);
0048     inline void setP2(const QPoint &p2);
0049     inline void setPoints(const QPoint &p1, const QPoint &p2);
0050     inline void setLine(int x1, int y1, int x2, int y2);
0051 
0052 #if QT_CORE_REMOVED_SINCE(6, 8)
0053     constexpr inline bool operator==(const QLine &d) const noexcept;
0054     constexpr inline bool operator!=(const QLine &d) const noexcept { return !operator==(d); }
0055 #endif
0056 
0057     [[nodiscard]] constexpr inline QLineF toLineF() const noexcept;
0058 
0059 private:
0060     friend constexpr bool comparesEqual(const QLine &lhs, const QLine &rhs) noexcept
0061     { return lhs.pt1 == rhs.pt1 && lhs.pt2 == rhs.pt2; }
0062 #if !QT_CORE_REMOVED_SINCE(6, 8)
0063     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLine)
0064 #endif
0065 
0066     QPoint pt1, pt2;
0067 };
0068 Q_DECLARE_TYPEINFO(QLine, Q_PRIMITIVE_TYPE);
0069 
0070 /*******************************************************************************
0071  * class QLine inline members
0072  *******************************************************************************/
0073 
0074 constexpr inline QLine::QLine() { }
0075 
0076 constexpr inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { }
0077 
0078 constexpr inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { }
0079 
0080 constexpr inline bool QLine::isNull() const
0081 {
0082     return pt1 == pt2;
0083 }
0084 
0085 constexpr inline int QLine::x1() const
0086 {
0087     return pt1.x();
0088 }
0089 
0090 constexpr inline int QLine::y1() const
0091 {
0092     return pt1.y();
0093 }
0094 
0095 constexpr inline int QLine::x2() const
0096 {
0097     return pt2.x();
0098 }
0099 
0100 constexpr inline int QLine::y2() const
0101 {
0102     return pt2.y();
0103 }
0104 
0105 constexpr inline QPoint QLine::p1() const
0106 {
0107     return pt1;
0108 }
0109 
0110 constexpr inline QPoint QLine::p2() const
0111 {
0112     return pt2;
0113 }
0114 
0115 constexpr inline int QLine::dx() const
0116 {
0117     return pt2.x() - pt1.x();
0118 }
0119 
0120 constexpr inline int QLine::dy() const
0121 {
0122     return pt2.y() - pt1.y();
0123 }
0124 
0125 inline void QLine::translate(const QPoint &point)
0126 {
0127     pt1 += point;
0128     pt2 += point;
0129 }
0130 
0131 inline void QLine::translate(int adx, int ady)
0132 {
0133     this->translate(QPoint(adx, ady));
0134 }
0135 
0136 constexpr inline QLine QLine::translated(const QPoint &p) const
0137 {
0138     return QLine(pt1 + p, pt2 + p);
0139 }
0140 
0141 constexpr inline QLine QLine::translated(int adx, int ady) const
0142 {
0143     return translated(QPoint(adx, ady));
0144 }
0145 
0146 constexpr inline QPoint QLine::center() const
0147 {
0148     return QPoint(int((qint64(pt1.x()) + pt2.x()) / 2), int((qint64(pt1.y()) + pt2.y()) / 2));
0149 }
0150 
0151 inline void QLine::setP1(const QPoint &aP1)
0152 {
0153     pt1 = aP1;
0154 }
0155 
0156 inline void QLine::setP2(const QPoint &aP2)
0157 {
0158     pt2 = aP2;
0159 }
0160 
0161 inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2)
0162 {
0163     pt1 = aP1;
0164     pt2 = aP2;
0165 }
0166 
0167 inline void QLine::setLine(int aX1, int aY1, int aX2, int aY2)
0168 {
0169     pt1 = QPoint(aX1, aY1);
0170     pt2 = QPoint(aX2, aY2);
0171 }
0172 
0173 #if QT_CORE_REMOVED_SINCE(6, 8)
0174 constexpr inline bool QLine::operator==(const QLine &d) const noexcept
0175 {
0176     return comparesEqual(*this, d);
0177 }
0178 #endif
0179 
0180 #ifndef QT_NO_DEBUG_STREAM
0181 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p);
0182 #endif
0183 
0184 #ifndef QT_NO_DATASTREAM
0185 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLine &);
0186 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLine &);
0187 #endif
0188 
0189 /*******************************************************************************
0190  * class QLineF
0191  *******************************************************************************/
0192 class Q_CORE_EXPORT QLineF
0193 {
0194 public:
0195 
0196     enum IntersectionType { NoIntersection, BoundedIntersection, UnboundedIntersection };
0197     using IntersectType = IntersectionType; // deprecated name
0198 
0199     constexpr inline QLineF();
0200     constexpr inline QLineF(const QPointF &pt1, const QPointF &pt2);
0201     constexpr inline QLineF(qreal x1, qreal y1, qreal x2, qreal y2);
0202     constexpr inline QLineF(const QLine &line) : pt1(line.p1()), pt2(line.p2()) { }
0203 
0204     [[nodiscard]] static QLineF fromPolar(qreal length, qreal angle);
0205 
0206     constexpr bool isNull() const;
0207 
0208     constexpr inline QPointF p1() const;
0209     constexpr inline QPointF p2() const;
0210 
0211     constexpr inline qreal x1() const;
0212     constexpr inline qreal y1() const;
0213 
0214     constexpr inline qreal x2() const;
0215     constexpr inline qreal y2() const;
0216 
0217     constexpr inline qreal dx() const;
0218     constexpr inline qreal dy() const;
0219 
0220     qreal length() const;
0221     void setLength(qreal len);
0222 
0223     qreal angle() const;
0224     void setAngle(qreal angle);
0225 
0226     qreal angleTo(const QLineF &l) const;
0227 
0228     [[nodiscard]] QLineF unitVector() const;
0229     [[nodiscard]] constexpr inline QLineF normalVector() const;
0230 
0231     IntersectionType intersects(const QLineF &l, QPointF *intersectionPoint = nullptr) const;
0232 
0233     constexpr inline QPointF pointAt(qreal t) const;
0234     inline void translate(const QPointF &p);
0235     inline void translate(qreal dx, qreal dy);
0236 
0237     [[nodiscard]] constexpr inline QLineF translated(const QPointF &p) const;
0238     [[nodiscard]] constexpr inline QLineF translated(qreal dx, qreal dy) const;
0239 
0240     [[nodiscard]] constexpr inline QPointF center() const;
0241 
0242     inline void setP1(const QPointF &p1);
0243     inline void setP2(const QPointF &p2);
0244     inline void setPoints(const QPointF &p1, const QPointF &p2);
0245     inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2);
0246 
0247 #if QT_CORE_REMOVED_SINCE(6, 8)
0248     constexpr inline bool operator==(const QLineF &d) const;
0249     constexpr inline bool operator!=(const QLineF &d) const { return !operator==(d); }
0250 #endif
0251 
0252     constexpr QLine toLine() const;
0253 
0254 private:
0255     friend constexpr bool comparesEqual(const QLineF &lhs, const QLineF &rhs) noexcept
0256     { return lhs.pt1 == rhs.pt1 && lhs.pt2 == rhs.pt2; }
0257 #if !QT_CORE_REMOVED_SINCE(6, 8)
0258     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF)
0259 #endif
0260 
0261     friend constexpr bool comparesEqual(const QLineF &lhs, const QLine &rhs) noexcept
0262     { return comparesEqual(lhs, rhs.toLineF()); }
0263     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF, QLine)
0264 
0265     friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept
0266     { return qFuzzyCompare(lhs.pt1, rhs.pt1) && qFuzzyCompare(lhs.pt2, rhs.pt2); }
0267 
0268     friend constexpr bool qFuzzyIsNull(const QLineF &line) noexcept
0269     { return qFuzzyCompare(line.pt1, line.pt2); }
0270 
0271     QPointF pt1, pt2;
0272 };
0273 Q_DECLARE_TYPEINFO(QLineF, Q_PRIMITIVE_TYPE);
0274 
0275 /*******************************************************************************
0276  * class QLineF inline members
0277  *******************************************************************************/
0278 
0279 constexpr inline QLineF::QLineF()
0280 {
0281 }
0282 
0283 constexpr inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
0284     : pt1(apt1), pt2(apt2)
0285 {
0286 }
0287 
0288 constexpr inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
0289     : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
0290 {
0291 }
0292 
0293 constexpr inline qreal QLineF::x1() const
0294 {
0295     return pt1.x();
0296 }
0297 
0298 constexpr inline qreal QLineF::y1() const
0299 {
0300     return pt1.y();
0301 }
0302 
0303 constexpr inline qreal QLineF::x2() const
0304 {
0305     return pt2.x();
0306 }
0307 
0308 constexpr inline qreal QLineF::y2() const
0309 {
0310     return pt2.y();
0311 }
0312 
0313 constexpr inline bool QLineF::isNull() const
0314 {
0315     return qFuzzyCompare(pt1, pt2);
0316 }
0317 
0318 constexpr inline QPointF QLineF::p1() const
0319 {
0320     return pt1;
0321 }
0322 
0323 constexpr inline QPointF QLineF::p2() const
0324 {
0325     return pt2;
0326 }
0327 
0328 constexpr inline qreal QLineF::dx() const
0329 {
0330     return pt2.x() - pt1.x();
0331 }
0332 
0333 constexpr inline qreal QLineF::dy() const
0334 {
0335     return pt2.y() - pt1.y();
0336 }
0337 
0338 constexpr inline QLineF QLineF::normalVector() const
0339 {
0340     return QLineF(p1(), p1() + QPointF(dy(), -dx()));
0341 }
0342 
0343 inline void QLineF::translate(const QPointF &point)
0344 {
0345     pt1 += point;
0346     pt2 += point;
0347 }
0348 
0349 inline void QLineF::translate(qreal adx, qreal ady)
0350 {
0351     this->translate(QPointF(adx, ady));
0352 }
0353 
0354 constexpr inline QLineF QLineF::translated(const QPointF &p) const
0355 {
0356     return QLineF(pt1 + p, pt2 + p);
0357 }
0358 
0359 constexpr inline QLineF QLineF::translated(qreal adx, qreal ady) const
0360 {
0361     return translated(QPointF(adx, ady));
0362 }
0363 
0364 constexpr inline QPointF QLineF::center() const
0365 {
0366     return QPointF(0.5 * pt1.x() + 0.5 * pt2.x(), 0.5 * pt1.y() + 0.5 * pt2.y());
0367 }
0368 
0369 inline void QLineF::setLength(qreal len)
0370 {
0371     Q_ASSERT(qIsFinite(len));
0372     const qreal oldLength = length();
0373     Q_ASSERT(qIsFinite(oldLength));
0374     // Scale len by dx() / length() and dy() / length(), two O(1) quantities,
0375     // rather than scaling dx() and dy() by len / length(), which might overflow.
0376     if (oldLength > 0)
0377         pt2 = QPointF(pt1.x() + len * (dx() / oldLength), pt1.y() + len * (dy() / oldLength));
0378 }
0379 
0380 constexpr inline QPointF QLineF::pointAt(qreal t) const
0381 {
0382     return QPointF(pt1.x() + (pt2.x() - pt1.x()) * t, pt1.y() + (pt2.y() - pt1.y()) * t);
0383 }
0384 
0385 constexpr inline QLineF QLine::toLineF() const noexcept { return *this; }
0386 
0387 constexpr inline QLine QLineF::toLine() const
0388 {
0389     return QLine(pt1.toPoint(), pt2.toPoint());
0390 }
0391 
0392 
0393 inline void QLineF::setP1(const QPointF &aP1)
0394 {
0395     pt1 = aP1;
0396 }
0397 
0398 inline void QLineF::setP2(const QPointF &aP2)
0399 {
0400     pt2 = aP2;
0401 }
0402 
0403 inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
0404 {
0405     pt1 = aP1;
0406     pt2 = aP2;
0407 }
0408 
0409 inline void QLineF::setLine(qreal aX1, qreal aY1, qreal aX2, qreal aY2)
0410 {
0411     pt1 = QPointF(aX1, aY1);
0412     pt2 = QPointF(aX2, aY2);
0413 }
0414 
0415 #if QT_CORE_REMOVED_SINCE(6, 8)
0416 constexpr inline bool QLineF::operator==(const QLineF &d) const
0417 {
0418     return comparesEqual(*this, d);
0419 }
0420 #endif
0421 
0422 
0423 #ifndef QT_NO_DEBUG_STREAM
0424 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p);
0425 #endif
0426 
0427 #ifndef QT_NO_DATASTREAM
0428 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &);
0429 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLineF &);
0430 #endif
0431 
0432 QT_END_NAMESPACE
0433 
0434 #endif // QLINE_H