Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qline.h
Go to the documentation of this file.
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
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#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
58private:
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)
63#endif
64
65 QPoint pt1, pt2;
66};
68
69/*******************************************************************************
70 * class QLine inline members
71 *******************************************************************************/
72
73constexpr inline QLine::QLine() { }
74
75constexpr inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { }
76
77constexpr inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { }
78
79constexpr inline bool QLine::isNull() const
80{
81 return pt1 == pt2;
82}
83
84constexpr inline int QLine::x1() const
85{
86 return pt1.x();
87}
88
89constexpr inline int QLine::y1() const
90{
91 return pt1.y();
92}
93
94constexpr inline int QLine::x2() const
95{
96 return pt2.x();
97}
98
99constexpr inline int QLine::y2() const
100{
101 return pt2.y();
102}
103
104constexpr inline QPoint QLine::p1() const
105{
106 return pt1;
107}
108
109constexpr inline QPoint QLine::p2() const
110{
111 return pt2;
112}
113
114constexpr inline int QLine::dx() const
115{
116 return pt2.x() - pt1.x();
117}
118
119constexpr inline int QLine::dy() const
120{
121 return pt2.y() - pt1.y();
122}
123
124inline void QLine::translate(const QPoint &point)
125{
126 pt1 += point;
127 pt2 += point;
128}
129
130inline void QLine::translate(int adx, int ady)
131{
132 this->translate(QPoint(adx, ady));
133}
134
135constexpr inline QLine QLine::translated(const QPoint &p) const
136{
137 return QLine(pt1 + p, pt2 + p);
138}
139
140constexpr inline QLine QLine::translated(int adx, int ady) const
141{
142 return translated(QPoint(adx, ady));
143}
144
145constexpr 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
150inline void QLine::setP1(const QPoint &aP1)
151{
152 pt1 = aP1;
153}
154
155inline void QLine::setP2(const QPoint &aP2)
156{
157 pt2 = aP2;
158}
159
160inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2)
161{
162 pt1 = aP1;
163 pt2 = aP2;
164}
165
166inline 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)
173constexpr 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
180Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p);
181#endif
182
183#ifndef QT_NO_DATASTREAM
184Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLine &);
186#endif
187
188/*******************************************************************************
189 * class QLineF
190 *******************************************************************************/
191class Q_CORE_EXPORT QLineF
192{
193public:
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
253private:
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)
258#endif
259
260 friend constexpr bool comparesEqual(const QLineF &lhs, const QLine &rhs) noexcept
261 { return comparesEqual(lhs, rhs.toLineF()); }
263
264 friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept
265 { return qFuzzyCompare(lhs.pt1, rhs.pt1) && qFuzzyCompare(lhs.pt2, rhs.pt2); }
266
267 friend constexpr bool qFuzzyIsNull(const QLineF &line) noexcept
268 { return qFuzzyCompare(line.pt1, line.pt2); }
269
270 QPointF pt1, pt2;
271};
273
274/*******************************************************************************
275 * class QLineF inline members
276 *******************************************************************************/
277
278constexpr inline QLineF::QLineF()
279{
280}
281
282constexpr inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
283 : pt1(apt1), pt2(apt2)
284{
285}
286
287constexpr inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
288 : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
289{
290}
291
292constexpr inline qreal QLineF::x1() const
293{
294 return pt1.x();
295}
296
297constexpr inline qreal QLineF::y1() const
298{
299 return pt1.y();
300}
301
302constexpr inline qreal QLineF::x2() const
303{
304 return pt2.x();
305}
306
307constexpr inline qreal QLineF::y2() const
308{
309 return pt2.y();
310}
311
312constexpr inline bool QLineF::isNull() const
313{
314 return qFuzzyCompare(pt1, pt2);
315}
316
317constexpr inline QPointF QLineF::p1() const
318{
319 return pt1;
320}
321
322constexpr inline QPointF QLineF::p2() const
323{
324 return pt2;
325}
326
327constexpr inline qreal QLineF::dx() const
328{
329 return pt2.x() - pt1.x();
330}
331
332constexpr inline qreal QLineF::dy() const
333{
334 return pt2.y() - pt1.y();
335}
336
337constexpr inline QLineF QLineF::normalVector() const
338{
339 return QLineF(p1(), p1() + QPointF(dy(), -dx()));
340}
341
342inline void QLineF::translate(const QPointF &point)
343{
344 pt1 += point;
345 pt2 += point;
346}
347
348inline void QLineF::translate(qreal adx, qreal ady)
349{
350 this->translate(QPointF(adx, ady));
351}
352
353constexpr inline QLineF QLineF::translated(const QPointF &p) const
354{
355 return QLineF(pt1 + p, pt2 + p);
356}
357
358constexpr inline QLineF QLineF::translated(qreal adx, qreal ady) const
359{
360 return translated(QPointF(adx, ady));
361}
362
363constexpr 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
369{
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
379constexpr 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
384constexpr inline QLineF QLine::toLineF() const noexcept { return *this; }
385
386constexpr inline QLine QLineF::toLine() const
387{
388 return QLine(pt1.toPoint(), pt2.toPoint());
389}
390
391
392inline void QLineF::setP1(const QPointF &aP1)
393{
394 pt1 = aP1;
395}
396
397inline void QLineF::setP2(const QPointF &aP2)
398{
399 pt2 = aP2;
400}
401
402inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
403{
404 pt1 = aP1;
405 pt2 = aP2;
406}
407
408inline 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)
415constexpr 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
423Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p);
424#endif
425
426#ifndef QT_NO_DATASTREAM
427Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &);
429#endif
430
432
433#endif // QLINE_H
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
constexpr QPointF p1() const
Returns the line's start point.
Definition qline.h:317
constexpr qreal x1() const
Returns the x-coordinate of the line's start point.
Definition qline.h:292
constexpr QLine toLine() const
Returns an integer-based copy of this line.
Definition qline.h:386
void setP2(const QPointF &p2)
Definition qline.h:397
constexpr bool isNull() const
Returns true if the line does not have distinct start and end points; otherwise returns false.
Definition qline.h:312
constexpr qreal y2() const
Returns the y-coordinate of the line's end point.
Definition qline.h:307
friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept
Definition qline.h:264
friend constexpr bool comparesEqual(const QLineF &lhs, const QLineF &rhs) noexcept
Definition qline.h:254
void translate(const QPointF &p)
Translates this line by the given offset.
Definition qline.h:342
constexpr qreal dx() const
Returns the horizontal component of the line's vector.
Definition qline.h:327
void setPoints(const QPointF &p1, const QPointF &p2)
Definition qline.h:402
constexpr QLineF translated(const QPointF &p) const
Definition qline.h:353
constexpr qreal dy() const
Returns the vertical component of the line's vector.
Definition qline.h:332
constexpr QLineF()
Constructs a null line.
Definition qline.h:278
qreal length() const
Returns the length of the line.
Definition qline.cpp:548
IntersectionType
\typealias QLineF::IntersectType
Definition qline.h:195
@ BoundedIntersection
Definition qline.h:195
constexpr QLineF(const QLine &line)
Construct a QLineF object from the given integer-based line.
Definition qline.h:201
friend constexpr bool comparesEqual(const QLineF &lhs, const QLine &rhs) noexcept
Definition qline.h:260
constexpr QLineF normalVector() const
Returns a line that is perpendicular to this line with the same starting point and length.
Definition qline.h:337
constexpr QPointF center() const
Definition qline.h:363
friend constexpr bool qFuzzyIsNull(const QLineF &line) noexcept
Definition qline.h:267
void setP1(const QPointF &p1)
Definition qline.h:392
constexpr qreal x2() const
Returns the x-coordinate of the line's end point.
Definition qline.h:302
constexpr QPointF p2() const
Returns the line's end point.
Definition qline.h:322
constexpr QPointF pointAt(qreal t) const
Returns the point at the position specified by finite parameter t.
Definition qline.h:379
void setLine(qreal x1, qreal y1, qreal x2, qreal y2)
Definition qline.h:408
void setLength(qreal len)
Sets the length of the line to the given finite length.
Definition qline.h:368
constexpr qreal y1() const
Returns the y-coordinate of the line's start point.
Definition qline.h:297
\inmodule QtCore\compares equality \compareswith equality QLineF \endcompareswith
Definition qline.h:18
constexpr int x2() const
Returns the x-coordinate of the line's end point.
Definition qline.h:94
void setPoints(const QPoint &p1, const QPoint &p2)
Definition qline.h:160
constexpr int y2() const
Returns the y-coordinate of the line's end point.
Definition qline.h:99
constexpr int y1() const
Returns the y-coordinate of the line's start point.
Definition qline.h:89
friend constexpr bool comparesEqual(const QLine &lhs, const QLine &rhs) noexcept
Definition qline.h:59
constexpr int dx() const
Returns the horizontal component of the line's vector.
Definition qline.h:114
constexpr QLine()
Constructs a null line.
Definition qline.h:73
void setP2(const QPoint &p2)
Definition qline.h:155
void setLine(int x1, int y1, int x2, int y2)
Definition qline.h:166
constexpr QPoint p1() const
Returns the line's start point.
Definition qline.h:104
constexpr QPoint p2() const
Returns the line's end point.
Definition qline.h:109
constexpr int x1() const
Returns the x-coordinate of the line's start point.
Definition qline.h:84
constexpr int dy() const
Returns the vertical component of the line's vector.
Definition qline.h:119
constexpr bool isNull() const
Returns true if the line does not have distinct start and end points; otherwise returns false.
Definition qline.h:79
constexpr QLine translated(const QPoint &p) const
Definition qline.h:135
void setP1(const QPoint &p1)
Definition qline.h:150
constexpr QLineF toLineF() const noexcept
Definition qline.h:384
void translate(const QPoint &p)
Translates this line by the given offset.
Definition qline.h:124
constexpr QPoint center() const
Definition qline.h:145
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
QPixmap p2
QPixmap p1
[0]
Combined button and popup list for selecting options.
#define Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(...)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:285
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p)
Definition qline.cpp:245
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QLine &)
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint GLfloat GLfloat GLfloat x1
GLenum GLuint GLenum GLsizei length
GLfloat angle
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
GLenum GLsizei len
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
static bool translate(xcb_connection_t *connection, xcb_window_t child, xcb_window_t parent, int *x, int *y)