Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:27

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