summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qline.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qline.cpp')
-rw-r--r--src/corelib/tools/qline.cpp107
1 files changed, 42 insertions, 65 deletions
diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp
index d284883dcc..9216b8875b 100644
--- a/src/corelib/tools/qline.cpp
+++ b/src/corelib/tools/qline.cpp
@@ -1,45 +1,11 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qline.h"
+
#include "qdebug.h"
#include "qdatastream.h"
+#include "qmath.h"
#include <private/qnumeric_p.h>
QT_BEGIN_NAMESPACE
@@ -98,7 +64,7 @@ QT_BEGIN_NAMESPACE
/*!
\fn bool QLine::isNull() const
- Returns \c true if the line is not set up with valid start and end point;
+ Returns \c true if the line does not have distinct start and end points;
otherwise returns \c false.
*/
@@ -261,6 +227,15 @@ QT_BEGIN_NAMESPACE
\sa setP1(), setP2(), p1(), p2()
*/
+/*!
+ \fn QLine::toLineF() const
+ \since 6.4
+
+ Returns this line as a line with floating point accuracy.
+
+ \sa QLineF::toLine()
+*/
+
#ifndef QT_NO_DEBUG_STREAM
@@ -320,7 +295,7 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
A QLineF describes a finite length line (or line segment) on a
two-dimensional surface. QLineF defines the start and end points
of the line using floating point accuracy for coordinates. Use
- the toLine() function to retrieve an integer based copy of this
+ the toLine() function to retrieve an integer-based copy of this
line.
\table
@@ -364,7 +339,7 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
/*!
\typealias QLineF::IntersectType
- \obsolete Use QLineF::IntersectionType instead.
+ \deprecated Use QLineF::IntersectionType instead.
*/
/*!
@@ -421,14 +396,20 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
Construct a QLineF object from the given integer-based \a line.
- \sa toLine()
+ \sa toLine(), QLine::toLineF()
*/
/*!
\fn bool QLineF::isNull() const
- Returns \c true if the line is not set up with valid start and end point;
- otherwise returns \c false.
+ Returns \c true if the line does not have distinct start and end points;
+ otherwise returns \c false. The start and end points are considered distinct
+ if qFuzzyCompare() can distinguish them in at least one coordinate.
+
+ \note Due to the use of fuzzy comparison, isNull() may return \c true for
+ lines whose length() is not zero.
+
+ \sa qFuzzyCompare(), length()
*/
/*!
@@ -450,12 +431,12 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
/*!
\fn QLine QLineF::toLine() const
- Returns an integer based copy of this line.
+ Returns an integer-based copy of this line.
Note that the returned line's start and end points are rounded to
the nearest integer.
- \sa QLineF()
+ \sa QLineF(), QLine::toLineF()
*/
/*!
\fn qreal QLineF::x1() const
@@ -508,13 +489,11 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
/*!
\fn void QLineF::setLength(qreal length)
- Sets the length of the line to the given \a length. QLineF will
- move the end point - p2() - of the line to give the line its new length.
-
- If the line is a null line, the length will remain zero regardless
- of the length specified.
+ Sets the length of the line to the given finite \a length. QLineF will move
+ the end point - p2() - of the line to give the line its new length, unless
+ length() was previously zero, in which case no scaling is attempted.
- \sa length(), isNull()
+ \sa length(), unitVector()
*/
/*!
@@ -549,9 +528,8 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
/*!
\fn qreal QLineF::pointAt(qreal t) const
- Returns the point at the parameterized position specified by \a
- t. The function returns the line's start point if t = 0, and its end
- point if t = 1.
+ Returns the point at the position specified by finite parameter \a t. The
+ function returns the line's start point if t = 0, and its end point if t = 1.
\sa dx(), dy()
*/
@@ -559,13 +537,11 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
/*!
Returns the length of the line.
- \sa setLength()
+ \sa setLength(), isNull()
*/
qreal QLineF::length() const
{
- qreal x = pt2.x() - pt1.x();
- qreal y = pt2.y() - pt1.y();
- return qSqrt(x*x + y*y);
+ return qHypot(dx(), dy());
}
/*!
@@ -636,17 +612,18 @@ QLineF QLineF::fromPolar(qreal length, qreal angle)
/*!
Returns the unit vector for this line, i.e a line starting at the
- same point as \e this line with a length of 1.0.
+ same point as \e this line with a length of 1.0, provided the line
+ is non-null.
- \sa normalVector()
+ \sa normalVector(), setLength()
*/
QLineF QLineF::unitVector() const
{
- qreal x = pt2.x() - pt1.x();
- qreal y = pt2.y() - pt1.y();
+ const qreal x = dx();
+ const qreal y = dy();
- qreal len = qSqrt(x*x + y*y);
- QLineF f(p1(), QPointF(pt1.x() + x/len, pt1.y() + y/len));
+ const qreal len = qHypot(x, y);
+ QLineF f(p1(), QPointF(pt1.x() + x / len, pt1.y() + y / len));
#ifndef QT_NO_DEBUG
if (qAbs(f.length() - 1) >= 0.001)