| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QLINE_H |
| 5 | #define QLINE_H |
| 6 | |
| 7 | #include <QtCore/qpoint.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | class QLineF; |
| 12 | |
| 13 | /******************************************************************************* |
| 14 | * class QLine |
| 15 | *******************************************************************************/ |
| 16 | |
| 17 | class Q_CORE_EXPORT QLine |
| 18 | { |
| 19 | public: |
| 20 | constexpr inline QLine(); |
| 21 | constexpr inline QLine(const QPoint &pt1, const QPoint &pt2); |
| 22 | constexpr inline QLine(int x1, int y1, int x2, int y2); |
| 23 | |
| 24 | constexpr inline bool isNull() const; |
| 25 | |
| 26 | constexpr inline QPoint p1() const; |
| 27 | constexpr inline QPoint p2() const; |
| 28 | |
| 29 | constexpr inline int x1() const; |
| 30 | constexpr inline int y1() const; |
| 31 | |
| 32 | constexpr inline int x2() const; |
| 33 | constexpr inline int y2() const; |
| 34 | |
| 35 | constexpr inline int dx() const; |
| 36 | constexpr inline int dy() const; |
| 37 | |
| 38 | inline void translate(const QPoint &p); |
| 39 | inline void translate(int dx, int dy); |
| 40 | |
| 41 | [[nodiscard]] constexpr inline QLine translated(const QPoint &p) const; |
| 42 | [[nodiscard]] constexpr inline QLine translated(int dx, int dy) const; |
| 43 | |
| 44 | [[nodiscard]] constexpr inline QPoint center() const; |
| 45 | |
| 46 | inline void setP1(const QPoint &p1); |
| 47 | inline void setP2(const QPoint &p2); |
| 48 | inline void setPoints(const QPoint &p1, const QPoint &p2); |
| 49 | inline void setLine(int x1, int y1, int x2, int y2); |
| 50 | |
| 51 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 52 | constexpr inline bool operator==(const QLine &d) const noexcept; |
| 53 | constexpr inline bool operator!=(const QLine &d) const noexcept { return !operator==(d); } |
| 54 | #endif |
| 55 | |
| 56 | [[nodiscard]] constexpr inline QLineF toLineF() const noexcept; |
| 57 | |
| 58 | private: |
| 59 | friend constexpr bool comparesEqual(const QLine &lhs, const QLine &rhs) noexcept |
| 60 | { return lhs.pt1 == rhs.pt1 && lhs.pt2 == rhs.pt2; } |
| 61 | #if !QT_CORE_REMOVED_SINCE(6, 8) |
| 62 | Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLine) |
| 63 | #endif |
| 64 | |
| 65 | QPoint pt1, pt2; |
| 66 | }; |
| 67 | Q_DECLARE_TYPEINFO(QLine, Q_PRIMITIVE_TYPE); |
| 68 | |
| 69 | /******************************************************************************* |
| 70 | * class QLine inline members |
| 71 | *******************************************************************************/ |
| 72 | |
| 73 | constexpr inline QLine::QLine() { } |
| 74 | |
| 75 | constexpr inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { } |
| 76 | |
| 77 | constexpr inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { } |
| 78 | |
| 79 | constexpr inline bool QLine::isNull() const |
| 80 | { |
| 81 | return pt1 == pt2; |
| 82 | } |
| 83 | |
| 84 | constexpr inline int QLine::x1() const |
| 85 | { |
| 86 | return pt1.x(); |
| 87 | } |
| 88 | |
| 89 | constexpr inline int QLine::y1() const |
| 90 | { |
| 91 | return pt1.y(); |
| 92 | } |
| 93 | |
| 94 | constexpr inline int QLine::x2() const |
| 95 | { |
| 96 | return pt2.x(); |
| 97 | } |
| 98 | |
| 99 | constexpr inline int QLine::y2() const |
| 100 | { |
| 101 | return pt2.y(); |
| 102 | } |
| 103 | |
| 104 | constexpr inline QPoint QLine::p1() const |
| 105 | { |
| 106 | return pt1; |
| 107 | } |
| 108 | |
| 109 | constexpr inline QPoint QLine::p2() const |
| 110 | { |
| 111 | return pt2; |
| 112 | } |
| 113 | |
| 114 | constexpr inline int QLine::dx() const |
| 115 | { |
| 116 | return pt2.x() - pt1.x(); |
| 117 | } |
| 118 | |
| 119 | constexpr inline int QLine::dy() const |
| 120 | { |
| 121 | return pt2.y() - pt1.y(); |
| 122 | } |
| 123 | |
| 124 | inline void QLine::translate(const QPoint &point) |
| 125 | { |
| 126 | pt1 += point; |
| 127 | pt2 += point; |
| 128 | } |
| 129 | |
| 130 | inline void QLine::translate(int adx, int ady) |
| 131 | { |
| 132 | this->translate(point: QPoint(adx, ady)); |
| 133 | } |
| 134 | |
| 135 | constexpr inline QLine QLine::translated(const QPoint &p) const |
| 136 | { |
| 137 | return QLine(pt1 + p, pt2 + p); |
| 138 | } |
| 139 | |
| 140 | constexpr inline QLine QLine::translated(int adx, int ady) const |
| 141 | { |
| 142 | return translated(p: QPoint(adx, ady)); |
| 143 | } |
| 144 | |
| 145 | constexpr inline QPoint QLine::center() const |
| 146 | { |
| 147 | return QPoint(int((qint64(pt1.x()) + pt2.x()) / 2), int((qint64(pt1.y()) + pt2.y()) / 2)); |
| 148 | } |
| 149 | |
| 150 | inline void QLine::setP1(const QPoint &aP1) |
| 151 | { |
| 152 | pt1 = aP1; |
| 153 | } |
| 154 | |
| 155 | inline void QLine::setP2(const QPoint &aP2) |
| 156 | { |
| 157 | pt2 = aP2; |
| 158 | } |
| 159 | |
| 160 | inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2) |
| 161 | { |
| 162 | pt1 = aP1; |
| 163 | pt2 = aP2; |
| 164 | } |
| 165 | |
| 166 | inline void QLine::setLine(int aX1, int aY1, int aX2, int aY2) |
| 167 | { |
| 168 | pt1 = QPoint(aX1, aY1); |
| 169 | pt2 = QPoint(aX2, aY2); |
| 170 | } |
| 171 | |
| 172 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 173 | constexpr inline bool QLine::operator==(const QLine &d) const noexcept |
| 174 | { |
| 175 | return comparesEqual(*this, d); |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #ifndef QT_NO_DEBUG_STREAM |
| 180 | Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p); |
| 181 | #endif |
| 182 | |
| 183 | #ifndef QT_NO_DATASTREAM |
| 184 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLine &); |
| 185 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLine &); |
| 186 | #endif |
| 187 | |
| 188 | /******************************************************************************* |
| 189 | * class QLineF |
| 190 | *******************************************************************************/ |
| 191 | class Q_CORE_EXPORT QLineF |
| 192 | { |
| 193 | public: |
| 194 | |
| 195 | enum IntersectionType { NoIntersection, BoundedIntersection, UnboundedIntersection }; |
| 196 | using IntersectType = IntersectionType; // deprecated name |
| 197 | |
| 198 | constexpr inline QLineF(); |
| 199 | constexpr inline QLineF(const QPointF &pt1, const QPointF &pt2); |
| 200 | constexpr inline QLineF(qreal x1, qreal y1, qreal x2, qreal y2); |
| 201 | constexpr inline QLineF(const QLine &line) : pt1(line.p1()), pt2(line.p2()) { } |
| 202 | |
| 203 | [[nodiscard]] static QLineF fromPolar(qreal length, qreal angle); |
| 204 | |
| 205 | constexpr bool isNull() const; |
| 206 | |
| 207 | constexpr inline QPointF p1() const; |
| 208 | constexpr inline QPointF p2() const; |
| 209 | |
| 210 | constexpr inline qreal x1() const; |
| 211 | constexpr inline qreal y1() const; |
| 212 | |
| 213 | constexpr inline qreal x2() const; |
| 214 | constexpr inline qreal y2() const; |
| 215 | |
| 216 | constexpr inline qreal dx() const; |
| 217 | constexpr inline qreal dy() const; |
| 218 | |
| 219 | qreal length() const; |
| 220 | void setLength(qreal len); |
| 221 | |
| 222 | qreal angle() const; |
| 223 | void setAngle(qreal angle); |
| 224 | |
| 225 | qreal angleTo(const QLineF &l) const; |
| 226 | |
| 227 | [[nodiscard]] QLineF unitVector() const; |
| 228 | [[nodiscard]] constexpr inline QLineF normalVector() const; |
| 229 | |
| 230 | IntersectionType intersects(const QLineF &l, QPointF *intersectionPoint = nullptr) const; |
| 231 | |
| 232 | constexpr inline QPointF pointAt(qreal t) const; |
| 233 | inline void translate(const QPointF &p); |
| 234 | inline void translate(qreal dx, qreal dy); |
| 235 | |
| 236 | [[nodiscard]] constexpr inline QLineF translated(const QPointF &p) const; |
| 237 | [[nodiscard]] constexpr inline QLineF translated(qreal dx, qreal dy) const; |
| 238 | |
| 239 | [[nodiscard]] constexpr inline QPointF center() const; |
| 240 | |
| 241 | inline void setP1(const QPointF &p1); |
| 242 | inline void setP2(const QPointF &p2); |
| 243 | inline void setPoints(const QPointF &p1, const QPointF &p2); |
| 244 | inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2); |
| 245 | |
| 246 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 247 | constexpr inline bool operator==(const QLineF &d) const; |
| 248 | constexpr inline bool operator!=(const QLineF &d) const { return !operator==(d); } |
| 249 | #endif |
| 250 | |
| 251 | constexpr QLine toLine() const; |
| 252 | |
| 253 | private: |
| 254 | friend constexpr bool comparesEqual(const QLineF &lhs, const QLineF &rhs) noexcept |
| 255 | { return lhs.pt1 == rhs.pt1 && lhs.pt2 == rhs.pt2; } |
| 256 | #if !QT_CORE_REMOVED_SINCE(6, 8) |
| 257 | Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF) |
| 258 | #endif |
| 259 | |
| 260 | friend constexpr bool comparesEqual(const QLineF &lhs, const QLine &rhs) noexcept |
| 261 | { return comparesEqual(lhs, rhs: rhs.toLineF()); } |
| 262 | Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF, QLine) |
| 263 | |
| 264 | friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept |
| 265 | { return qFuzzyCompare(p1: lhs.pt1, p2: rhs.pt1) && qFuzzyCompare(p1: lhs.pt2, p2: rhs.pt2); } |
| 266 | |
| 267 | friend constexpr bool qFuzzyIsNull(const QLineF &line) noexcept |
| 268 | { return qFuzzyCompare(p1: line.pt1, p2: line.pt2); } |
| 269 | |
| 270 | QPointF pt1, pt2; |
| 271 | }; |
| 272 | Q_DECLARE_TYPEINFO(QLineF, Q_PRIMITIVE_TYPE); |
| 273 | |
| 274 | /******************************************************************************* |
| 275 | * class QLineF inline members |
| 276 | *******************************************************************************/ |
| 277 | |
| 278 | constexpr inline QLineF::QLineF() |
| 279 | { |
| 280 | } |
| 281 | |
| 282 | constexpr inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2) |
| 283 | : pt1(apt1), pt2(apt2) |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | constexpr inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos) |
| 288 | : pt1(x1pos, y1pos), pt2(x2pos, y2pos) |
| 289 | { |
| 290 | } |
| 291 | |
| 292 | constexpr inline qreal QLineF::x1() const |
| 293 | { |
| 294 | return pt1.x(); |
| 295 | } |
| 296 | |
| 297 | constexpr inline qreal QLineF::y1() const |
| 298 | { |
| 299 | return pt1.y(); |
| 300 | } |
| 301 | |
| 302 | constexpr inline qreal QLineF::x2() const |
| 303 | { |
| 304 | return pt2.x(); |
| 305 | } |
| 306 | |
| 307 | constexpr inline qreal QLineF::y2() const |
| 308 | { |
| 309 | return pt2.y(); |
| 310 | } |
| 311 | |
| 312 | constexpr inline bool QLineF::isNull() const |
| 313 | { |
| 314 | return qFuzzyCompare(p1: pt1, p2: pt2); |
| 315 | } |
| 316 | |
| 317 | constexpr inline QPointF QLineF::p1() const |
| 318 | { |
| 319 | return pt1; |
| 320 | } |
| 321 | |
| 322 | constexpr inline QPointF QLineF::p2() const |
| 323 | { |
| 324 | return pt2; |
| 325 | } |
| 326 | |
| 327 | constexpr inline qreal QLineF::dx() const |
| 328 | { |
| 329 | return pt2.x() - pt1.x(); |
| 330 | } |
| 331 | |
| 332 | constexpr inline qreal QLineF::dy() const |
| 333 | { |
| 334 | return pt2.y() - pt1.y(); |
| 335 | } |
| 336 | |
| 337 | constexpr inline QLineF QLineF::normalVector() const |
| 338 | { |
| 339 | return QLineF(p1(), p1() + QPointF(dy(), -dx())); |
| 340 | } |
| 341 | |
| 342 | inline void QLineF::translate(const QPointF &point) |
| 343 | { |
| 344 | pt1 += point; |
| 345 | pt2 += point; |
| 346 | } |
| 347 | |
| 348 | inline void QLineF::translate(qreal adx, qreal ady) |
| 349 | { |
| 350 | this->translate(point: QPointF(adx, ady)); |
| 351 | } |
| 352 | |
| 353 | constexpr inline QLineF QLineF::translated(const QPointF &p) const |
| 354 | { |
| 355 | return QLineF(pt1 + p, pt2 + p); |
| 356 | } |
| 357 | |
| 358 | constexpr inline QLineF QLineF::translated(qreal adx, qreal ady) const |
| 359 | { |
| 360 | return translated(p: QPointF(adx, ady)); |
| 361 | } |
| 362 | |
| 363 | constexpr inline QPointF QLineF::center() const |
| 364 | { |
| 365 | return QPointF(0.5 * pt1.x() + 0.5 * pt2.x(), 0.5 * pt1.y() + 0.5 * pt2.y()); |
| 366 | } |
| 367 | |
| 368 | inline void QLineF::setLength(qreal len) |
| 369 | { |
| 370 | Q_ASSERT(qIsFinite(len)); |
| 371 | const qreal oldLength = length(); |
| 372 | Q_ASSERT(qIsFinite(oldLength)); |
| 373 | // Scale len by dx() / length() and dy() / length(), two O(1) quantities, |
| 374 | // rather than scaling dx() and dy() by len / length(), which might overflow. |
| 375 | if (oldLength > 0) |
| 376 | pt2 = QPointF(pt1.x() + len * (dx() / oldLength), pt1.y() + len * (dy() / oldLength)); |
| 377 | } |
| 378 | |
| 379 | constexpr inline QPointF QLineF::pointAt(qreal t) const |
| 380 | { |
| 381 | return QPointF(pt1.x() + (pt2.x() - pt1.x()) * t, pt1.y() + (pt2.y() - pt1.y()) * t); |
| 382 | } |
| 383 | |
| 384 | constexpr inline QLineF QLine::toLineF() const noexcept { return *this; } |
| 385 | |
| 386 | constexpr inline QLine QLineF::toLine() const |
| 387 | { |
| 388 | return QLine(pt1.toPoint(), pt2.toPoint()); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | inline void QLineF::setP1(const QPointF &aP1) |
| 393 | { |
| 394 | pt1 = aP1; |
| 395 | } |
| 396 | |
| 397 | inline void QLineF::setP2(const QPointF &aP2) |
| 398 | { |
| 399 | pt2 = aP2; |
| 400 | } |
| 401 | |
| 402 | inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2) |
| 403 | { |
| 404 | pt1 = aP1; |
| 405 | pt2 = aP2; |
| 406 | } |
| 407 | |
| 408 | inline void QLineF::setLine(qreal aX1, qreal aY1, qreal aX2, qreal aY2) |
| 409 | { |
| 410 | pt1 = QPointF(aX1, aY1); |
| 411 | pt2 = QPointF(aX2, aY2); |
| 412 | } |
| 413 | |
| 414 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 415 | constexpr inline bool QLineF::operator==(const QLineF &d) const |
| 416 | { |
| 417 | return comparesEqual(*this, d); |
| 418 | } |
| 419 | #endif |
| 420 | |
| 421 | |
| 422 | #ifndef QT_NO_DEBUG_STREAM |
| 423 | Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p); |
| 424 | #endif |
| 425 | |
| 426 | #ifndef QT_NO_DATASTREAM |
| 427 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &); |
| 428 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLineF &); |
| 429 | #endif |
| 430 | |
| 431 | QT_END_NAMESPACE |
| 432 | |
| 433 | #endif // QLINE_H |
| 434 | |