1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "qdatastream.h"
32
#include <private/qnumeric_p.h>
39
\brief The QLine class provides a two-dimensional vector that
40
uses integer point accuracy for coordinates.
42
A QLine describes a finite length line (or line segment) on a two-dimensional surface.
43
The start and end points of the line are specified using integer point
46
Use isNull() to determine whether the QLine represents a valid line
49
The positions of the line's end points can be found with the p1(),
50
x1(), y1(), p2(), x2(), and y2() functions. The horizontal and vertical
51
components of the line are returned by the dx() and dy() functions.
53
The line can be translated along the length of another line with the
56
\sa QPoint QSize QRect QLineF
62
Constructs a null line.
66
\fn QLine::QLine(const QPoint &pt1, const QPoint &pt2)
68
Constructs a line object that represents the line between \a pt1 and
73
\fn QLine::QLine(int x1, int y1, int x2, int y2)
75
Constructs a line object that represents the line between (\a x1, \a y1) and
80
\fn bool QLine::isNull() const
82
Returns true if the line is not set up with valid start and end point;
83
otherwise returns false.
87
\fn QPoint QLine::p1() const
89
Returns the line's start point.
95
\fn QPoint QLine::p2() const
97
Returns the line's end point.
103
\fn int QLine::x1() const
105
Returns the x-coordinate of the line's start point.
111
\fn int QLine::y1() const
113
Returns the y-coordinate of the line's start point.
119
\fn int QLine::x2() const
121
Returns the x-coordinate of the line's end point.
127
\fn int QLine::y2() const
129
Returns the y-coordinate of the line's end point.
135
\fn int QLine::dx() const
137
Returns the horizontal component of the line's vector.
143
\fn int QLine::dy() const
145
Returns the vertical component of the line's vector.
151
\fn bool QLine::operator!=(const QLine &other) const
153
Returns true if \a other is not the same as this line.
155
A line is different from another line if any of their points are
156
different or their order is different.
160
\fn bool QLine::operator==(const QLine &other) const
162
Returns true if \a other is the same line as this line.
164
A line is identical to another line if the points are the same and their
169
\fn void QLine::translate(const QPoint &point)
171
Translates this line with the \a point given.
175
\fn void QLine::translate(int dx, int dy)
179
Translates this line the distance \a dx and \a dy.
182
#ifndef QT_NO_DEBUG_STREAM
183
QDebug operator<<(QDebug d, const QLine &p)
185
d << "QLine(" << p.p1() << "," << p.p2() << ")";
190
#ifndef QT_NO_DATASTREAM
194
Writes the \a line to the \a stream and returns a reference to
197
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
200
QDataStream &operator<<(QDataStream &stream, const QLine &line)
202
stream << line.p1() << line.p2();
209
Reads a QLine from the \a stream into the \a line and returns a
210
reference to the stream.
212
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
215
QDataStream &operator>>(QDataStream &stream, QLine &line)
220
line = QLine(p1, p2);
225
#endif // QT_NO_DATASTREAM
229
#define M_2PI 6.28318530717958647692528676655900576
235
\brief The QLineF class provides a two-dimensional vector that
236
uses floating point accuracy for coordinates.
238
A QLineF describes a finite length line (or line segment) on a two-dimensional surface.
239
The start and end points of the line are specified using floating point
242
Use isNull() to determine whether the QLineF represents a valid line
245
The positions of the line's end points can be found with the p1(), x1(),
246
y1(), p2(), x2(), and y2() functions. The horizontal and vertical
247
components of the line are returned by the dx() and dy() functions.
249
Convenience functions are provided for finding the lines's length(),
250
the unitVector() along the line, whether two lines intersect(), and
251
the angle() between two lines. The line's length can be changed using
254
The line can be translated along the length of another line with the
255
moveBy() function, and can be traversed using a parameter with the
258
\sa QPointF QSizeF QRectF QLine
262
\enum QLineF::IntersectType
264
\value NoIntersection Indicates that the lines do not intersect;
265
i.e. they are parallel.
267
\value UnboundedIntersection The two lines intersect,
268
but not within the range defined by their lengths. This will be
269
the case if the lines are not parallel.
271
\img qlinef-unbounded.png
273
\value BoundedIntersection The two lines intersect with each other
274
within the start and end points of each line.
276
\img qlinef-bounded.png
283
Constructs a null line.
287
\fn QLineF::QLineF(const QPointF &pt1, const QPointF &pt2)
289
Constructs a line object that represents the line between \a pt1 and
294
\fn QLineF::QLineF(qreal x1, qreal y1, qreal x2, qreal y2)
296
Constructs a line object that represents the line between (\a x1, \a y1) and
301
\fn QLineF::QLineF(const QLine &line)
303
Construct a QLineF from a integer-based QLine \a line.
307
Returns true if the line is not set up with valid start and end point;
308
otherwise returns false.
311
bool QLineF::isNull() const
313
return (qFuzzyCompare(pt1.x(), pt2.x()) && qFuzzyCompare(pt1.y(), pt2.y())) ? true : false;
318
\fn QPointF QLineF::p1() const
320
Returns the line's start point.
326
\fn QPointF QLineF::p2() const
328
Returns the line's end point.
334
\fn QLine QLineF::toLine() const
336
Returns a QLine. The returned QLine's starting and end points are rounded to
341
\fn qreal QLineF::x1() const
343
Returns the x-coordinate of the line's start point.
349
\fn qreal QLineF::y1() const
351
Returns the y-coordinate of the line's start point.
357
\fn qreal QLineF::x2() const
359
Returns the x-coordinate of the line's end point.
365
\fn qreal QLineF::y2() const
367
Returns the y-coordinate of the line's end point.
373
\fn qreal QLineF::dx() const
375
Returns the horizontal component of the line's vector.
381
\fn qreal QLineF::dy() const
383
Returns the vertical component of the line's vector.
389
\fn QLineF::setLength(qreal length)
391
Sets the \a length of the line.
397
\fn QLineF QLineF::normalVector() const
399
Returns a line that is perpendicular to this line with the same starting
404
\img qlinef-normalvector.png
408
\fn bool QLineF::operator!=(const QLineF &other) const
410
Returns true if \a other is not the same as this line.
412
A line is different from another line if any of their points are
413
different or their order is different.
417
\fn bool QLineF::operator==(const QLineF &other) const
419
Returns true if \a other is the same line as this line.
421
A line is identical if the two points are the same and their
426
\fn qreal QLineF::pointAt(qreal t) const
428
Returns the point at the parameterized position \a t, where
429
the start and end point are defined to be at positions t=0 and t=1.
435
Returns the length of the line.
439
qreal QLineF::length() const
441
qreal x = pt2.x() - pt1.x();
442
qreal y = pt2.y() - pt1.y();
443
return sqrt(x*x + y*y);
448
Returns a normalized version of this line, starting at the same
449
point as this line. A normalized line is a line of unit length
450
(length() is equal to 1.0).
454
QLineF QLineF::unitVector() const
456
qreal x = pt2.x() - pt1.x();
457
qreal y = pt2.y() - pt1.y();
459
qreal len = sqrt(x*x + y*y);
460
QLineF f(p1(), QPointF(pt1.x() + x/len, pt1.y() + y/len));
463
if (qAbs(f.length() - 1) >= 0.001)
464
qWarning("QLine::unitVector(), new line does not have length of 1");
470
#define SAME_SIGNS(a, b) ((a) * (b) >= 0)
472
// Line intersection algorithm, copied from Graphics Gems II
473
static bool qt_linef_intersect(qreal x1, qreal y1, qreal x2, qreal y2,
474
qreal x3, qreal y3, qreal x4, qreal y4)
476
qreal a1, a2, b1, b2, c1, c2; /* Coefficients of line eqns. */
477
qreal r1, r2, r3, r4; /* 'Sign' values */
481
c1 = x2 * y1 - x1 * y2;
483
r3 = a1 * x3 + b1 * y3 + c1;
484
r4 = a1 * x4 + b1 * y4 + c1;
486
if ( r3 != 0 && r4 != 0 && SAME_SIGNS( r3, r4 ))
491
c2 = x4 * y3 - x3 * y4;
493
r1 = a2 * x1 + b2 * y1 + c2;
494
r2 = a2 * x2 + b2 * y2 + c2;
496
if ( r1 != 0 && r2 != 0 && SAME_SIGNS( r1, r2 ))
503
\fn QLineF::IntersectType QLineF::intersect(const QLineF &other, QPointF *intersectionPoint) const
505
Returns a value indicating whether or not this line intersects the
506
\a other line. By passing a valid pointer as \a intersectionPoint, it
507
is possible to get the actual intersection point. The intersection
508
point is undefined if the lines are parallel.
511
QLineF::IntersectType QLineF::intersect(const QLineF &l, QPointF *intersectionPoint) const
513
if (isNull() || l.isNull()
514
|| !qIsFinite(pt1.x()) || !qIsFinite(pt1.y()) || !qIsFinite(pt2.x()) || !qIsFinite(pt2.y())
515
|| !qIsFinite(l.pt1.x()) || !qIsFinite(l.pt1.y()) || !qIsFinite(l.pt2.x()) || !qIsFinite(l.pt2.y()))
516
return NoIntersection;
519
IntersectType type = qt_linef_intersect(pt1.x(), pt1.y(), pt2.x(), pt2.y(),
520
l.x1(), l.y1(), l.x2(), l.y2())
521
? BoundedIntersection : UnboundedIntersection;
523
// For special case where one of the lines are vertical
524
if (dx() == 0 && l.dx() == 0) {
525
type = NoIntersection;
526
} else if (dx() == 0) {
527
qreal la = l.dy() / l.dx();
528
isect = QPointF(pt1.x(), la * pt1.x() + l.y1() - la*l.x1());
529
} else if (l.dx() == 0) {
530
qreal ta = dy() / dx();
531
isect = QPointF(l.x1(), ta * l.x1() + y1() - ta*x1());
533
qreal ta = dy()/dx();
534
qreal la = l.dy()/l.dx();
535
if (ta == la) // no intersection
536
return NoIntersection;
538
qreal x = ( - l.y1() + la * l.x1() + pt1.y() - ta * pt1.x() ) / (la - ta);
539
isect = QPointF(x, ta*(x - pt1.x()) + pt1.y());
541
if (intersectionPoint)
542
*intersectionPoint = isect;
547
\fn void QLineF::translate(const QPointF &point)
549
Translates this line with the \a point given.
553
\fn void QLineF::translate(qreal dx, qreal dy)
557
Translates this line the distance \a dx and \a dy.
561
\fn qreal QLineF::angle(const QLineF &line) const
563
Returns the smallest angle between the given \a line and this line, not
564
taking into account whether the lines intersect or not. The angle is
565
specified in degrees.
569
qreal QLineF::angle(const QLineF &l) const
571
if (isNull() || l.isNull())
573
qreal rad = acos( (dx()*l.dx() + dy()*l.dy()) / (length()*l.length()) );
574
return rad * 360 / M_2PI;
578
#ifndef QT_NO_DEBUG_STREAM
579
QDebug operator<<(QDebug d, const QLineF &p)
581
d << "QLineF(" << p.p1() << "," << p.p2() << ")";
586
#ifndef QT_NO_DATASTREAM
590
Writes the \a line to the \a stream and returns a reference to
593
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
596
QDataStream &operator<<(QDataStream &stream, const QLineF &line)
598
stream << line.p1() << line.p2();
605
Reads a QLineF from the \a stream into the \a line and returns a
606
reference to the stream.
608
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
611
QDataStream &operator>>(QDataStream &stream, QLineF &line)
616
line = QLineF(start, end);
621
#endif // QT_NO_DATASTREAM