Qt 4.8
qline.cpp
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 #include "qline.h"
43 #include "qdebug.h"
44 #include "qdatastream.h"
45 #include "qmath.h"
46 #include <private/qnumeric_p.h>
47 
49 
329 #ifndef QT_NO_DEBUG_STREAM
331 {
332  d << "QLine(" << p.p1() << ',' << p.p2() << ')';
333  return d;
334 }
335 #endif
336 
337 #ifndef QT_NO_DATASTREAM
338 
351 {
352  stream << line.p1() << line.p2();
353  return stream;
354 }
355 
369 {
370  QPoint p1, p2;
371  stream >> p1;
372  stream >> p2;
373  line = QLine(p1, p2);
374 
375  return stream;
376 }
377 
378 #endif // QT_NO_DATASTREAM
379 
380 inline static qreal q_deg2rad(qreal x)
381 {
382  return x * qreal(0.01745329251994329576923690768489); /* pi/180 */
383 }
384 
385 inline static qreal q_rad2deg(qreal x)
386 {
387  return x * qreal(57.295779513082320876798154814105); /* 180/pi */
388 }
389 
517 bool QLineF::isNull() const
518 {
519  return (qFuzzyCompare(pt1.x(), pt2.x()) && qFuzzyCompare(pt1.y(), pt2.y())) ? true : false;
520 }
521 
522 
699 {
700  qreal x = pt2.x() - pt1.x();
701  qreal y = pt2.y() - pt1.y();
702  return qSqrt(x*x + y*y);
703 }
704 
720 {
721  const qreal dx = pt2.x() - pt1.x();
722  const qreal dy = pt2.y() - pt1.y();
723 
724  const qreal theta = q_rad2deg(qAtan2(-dy, dx));
725 
726  const qreal theta_normalized = theta < 0 ? theta + 360 : theta;
727 
728  if (qFuzzyCompare(theta_normalized, qreal(360)))
729  return qreal(0);
730  else
731  return theta_normalized;
732 }
733 
750 {
751  const qreal angleR = q_deg2rad(angle);
752  const qreal l = length();
753 
754  const qreal dx = qCos(angleR) * l;
755  const qreal dy = -qSin(angleR) * l;
756 
757  pt2.rx() = pt1.x() + dx;
758  pt2.ry() = pt1.y() + dy;
759 }
760 
775 {
776  const qreal angleR = q_deg2rad(angle);
777  return QLineF(0, 0, qCos(angleR) * length, -qSin(angleR) * length);
778 }
779 
787 {
788  qreal x = pt2.x() - pt1.x();
789  qreal y = pt2.y() - pt1.y();
790 
791  qreal len = qSqrt(x*x + y*y);
792  QLineF f(p1(), QPointF(pt1.x() + x/len, pt1.y() + y/len));
793 
794 #ifndef QT_NO_DEBUG
795  if (qAbs(f.length() - 1) >= 0.001)
796  qWarning("QLine::unitVector: New line does not have unit length");
797 #endif
798 
799  return f;
800 }
801 
813 QLineF::IntersectType QLineF::intersect(const QLineF &l, QPointF *intersectionPoint) const
814 {
815  // ipmlementation is based on Graphics Gems III's "Faster Line Segment Intersection"
816  const QPointF a = pt2 - pt1;
817  const QPointF b = l.pt1 - l.pt2;
818  const QPointF c = pt1 - l.pt1;
819 
820  const qreal denominator = a.y() * b.x() - a.x() * b.y();
821  if (denominator == 0 || !qt_is_finite(denominator))
822  return NoIntersection;
823 
824  const qreal reciprocal = 1 / denominator;
825  const qreal na = (b.y() * c.x() - b.x() * c.y()) * reciprocal;
826  if (intersectionPoint)
827  *intersectionPoint = pt1 + a * na;
828 
829  if (na < 0 || na > 1)
830  return UnboundedIntersection;
831 
832  const qreal nb = (a.x() * c.y() - a.y() * c.x()) * reciprocal;
833  if (nb < 0 || nb > 1)
834  return UnboundedIntersection;
835 
836  return BoundedIntersection;
837 }
838 
952 {
953  if (isNull() || l.isNull())
954  return 0;
955 
956  const qreal a1 = angle();
957  const qreal a2 = l.angle();
958 
959  const qreal delta = a2 - a1;
960  const qreal delta_normalized = delta < 0 ? delta + 360 : delta;
961 
962  if (qFuzzyCompare(delta, qreal(360)))
963  return 0;
964  else
965  return delta_normalized;
966 }
967 
994 {
995  if (isNull() || l.isNull())
996  return 0;
997  qreal cos_line = (dx()*l.dx() + dy()*l.dy()) / (length()*l.length());
998  qreal rad = 0;
999  // only accept cos_line in the range [-1,1], if it is outside, use 0 (we return 0 rather than PI for those cases)
1000  if (cos_line >= qreal(-1.0) && cos_line <= qreal(1.0)) rad = qAcos( cos_line );
1001  return q_rad2deg(rad);
1002 }
1003 
1004 
1005 #ifndef QT_NO_DEBUG_STREAM
1007 {
1008  d << "QLineF(" << p.p1() << ',' << p.p2() << ')';
1009  return d;
1010 }
1011 #endif
1012 
1013 #ifndef QT_NO_DATASTREAM
1014 
1027 {
1028  stream << line.p1() << line.p2();
1029  return stream;
1030 }
1031 
1045 {
1046  QPointF start, end;
1047  stream >> start;
1048  stream >> end;
1049  line = QLineF(start, end);
1050 
1051  return stream;
1052 }
1053 
1054 #endif // QT_NO_DATASTREAM
1055 
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
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
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
qreal length() const
Returns the length of the line.
Definition: qline.cpp:698
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
QPoint p1() const
Returns the line&#39;s start point.
Definition: qline.h:132
static QLineF fromPolar(qreal length, qreal angle)
Returns a QLineF with the given length and angle.
Definition: qline.cpp:774
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
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
IntersectType intersect(const QLineF &l, QPointF *intersectionPoint) const
Returns a value indicating whether or not this line intersects with the given line.
Definition: qline.cpp:813
long ASN1_INTEGER_get ASN1_INTEGER * a
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
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
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
qreal angleTo(const QLineF &l) const
Returns the angle (in positive degrees) from this line to the given line, taking the direction of the...
Definition: qline.cpp:951
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
static qreal q_rad2deg(qreal x)
Definition: qline.cpp:385
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qreal & rx()
Returns a reference to the x coordinate of this point.
Definition: qpoint.h:302
static FILE * stream
QPointF pt1
Definition: qline.h:272
qreal qSin(qreal v)
Definition: qmath.h:93
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
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
static qreal q_deg2rad(qreal x)
Definition: qline.cpp:380
IntersectType
Describes the intersection between two lines.
Definition: qline.h:215
Q_CORE_EXPORT void qWarning(const char *,...)
qreal angle() const
Returns the angle of the line in degrees.
Definition: qline.cpp:719
qreal qAtan2(qreal x, qreal y)
Definition: qmath.h:189
qreal dx() const
Returns the horizontal component of the line&#39;s vector.
Definition: qline.h:324
qreal qAcos(qreal v)
Definition: qmath.h:141
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
QPointF pt2
Definition: qline.h:272
qreal & ry()
Returns a reference to the y coordinate of this point.
Definition: qpoint.h:307
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QLineF()
Constructs a null line.
Definition: qline.h:280
static const KeyPair *const end
qreal qCos(qreal v)
Definition: qmath.h:109
QDataStream & operator<<(QDataStream &out, const QUrl &url)
Writes url url to the stream out and returns a reference to the stream.
Definition: qurl.cpp:6757
qreal qSqrt(qreal v)
Definition: qmath.h:205
static bool qt_is_finite(double d)
Definition: qnumeric_p.h:197
void setAngle(qreal angle)
Sets the angle of the line to the given angle (in degrees).
Definition: qline.cpp:749
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