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