summaryrefslogtreecommitdiff
path: root/basegfx
diff options
context:
space:
mode:
authorRMZeroFour <ritobroto04@gmail.com>2024-03-29 20:19:58 +0530
committerHossein <hossein@libreoffice.org>2024-04-05 13:59:33 +0200
commit45d951492a34d8d4134a518662377fa4c1e08395 (patch)
treee56b450260cfacf1adbaaacb6ccf117efa52b07f /basegfx
parentaa11db227da2830accac7c523c534b2b86a86bb2 (diff)
tdf#147906 Use std::hypot for Pythagorean distance
As part of the efforts in tdf#147906 to replace expressions like sqrt(a^2 + b^2) with std::hypot(a, b), to eliminate overflow errors, this commit performs the changes in certain files. This also changes the B3DVector::normalize and B2DVector::normalize methods to have similar behaviour. Change-Id: I142585cfa594849f06cd06517ad9d40430df2ade Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165555 Tested-by: Hossein <hossein@libreoffice.org> Reviewed-by: Hossein <hossein@libreoffice.org>
Diffstat (limited to 'basegfx')
-rw-r--r--basegfx/source/vector/b2dvector.cxx27
-rw-r--r--basegfx/source/vector/b2ivector.cxx4
-rw-r--r--basegfx/source/vector/b3dvector.cxx10
3 files changed, 20 insertions, 21 deletions
diff --git a/basegfx/source/vector/b2dvector.cxx b/basegfx/source/vector/b2dvector.cxx
index 85ec6ca81116..1f696237fecf 100644
--- a/basegfx/source/vector/b2dvector.cxx
+++ b/basegfx/source/vector/b2dvector.cxx
@@ -25,28 +25,23 @@ namespace basegfx
{
B2DVector& B2DVector::normalize()
{
- double fLen(scalar(*this));
+ double fLen(std::hypot(mnX, mnY));
- if(fTools::equalZero(fLen))
- {
- mnX = 0.0;
- mnY = 0.0;
- }
- else
+ if(!fTools::equalZero(fLen))
{
const double fOne(1.0);
if(!fTools::equal(fOne, fLen))
{
- fLen = sqrt(fLen);
-
- if(!fTools::equalZero(fLen))
- {
- mnX /= fLen;
- mnY /= fLen;
- }
+ mnX /= fLen;
+ mnY /= fLen;
}
}
+ else
+ {
+ mnX = 0.0;
+ mnY = 0.0;
+ }
return *this;
}
@@ -90,7 +85,7 @@ namespace basegfx
B2DVector& B2DVector::setLength(double fLen)
{
- double fLenNow(scalar(*this));
+ double fLenNow(std::hypot(mnX, mnY));
if(!fTools::equalZero(fLenNow))
{
@@ -98,7 +93,7 @@ namespace basegfx
if(!fTools::equal(fOne, fLenNow))
{
- fLen /= sqrt(fLenNow);
+ fLen /= fLenNow;
}
mnX *= fLen;
diff --git a/basegfx/source/vector/b2ivector.cxx b/basegfx/source/vector/b2ivector.cxx
index 5b45871a39cf..6d618facf48f 100644
--- a/basegfx/source/vector/b2ivector.cxx
+++ b/basegfx/source/vector/b2ivector.cxx
@@ -42,7 +42,7 @@ namespace basegfx
B2IVector& B2IVector::setLength(double fLen)
{
- double fLenNow(scalar(*this));
+ double fLenNow(std::hypot(mnX, mnY));
if(!::basegfx::fTools::equalZero(fLenNow))
{
@@ -50,7 +50,7 @@ namespace basegfx
if(!::basegfx::fTools::equal(fOne, fLenNow))
{
- fLen /= sqrt(fLenNow);
+ fLen /= fLenNow;
}
mnX = fround( mnX*fLen );
diff --git a/basegfx/source/vector/b3dvector.cxx b/basegfx/source/vector/b3dvector.cxx
index 89bc33c2bd9e..68e3fcf205c4 100644
--- a/basegfx/source/vector/b3dvector.cxx
+++ b/basegfx/source/vector/b3dvector.cxx
@@ -24,7 +24,7 @@ namespace basegfx
{
B3DVector& B3DVector::normalize()
{
- double fLen(scalar(*this));
+ double fLen(std::hypot(mnX, mnY, mnZ));
if(!::basegfx::fTools::equalZero(fLen))
{
@@ -32,13 +32,17 @@ namespace basegfx
if(!::basegfx::fTools::equal(fOne, fLen))
{
- fLen = sqrt(fLen);
-
mnX /= fLen;
mnY /= fLen;
mnZ /= fLen;
}
}
+ else
+ {
+ mnX = 0.0;
+ mnY = 0.0;
+ mnZ = 0.0;
+ }
return *this;
}