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
219 Q_DECL_CONSTEXPR inline QLineF();
220 Q_DECL_CONSTEXPR inline QLineF(const QPointF &pt1, const QPointF &pt2);
221 Q_DECL_CONSTEXPR inline QLineF(qreal x1, qreal y1, qreal x2, qreal y2);
222 Q_DECL_CONSTEXPR inline QLineF(const QLine &line) : pt1(line.p1()), pt2(line.p2()) { }
223
224 Q_REQUIRED_RESULT static QLineF fromPolar(qreal length, qreal angle);
225
226 Q_DECL_CONSTEXPR bool isNull() const;
227
228 Q_DECL_CONSTEXPR inline QPointF p1() const;
229 Q_DECL_CONSTEXPR inline QPointF p2() const;
230
231 Q_DECL_CONSTEXPR inline qreal x1() const;
232 Q_DECL_CONSTEXPR inline qreal y1() const;
233
234 Q_DECL_CONSTEXPR inline qreal x2() const;
235 Q_DECL_CONSTEXPR inline qreal y2() const;
236
237 Q_DECL_CONSTEXPR inline qreal dx() const;
238 Q_DECL_CONSTEXPR inline qreal dy() const;
239
240 qreal length() const;
241 void setLength(qreal len);
242
243 qreal angle() const;
244 void setAngle(qreal angle);
245
246 qreal angleTo(const QLineF &l) const;
247
248 Q_REQUIRED_RESULT QLineF unitVector() const;
249 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QLineF normalVector() const;
250
251 // ### Qt 6: rename intersects() or intersection() and rename IntersectType IntersectionType
252 IntersectType intersect(const QLineF &l, QPointF *intersectionPoint) const;
253
254 qreal angle(const QLineF &l) const;
255
256 Q_DECL_CONSTEXPR inline QPointF pointAt(qreal t) const;
257 inline void translate(const QPointF &p);
258 inline void translate(qreal dx, qreal dy);
259
260 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QLineF translated(const QPointF &p) const;
261 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QLineF translated(qreal dx, qreal dy) const;
262
263 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QPointF center() const;
264
265 inline void setP1(const QPointF &p1);
266 inline void setP2(const QPointF &p2);
267 inline void setPoints(const QPointF &p1, const QPointF &p2);
268 inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2);
269
270 Q_DECL_CONSTEXPR inline bool operator==(const QLineF &d) const;
271 Q_DECL_CONSTEXPR inline bool operator!=(const QLineF &d) const { return !(*this == d); }
272
273 Q_DECL_CONSTEXPR QLine toLine() const;
274
275private:
276 QPointF pt1, pt2;
277};
278Q_DECLARE_TYPEINFO(QLineF, Q_MOVABLE_TYPE);
279
280/*******************************************************************************
281 * class QLineF inline members
282 *******************************************************************************/
283
284Q_DECL_CONSTEXPR inline QLineF::QLineF()
285{
286}
287
288Q_DECL_CONSTEXPR inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
289 : pt1(apt1), pt2(apt2)
290{
291}
292
293Q_DECL_CONSTEXPR inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
294 : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
295{
296}
297
298Q_DECL_CONSTEXPR inline qreal QLineF::x1() const
299{
300 return pt1.x();
301}
302
303Q_DECL_CONSTEXPR inline qreal QLineF::y1() const
304{
305 return pt1.y();
306}
307
308Q_DECL_CONSTEXPR inline qreal QLineF::x2() const
309{
310 return pt2.x();
311}
312
313Q_DECL_CONSTEXPR inline qreal QLineF::y2() const
314{
315 return pt2.y();
316}
317
318Q_DECL_CONSTEXPR inline bool QLineF::isNull() const
319{
320 return qFuzzyCompare(pt1.x(), pt2.x()) && qFuzzyCompare(pt1.y(), pt2.y());
321}
322
323Q_DECL_CONSTEXPR inline QPointF QLineF::p1() const
324{
325 return pt1;
326}
327
328Q_DECL_CONSTEXPR inline QPointF QLineF::p2() const
329{
330 return pt2;
331}
332
333Q_DECL_CONSTEXPR inline qreal QLineF::dx() const
334{
335 return pt2.x() - pt1.x();
336}
337
338Q_DECL_CONSTEXPR inline qreal QLineF::dy() const
339{
340 return pt2.y() - pt1.y();
341}
342
343Q_DECL_CONSTEXPR inline QLineF QLineF::normalVector() const
344{
345 return QLineF(p1(), p1() + QPointF(dy(), -dx()));
346}
347
348inline void QLineF::translate(const QPointF &point)
349{
350 pt1 += point;
351 pt2 += point;
352}
353
354inline void QLineF::translate(qreal adx, qreal ady)
355{
356 this->translate(QPointF(adx, ady));
357}
358
359Q_DECL_CONSTEXPR inline QLineF QLineF::translated(const QPointF &p) const
360{
361 return QLineF(pt1 + p, pt2 + p);
362}
363
364Q_DECL_CONSTEXPR inline QLineF QLineF::translated(qreal adx, qreal ady) const
365{
366 return translated(QPointF(adx, ady));
367}
368
369Q_DECL_CONSTEXPR inline QPointF QLineF::center() const
370{
371 return QPointF(0.5 * pt1.x() + 0.5 * pt2.x(), 0.5 * pt1.y() + 0.5 * pt2.y());
372}
373
374inline void QLineF::setLength(qreal len)
375{
376 if (isNull())
377 return;
378 QLineF v = unitVector();
379 pt2 = QPointF(pt1.x() + v.dx() * len, pt1.y() + v.dy() * len);
380}
381
382Q_DECL_CONSTEXPR inline QPointF QLineF::pointAt(qreal t) const
383{
384 return QPointF(pt1.x() + (pt2.x() - pt1.x()) * t, pt1.y() + (pt2.y() - pt1.y()) * t);
385}
386
387Q_DECL_CONSTEXPR inline QLine QLineF::toLine() const
388{
389 return QLine(pt1.toPoint(), pt2.toPoint());
390}
391
392
393inline void QLineF::setP1(const QPointF &aP1)
394{
395 pt1 = aP1;
396}
397
398inline void QLineF::setP2(const QPointF &aP2)
399{
400 pt2 = aP2;
401}
402
403inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
404{
405 pt1 = aP1;
406 pt2 = aP2;
407}
408
409inline void QLineF::setLine(qreal aX1, qreal aY1, qreal aX2, qreal aY2)
410{
411 pt1 = QPointF(aX1, aY1);
412 pt2 = QPointF(aX2, aY2);
413}
414
415
416Q_DECL_CONSTEXPR inline bool QLineF::operator==(const QLineF &d) const
417{
418 return pt1 == d.pt1 && pt2 == d.pt2;
419}
420
421
422
423#ifndef QT_NO_DEBUG_STREAM
424Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p);
425#endif
426
427#ifndef QT_NO_DATASTREAM
428Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &);
429Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLineF &);
430#endif
431
432QT_END_NAMESPACE
433
434#endif // QLINE_H
435