1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
45 #include <QtCore/qpoint.h>
52 /*******************************************************************************
54 *******************************************************************************/
56 class Q_CORE_EXPORT QLine
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);
63 Q_DECL_CONSTEXPR inline bool isNull() const;
65 Q_DECL_CONSTEXPR inline QPoint p1() const;
66 Q_DECL_CONSTEXPR inline QPoint p2() const;
68 Q_DECL_CONSTEXPR inline int x1() const;
69 Q_DECL_CONSTEXPR inline int y1() const;
71 Q_DECL_CONSTEXPR inline int x2() const;
72 Q_DECL_CONSTEXPR inline int y2() const;
74 Q_DECL_CONSTEXPR inline int dx() const;
75 Q_DECL_CONSTEXPR inline int dy() const;
77 inline void translate(const QPoint &p);
78 inline void translate(int dx, int dy);
80 Q_DECL_CONSTEXPR inline QLine translated(const QPoint &p) const;
81 Q_DECL_CONSTEXPR inline QLine translated(int dx, int dy) const;
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);
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); }
94 Q_DECLARE_TYPEINFO(QLine, Q_MOVABLE_TYPE);
96 /*******************************************************************************
97 * class QLine inline members
98 *******************************************************************************/
100 Q_DECL_CONSTEXPR inline QLine::QLine() { }
102 Q_DECL_CONSTEXPR inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { }
104 Q_DECL_CONSTEXPR inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { }
106 Q_DECL_CONSTEXPR inline bool QLine::isNull() const
111 Q_DECL_CONSTEXPR inline int QLine::x1() const
116 Q_DECL_CONSTEXPR inline int QLine::y1() const
121 Q_DECL_CONSTEXPR inline int QLine::x2() const
126 Q_DECL_CONSTEXPR inline int QLine::y2() const
131 Q_DECL_CONSTEXPR inline QPoint QLine::p1() const
136 Q_DECL_CONSTEXPR inline QPoint QLine::p2() const
141 Q_DECL_CONSTEXPR inline int QLine::dx() const
143 return pt2.x() - pt1.x();
146 Q_DECL_CONSTEXPR inline int QLine::dy() const
148 return pt2.y() - pt1.y();
151 inline void QLine::translate(const QPoint &point)
157 inline void QLine::translate(int adx, int ady)
159 this->translate(QPoint(adx, ady));
162 Q_DECL_CONSTEXPR inline QLine QLine::translated(const QPoint &p) const
164 return QLine(pt1 + p, pt2 + p);
167 Q_DECL_CONSTEXPR inline QLine QLine::translated(int adx, int ady) const
169 return translated(QPoint(adx, ady));
172 inline void QLine::setP1(const QPoint &aP1)
177 inline void QLine::setP2(const QPoint &aP2)
182 inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2)
188 inline void QLine::setLine(int aX1, int aY1, int aX2, int aY2)
190 pt1 = QPoint(aX1, aY1);
191 pt2 = QPoint(aX2, aY2);
194 Q_DECL_CONSTEXPR inline bool QLine::operator==(const QLine &d) const
196 return pt1 == d.pt1 && pt2 == d.pt2;
199 #ifndef QT_NO_DEBUG_STREAM
200 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p);
203 #ifndef QT_NO_DATASTREAM
204 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLine &);
205 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLine &);
208 /*******************************************************************************
210 *******************************************************************************/
211 class Q_CORE_EXPORT QLineF {
214 enum IntersectType { NoIntersection, BoundedIntersection, UnboundedIntersection };
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()) { }
221 static QLineF fromPolar(qreal length, qreal angle);
223 Q_DECL_CONSTEXPR bool isNull() const;
225 Q_DECL_CONSTEXPR inline QPointF p1() const;
226 Q_DECL_CONSTEXPR inline QPointF p2() const;
228 Q_DECL_CONSTEXPR inline qreal x1() const;
229 Q_DECL_CONSTEXPR inline qreal y1() const;
231 Q_DECL_CONSTEXPR inline qreal x2() const;
232 Q_DECL_CONSTEXPR inline qreal y2() const;
234 Q_DECL_CONSTEXPR inline qreal dx() const;
235 Q_DECL_CONSTEXPR inline qreal dy() const;
237 qreal length() const;
238 void setLength(qreal len);
241 void setAngle(qreal angle);
243 qreal angleTo(const QLineF &l) const;
245 QLineF unitVector() const;
246 Q_DECL_CONSTEXPR inline QLineF normalVector() const;
248 // ### Qt 5: rename intersects() or intersection() and rename IntersectType IntersectionType
249 IntersectType intersect(const QLineF &l, QPointF *intersectionPoint) const;
251 qreal angle(const QLineF &l) const;
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);
257 Q_DECL_CONSTEXPR inline QLineF translated(const QPointF &p) const;
258 Q_DECL_CONSTEXPR inline QLineF translated(qreal dx, qreal dy) const;
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);
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); }
268 Q_DECL_CONSTEXPR QLine toLine() const;
273 Q_DECLARE_TYPEINFO(QLineF, Q_MOVABLE_TYPE);
275 /*******************************************************************************
276 * class QLineF inline members
277 *******************************************************************************/
279 Q_DECL_CONSTEXPR inline QLineF::QLineF()
283 Q_DECL_CONSTEXPR inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
284 : pt1(apt1), pt2(apt2)
288 Q_DECL_CONSTEXPR inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
289 : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
293 Q_DECL_CONSTEXPR inline qreal QLineF::x1() const
298 Q_DECL_CONSTEXPR inline qreal QLineF::y1() const
303 Q_DECL_CONSTEXPR inline qreal QLineF::x2() const
308 Q_DECL_CONSTEXPR inline qreal QLineF::y2() const
313 Q_DECL_CONSTEXPR inline bool QLineF::isNull() const
315 return qFuzzyCompare(pt1.x(), pt2.x()) && qFuzzyCompare(pt1.y(), pt2.y());
318 Q_DECL_CONSTEXPR inline QPointF QLineF::p1() const
323 Q_DECL_CONSTEXPR inline QPointF QLineF::p2() const
328 Q_DECL_CONSTEXPR inline qreal QLineF::dx() const
330 return pt2.x() - pt1.x();
333 Q_DECL_CONSTEXPR inline qreal QLineF::dy() const
335 return pt2.y() - pt1.y();
338 Q_DECL_CONSTEXPR inline QLineF QLineF::normalVector() const
340 return QLineF(p1(), p1() + QPointF(dy(), -dx()));
343 inline void QLineF::translate(const QPointF &point)
349 inline void QLineF::translate(qreal adx, qreal ady)
351 this->translate(QPointF(adx, ady));
354 Q_DECL_CONSTEXPR inline QLineF QLineF::translated(const QPointF &p) const
356 return QLineF(pt1 + p, pt2 + p);
359 Q_DECL_CONSTEXPR inline QLineF QLineF::translated(qreal adx, qreal ady) const
361 return translated(QPointF(adx, ady));
364 inline void QLineF::setLength(qreal len)
368 QLineF v = unitVector();
369 pt2 = QPointF(pt1.x() + v.dx() * len, pt1.y() + v.dy() * len);
372 Q_DECL_CONSTEXPR inline QPointF QLineF::pointAt(qreal t) const
374 return QPointF(pt1.x() + (pt2.x() - pt1.x()) * t, pt1.y() + (pt2.y() - pt1.y()) * t);
377 Q_DECL_CONSTEXPR inline QLine QLineF::toLine() const
379 return QLine(pt1.toPoint(), pt2.toPoint());
383 inline void QLineF::setP1(const QPointF &aP1)
388 inline void QLineF::setP2(const QPointF &aP2)
393 inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
399 inline void QLineF::setLine(qreal aX1, qreal aY1, qreal aX2, qreal aY2)
401 pt1 = QPointF(aX1, aY1);
402 pt2 = QPointF(aX2, aY2);
406 Q_DECL_CONSTEXPR inline bool QLineF::operator==(const QLineF &d) const
408 return pt1 == d.pt1 && pt2 == d.pt2;
413 #ifndef QT_NO_DEBUG_STREAM
414 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p);
417 #ifndef QT_NO_DATASTREAM
418 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &);
419 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLineF &);