Qt 4.8
qline.h
Go to the documentation of this file.
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 
48 
50 
51 QT_MODULE(Core)
52 
53 /*******************************************************************************
54  * class QLine
55  *******************************************************************************/
56 
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 };
96 
97 /*******************************************************************************
98  * class QLine inline members
99  *******************************************************************************/
100 
101 inline QLine::QLine() { }
102 
103 inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { }
104 
105 inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { }
106 
107 inline bool QLine::isNull() const
108 {
109  return pt1 == pt2;
110 }
111 
112 inline int QLine::x1() const
113 {
114  return pt1.x();
115 }
116 
117 inline int QLine::y1() const
118 {
119  return pt1.y();
120 }
121 
122 inline int QLine::x2() const
123 {
124  return pt2.x();
125 }
126 
127 inline int QLine::y2() const
128 {
129  return pt2.y();
130 }
131 
132 inline QPoint QLine::p1() const
133 {
134  return pt1;
135 }
136 
137 inline QPoint QLine::p2() const
138 {
139  return pt2;
140 }
141 
142 inline int QLine::dx() const
143 {
144  return pt2.x() - pt1.x();
145 }
146 
147 inline int QLine::dy() const
148 {
149  return pt2.y() - pt1.y();
150 }
151 
152 inline void QLine::translate(const QPoint &point)
153 {
154  pt1 += point;
155  pt2 += point;
156 }
157 
158 inline void QLine::translate(int adx, int ady)
159 {
160  this->translate(QPoint(adx, ady));
161 }
162 
163 inline QLine QLine::translated(const QPoint &p) const
164 {
165  return QLine(pt1 + p, pt2 + p);
166 }
167 
168 inline QLine QLine::translated(int adx, int ady) const
169 {
170  return translated(QPoint(adx, ady));
171 }
172 
173 inline void QLine::setP1(const QPoint &aP1)
174 {
175  pt1 = aP1;
176 }
177 
178 inline void QLine::setP2(const QPoint &aP2)
179 {
180  pt2 = aP2;
181 }
182 
183 inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2)
184 {
185  pt1 = aP1;
186  pt2 = aP2;
187 }
188 
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
202 #endif
203 
204 #ifndef QT_NO_DATASTREAM
207 #endif
208 
209 /*******************************************************************************
210  * class QLineF
211  *******************************************************************************/
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);
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:
273 };
275 
276 /*******************************************************************************
277  * class QLineF inline members
278  *******************************************************************************/
279 
281 {
282 }
283 
284 inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
285  : pt1(apt1), pt2(apt2)
286 {
287 }
288 
289 inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
290  : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
291 {
292 }
293 
294 inline qreal QLineF::x1() const
295 {
296  return pt1.x();
297 }
298 
299 inline qreal QLineF::y1() const
300 {
301  return pt1.y();
302 }
303 
304 inline qreal QLineF::x2() const
305 {
306  return pt2.x();
307 }
308 
309 inline qreal QLineF::y2() const
310 {
311  return pt2.y();
312 }
313 
314 inline QPointF QLineF::p1() const
315 {
316  return pt1;
317 }
318 
319 inline QPointF QLineF::p2() const
320 {
321  return pt2;
322 }
323 
324 inline qreal QLineF::dx() const
325 {
326  return pt2.x() - pt1.x();
327 }
328 
329 inline qreal QLineF::dy() const
330 {
331  return pt2.y() - pt1.y();
332 }
333 
335 {
336  return QLineF(p1(), p1() + QPointF(dy(), -dx()));
337 }
338 
339 inline void QLineF::translate(const QPointF &point)
340 {
341  pt1 += point;
342  pt2 += point;
343 }
344 
345 inline void QLineF::translate(qreal adx, qreal ady)
346 {
347  this->translate(QPointF(adx, ady));
348 }
349 
350 inline QLineF QLineF::translated(const QPointF &p) const
351 {
352  return QLineF(pt1 + p, pt2 + p);
353 }
354 
355 inline QLineF QLineF::translated(qreal adx, qreal ady) const
356 {
357  return translated(QPointF(adx, ady));
358 }
359 
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 
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 
375 inline QLine QLineF::toLine() const
376 {
377  return QLine(pt1.toPoint(), pt2.toPoint());
378 }
379 
380 
381 inline void QLineF::setP1(const QPointF &aP1)
382 {
383  pt1 = aP1;
384 }
385 
386 inline void QLineF::setP2(const QPointF &aP2)
387 {
388  pt2 = aP2;
389 }
390 
391 inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
392 {
393  pt1 = aP1;
394  pt2 = aP2;
395 }
396 
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
413 #endif
414 
415 #ifndef QT_NO_DATASTREAM
418 #endif
419 
421 
423 
424 #endif // QLINE_H
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
bool operator==(const QLineF &d) const
Returns true if the given line is the same as this line.
Definition: qline.h:404
void setP1(const QPointF &p1)
Sets the starting point of this line to p1.
Definition: qline.h:381
QDataStream & operator>>(QDataStream &stream, QLine &line)
Reads a line from the given stream into the given line and returns a reference to the stream...
Definition: qline.cpp:368
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isNull() const
Returns true if the line is not set up with valid start and end point; otherwise returns false...
Definition: qline.h:107
#define QT_MODULE(x)
Definition: qglobal.h:2783
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
QPoint pt2
Definition: qline.h:93
QPoint p1() const
Returns the line&#39;s start point.
Definition: qline.h:132
qreal x2() const
Returns the x-coordinate of the line&#39;s end point.
Definition: qline.h:304
QPointF p1() const
Returns the line&#39;s start point.
Definition: qline.h:314
QPoint p2() const
Returns the line&#39;s end point.
Definition: qline.h:137
qreal y2() const
Returns the y-coordinate of the line&#39;s end point.
Definition: qline.h:309
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
void setP1(const QPoint &p1)
Sets the starting point of this line to p1.
Definition: qline.h:173
void setPoints(const QPoint &p1, const QPoint &p2)
Sets the start point of this line to p1 and the end point of this line to p2.
Definition: qline.h:183
void setPoints(const QPointF &p1, const QPointF &p2)
Sets the start point of this line to p1 and the end point of this line to p2.
Definition: qline.h:391
qreal dy() const
Returns the vertical component of the line&#39;s vector.
Definition: qline.h:329
QLineF unitVector() const
Returns the unit vector for this line, i.e a line starting at the same point as this line with a leng...
Definition: qline.cpp:786
QDataStream & operator>>(QDataStream &stream, QLineF &line)
Reads a line from the given stream into the given line and returns a reference to the stream...
Definition: qline.cpp:1044
int y1() const
Returns the y-coordinate of the line&#39;s start point.
Definition: qline.h:117
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
void translate(const QPointF &p)
Translates this line by the given offset.
Definition: qline.h:339
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QPointF pt1
Definition: qline.h:272
void translate(const QPoint &p)
Translates this line by the given offset.
Definition: qline.h:152
QDataStream & operator<<(QDataStream &stream, const QLineF &line)
Writes the given line to the given stream and returns a reference to the stream.
Definition: qline.cpp:1026
bool operator==(const QLine &d) const
Returns true if the given line is the same as this line.
Definition: qline.h:195
QDataStream & operator<<(QDataStream &stream, const QLine &line)
Writes the given line to the given stream and returns a reference to the stream.
Definition: qline.cpp:350
IntersectType
Describes the intersection between two lines.
Definition: qline.h:215
qreal y1() const
Returns the y-coordinate of the line&#39;s start point.
Definition: qline.h:299
void setP2(const QPointF &p2)
Sets the end point of this line to p2.
Definition: qline.h:386
int x2() const
Returns the x-coordinate of the line&#39;s end point.
Definition: qline.h:122
QLineF translated(const QPointF &p) const
Returns this line translated by the given offset.
Definition: qline.h:350
void setLength(qreal len)
Sets the length of the line to the given length.
Definition: qline.h:360
qreal dx() const
Returns the horizontal component of the line&#39;s vector.
Definition: qline.h:324
int dx() const
Returns the horizontal component of the line&#39;s vector.
Definition: qline.h:142
qreal angle(const QPointF &p1, const QPointF &p2)
int dy() const
Returns the vertical component of the line&#39;s vector.
Definition: qline.h:147
bool operator!=(const QLine &d) const
Returns true if the given line is not the same as this line.
Definition: qline.h:90
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
QLine()
Constructs a null line.
Definition: qline.h:101
QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition: qpoint.h:376
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QPointF p2() const
Returns the line&#39;s end point.
Definition: qline.h:319
QFactoryLoader * l
void setP2(const QPoint &p2)
Sets the end point of this line to p2.
Definition: qline.h:178
QPointF pt2
Definition: qline.h:272
bool operator==(const T *o, const QPointer< T > &p)
Definition: qpointer.h:87
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
bool operator!=(const QLineF &d) const
Returns true if the given line is not the same as this line.
Definition: qline.h:267
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
QLineF normalVector() const
Returns a line that is perpendicular to this line with the same starting point and length...
Definition: qline.h:334
QPoint pt1
Definition: qline.h:93
Q_DECLARE_TYPEINFO(QLine, Q_MOVABLE_TYPE)
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
int y2() const
Returns the y-coordinate of the line&#39;s end point.
Definition: qline.h:127
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
QLineF(const QLine &line)
Construct a QLineF object from the given integer-based line.
Definition: qline.h:220
QLineF()
Constructs a null line.
Definition: qline.h:280
QLine toLine() const
Returns an integer based copy of this line.
Definition: qline.h:375
#define class
qreal x1() const
Returns the x-coordinate of the line&#39;s start point.
Definition: qline.h:294
int x1() const
Returns the x-coordinate of the line&#39;s start point.
Definition: qline.h:112
QLine translated(const QPoint &p) const
Returns this line translated by the given offset.
Definition: qline.h:163
#define QT_END_HEADER
Definition: qglobal.h:137
bool isNull() const
Returns true if the line is not set up with valid start and end point; otherwise returns false...
Definition: qline.cpp:517
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
void setLine(qreal x1, qreal y1, qreal x2, qreal y2)
Sets this line to the start in x1, y1 and end in x2, y2.
Definition: qline.h:397
void setLine(int x1, int y1, int x2, int y2)
Sets this line to the start in x1, y1 and end in x2, y2.
Definition: qline.h:189
QPointF pointAt(qreal t) const
Returns the point at the parameterized position specified by t.
Definition: qline.h:368