Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-05 09:27:25

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