diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2017-02-24 14:40:51 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-01-27 15:00:57 +0100 |
commit | b08368d99fa41b3f84cf30468c2dc9f5c2c43423 (patch) | |
tree | 9b5f6400423c7f23a547c0a21675a989683c4b7e /src/gui/painting | |
parent | ad16f79e5fac13f8a9dab8604de6adb6c46fe7bd (diff) |
Add qHypot() to qmath.h, exposing and extending std::hypot()
We have plenty of places where we add some squares and take a square
root; this may be done more accurately and faster by hypot().
Introduce QHypotHelper to handle hypot with more than 3 parameters,
and with 3 when the C++17 version is missing (which it never should
be). Include an overload taking arbitrarily many valus and ensure that
we can use qHypot() with qfloat16. Illustrate with some example uses,
add some tests.
[ChangeLog][QtCore][QMath] Header <QMath> now provides qHypot(), an
implementation of std::hypot() taking arbitrarily many numeric values,
including support for qfloat16, while avoiding the overflow and
underflow problems that arise when naively taking the square root of a
sum of squares.
Change-Id: Ia4e3913fe83fc27d17d8e7f1a52f03ad445c1fed
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/gui/painting')
-rw-r--r-- | src/gui/painting/qtriangulatingstroker_p.h | 18 |
1 files changed, 5 insertions, 13 deletions
diff --git a/src/gui/painting/qtriangulatingstroker_p.h b/src/gui/painting/qtriangulatingstroker_p.h index 2b0f08972b..9537311ec5 100644 --- a/src/gui/painting/qtriangulatingstroker_p.h +++ b/src/gui/painting/qtriangulatingstroker_p.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -51,6 +51,7 @@ // We mean it. // +#include <QtCore/qmath.h> #include <QtGui/private/qtguiglobal_p.h> #include <private/qdatabuffer_p.h> #include <qvarlengtharray.h> @@ -134,18 +135,9 @@ private: inline void QTriangulatingStroker::normalVector(float x1, float y1, float x2, float y2, float *nx, float *ny) { - float dx = x2 - x1; - float dy = y2 - y1; - Q_ASSERT(dx != 0 || dy != 0); - - float pw; - - if (dx == 0) - pw = m_width / std::abs(dy); - else if (dy == 0) - pw = m_width / std::abs(dx); - else - pw = m_width / std::sqrt(dx*dx + dy*dy); + const float dx = x2 - x1; + const float dy = y2 - y1; + const float pw = m_width / qHypot(dx, dy); *nx = -dy * pw; *ny = dx * pw; |