1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QLINE_H
43 #define QLINE_H
44 
45 #include <QtCore/qpoint.h>
46 
47 QT_BEGIN_HEADER
48 
49 QT_BEGIN_NAMESPACE
50 
QT_MODULE(Core)51 QT_MODULE(Core)
52 
53 /*******************************************************************************
54  * class QLine
55  *******************************************************************************/
56 
57 class Q_CORE_EXPORT QLine
58 {
59 public:
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 
92 private:
93     QPoint pt1, pt2;
94 };
95 Q_DECLARE_TYPEINFO(QLine, Q_MOVABLE_TYPE);
96 
97 /*******************************************************************************
98  * class QLine inline members
99  *******************************************************************************/
100 
QLine()101 inline QLine::QLine() { }
102 
QLine(const QPoint & pt1_,const QPoint & pt2_)103 inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { }
104 
QLine(int x1pos,int y1pos,int x2pos,int y2pos)105 inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { }
106 
isNull()107 inline bool QLine::isNull() const
108 {
109     return pt1 == pt2;
110 }
111 
x1()112 inline int QLine::x1() const
113 {
114     return pt1.x();
115 }
116 
y1()117 inline int QLine::y1() const
118 {
119     return pt1.y();
120 }
121 
x2()122 inline int QLine::x2() const
123 {
124     return pt2.x();
125 }
126 
y2()127 inline int QLine::y2() const
128 {
129     return pt2.y();
130 }
131 
p1()132 inline QPoint QLine::p1() const
133 {
134     return pt1;
135 }
136 
p2()137 inline QPoint QLine::p2() const
138 {
139     return pt2;
140 }
141 
dx()142 inline int QLine::dx() const
143 {
144     return pt2.x() - pt1.x();
145 }
146 
dy()147 inline int QLine::dy() const
148 {
149     return pt2.y() - pt1.y();
150 }
151 
translate(const QPoint & point)152 inline void QLine::translate(const QPoint &point)
153 {
154     pt1 += point;
155     pt2 += point;
156 }
157 
translate(int adx,int ady)158 inline void QLine::translate(int adx, int ady)
159 {
160     this->translate(QPoint(adx, ady));
161 }
162 
translated(const QPoint & p)163 inline QLine QLine::translated(const QPoint &p) const
164 {
165     return QLine(pt1 + p, pt2 + p);
166 }
167 
translated(int adx,int ady)168 inline QLine QLine::translated(int adx, int ady) const
169 {
170     return translated(QPoint(adx, ady));
171 }
172 
setP1(const QPoint & aP1)173 inline void QLine::setP1(const QPoint &aP1)
174 {
175     pt1 = aP1;
176 }
177 
setP2(const QPoint & aP2)178 inline void QLine::setP2(const QPoint &aP2)
179 {
180     pt2 = aP2;
181 }
182 
setPoints(const QPoint & aP1,const QPoint & aP2)183 inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2)
184 {
185     pt1 = aP1;
186     pt2 = aP2;
187 }
188 
setLine(int aX1,int aY1,int aX2,int aY2)189 inline void QLine::setLine(int aX1, int aY1, int aX2, int aY2)
190 {
191     pt1 = QPoint(aX1, aY1);
192     pt2 = QPoint(aX2, aY2);
193 }
194 
195 inline bool QLine::operator==(const QLine &d) const
196 {
197     return pt1 == d.pt1 && pt2 == d.pt2;
198 }
199 
200 #ifndef QT_NO_DEBUG_STREAM
201 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p);
202 #endif
203 
204 #ifndef QT_NO_DATASTREAM
205 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLine &);
206 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLine &);
207 #endif
208 
209 /*******************************************************************************
210  * class QLineF
211  *******************************************************************************/
212 class Q_CORE_EXPORT QLineF {
213 public:
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);
QLineF(const QLine & line)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 
271 private:
272     QPointF pt1, pt2;
273 };
274 Q_DECLARE_TYPEINFO(QLineF, Q_MOVABLE_TYPE);
275 
276 /*******************************************************************************
277  * class QLineF inline members
278  *******************************************************************************/
279 
QLineF()280 inline QLineF::QLineF()
281 {
282 }
283 
QLineF(const QPointF & apt1,const QPointF & apt2)284 inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
285     : pt1(apt1), pt2(apt2)
286 {
287 }
288 
QLineF(qreal x1pos,qreal y1pos,qreal x2pos,qreal y2pos)289 inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
290     : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
291 {
292 }
293 
x1()294 inline qreal QLineF::x1() const
295 {
296     return pt1.x();
297 }
298 
y1()299 inline qreal QLineF::y1() const
300 {
301     return pt1.y();
302 }
303 
x2()304 inline qreal QLineF::x2() const
305 {
306     return pt2.x();
307 }
308 
y2()309 inline qreal QLineF::y2() const
310 {
311     return pt2.y();
312 }
313 
p1()314 inline QPointF QLineF::p1() const
315 {
316     return pt1;
317 }
318 
p2()319 inline QPointF QLineF::p2() const
320 {
321     return pt2;
322 }
323 
dx()324 inline qreal QLineF::dx() const
325 {
326     return pt2.x() - pt1.x();
327 }
328 
dy()329 inline qreal QLineF::dy() const
330 {
331     return pt2.y() - pt1.y();
332 }
333 
normalVector()334 inline QLineF QLineF::normalVector() const
335 {
336     return QLineF(p1(), p1() + QPointF(dy(), -dx()));
337 }
338 
translate(const QPointF & point)339 inline void QLineF::translate(const QPointF &point)
340 {
341     pt1 += point;
342     pt2 += point;
343 }
344 
translate(qreal adx,qreal ady)345 inline void QLineF::translate(qreal adx, qreal ady)
346 {
347     this->translate(QPointF(adx, ady));
348 }
349 
translated(const QPointF & p)350 inline QLineF QLineF::translated(const QPointF &p) const
351 {
352     return QLineF(pt1 + p, pt2 + p);
353 }
354 
translated(qreal adx,qreal ady)355 inline QLineF QLineF::translated(qreal adx, qreal ady) const
356 {
357     return translated(QPointF(adx, ady));
358 }
359 
setLength(qreal len)360 inline 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 
pointAt(qreal t)368 inline 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 
toLine()375 inline QLine QLineF::toLine() const
376 {
377     return QLine(pt1.toPoint(), pt2.toPoint());
378 }
379 
380 
setP1(const QPointF & aP1)381 inline void QLineF::setP1(const QPointF &aP1)
382 {
383     pt1 = aP1;
384 }
385 
setP2(const QPointF & aP2)386 inline void QLineF::setP2(const QPointF &aP2)
387 {
388     pt2 = aP2;
389 }
390 
setPoints(const QPointF & aP1,const QPointF & aP2)391 inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
392 {
393     pt1 = aP1;
394     pt2 = aP2;
395 }
396 
setLine(qreal aX1,qreal aY1,qreal aX2,qreal aY2)397 inline 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 
404 inline 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
412 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p);
413 #endif
414 
415 #ifndef QT_NO_DATASTREAM
416 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &);
417 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLineF &);
418 #endif
419 
420 QT_END_NAMESPACE
421 
422 QT_END_HEADER
423 
424 #endif // QLINE_H
425