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