diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2020-09-08 16:50:13 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-01-06 14:53:24 +0000 |
commit | 28ba7ac1780b41924de267861826b01233618570 (patch) | |
tree | b951e83269da70cd114990c486dcbe007bc5a786 /src | |
parent | 636fcc4bb2edbbd0b75fdecfc9c6b40b0c872803 (diff) |
Change QLineF::setLength() to work whenever length() is non-zero
Previously it only worked when isNull() was false, which is true for
very short lines, even though length() may be non-zero.
[ChangeLog][QtCore][QLineF] QLineF::setLength() will now set the
length if the line's length() is non-zero. Previously, it was
documented to only set the length if isNull() was false; this is a
fuzzy check, so isNull() could be true for a line with non-zero
length().
Fixes: QTBUG-89569
Change-Id: I803e622ad09c85815dde25df8dd3ba6dfcba0714
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
(cherry picked from commit 6974737695eae5a41bc8a3f344a4f1f199006f21)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qline.cpp | 11 | ||||
-rw-r--r-- | src/corelib/tools/qline.h | 11 |
2 files changed, 11 insertions, 11 deletions
diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp index ebcbba0195..1eb645501c 100644 --- a/src/corelib/tools/qline.cpp +++ b/src/corelib/tools/qline.cpp @@ -517,12 +517,13 @@ 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. + 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. For lines with very short lengths + (represented by denormal floating-point values), results may be + imprecise. - A null line will not be rescaled. For non-null lines with very short lengths - (represented by denormal floating-point values), results may be imprecise. - - \sa length(), isNull(), unitVector() + \sa length(), unitVector() */ /*! diff --git a/src/corelib/tools/qline.h b/src/corelib/tools/qline.h index 4948559509..9b544f2e7e 100644 --- a/src/corelib/tools/qline.h +++ b/src/corelib/tools/qline.h @@ -372,12 +372,11 @@ constexpr inline QPointF QLineF::center() const inline void QLineF::setLength(qreal len) { - if (isNull()) - return; - Q_ASSERT(length() > 0); - const QLineF v = unitVector(); - len /= v.length(); // In case it's not quite exactly 1. - pt2 = QPointF(pt1.x() + len * v.dx(), pt1.y() + len * v.dy()); + const qreal oldLength = length(); + // Scale len by dx() / length() and dy() / length(), two O(1) quantities, + // rather than scaling dx() and dy() by len / length(), which might overflow. + if (oldLength > 0) + pt2 = QPointF(pt1.x() + len * (dx() / oldLength), pt1.y() + len * (dy() / oldLength)); } constexpr inline QPointF QLineF::pointAt(qreal t) const |