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