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

source code of qtbase/src/corelib/tools/qline.h