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