Skip to content

Commit 3d9fceb

Browse files
committed
fixed and unified getLenght() and getLengthSquared()
1 parent dbfded3 commit 3d9fceb

File tree

6 files changed

+9
-13
lines changed

6 files changed

+9
-13
lines changed

src/modm/math/geometry/vector.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <modm/math/matrix.hpp>
2121
#include <modm/math/utils/arithmetic_traits.hpp>
2222

23+
#include "geometric_traits.hpp"
24+
2325
namespace modm
2426
{
2527
// forward declaration

src/modm/math/geometry/vector1.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace modm
7373
Vector& operator /= (const T &rhs);
7474

7575
T getLength() const;
76-
T getLengthSquared() const;
76+
WideType getLengthSquared() const;
7777

7878
Matrix<T, 1, 1>&
7979
asMatrix();

src/modm/math/geometry/vector1_impl.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ modm::Vector<T, 1>::getLength() const
142142
}
143143

144144
template<typename T>
145-
T
145+
modm::Vector<T, 1>::WideType
146146
modm::Vector<T, 1>::getLengthSquared() const
147147
{
148-
return x * x;
148+
return std::pow(x, 2);
149149
}
150150

151151
// ----------------------------------------------------------------------------

src/modm/math/geometry/vector2_impl.hpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,14 @@ template<typename T>
2323
T
2424
modm::Vector<T, 2>::getLength() const
2525
{
26-
float tx = this->x;
27-
float ty = this->y;
28-
29-
return GeometricTraits<T>::round(std::sqrt(tx*tx + ty*ty));
26+
return GeometricTraits<T>::round(std::sqrt(getLengthSquared()));
3027
}
3128

3229
template<typename T>
3330
typename modm::Vector<T, 2>::WideType
3431
modm::Vector<T, 2>::getLengthSquared() const
3532
{
36-
WideType tx = static_cast<WideType>(this->x);
37-
WideType ty = static_cast<WideType>(this->y);
38-
39-
return tx * tx + ty * ty;
33+
return std::pow(x, 2) + std::pow(y, 2);
4034
}
4135

4236
template<typename T>

src/modm/math/geometry/vector3_impl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ template<typename T>
169169
float
170170
modm::Vector<T, 3>::getLengthSquared() const
171171
{
172-
return x*x+y*y+z*z;
172+
return std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2);
173173
}
174174

175175
template<typename T>

src/modm/math/geometry/vector4_impl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ template<typename T>
166166
float
167167
modm::Vector<T, 4>::getLengthSquared() const
168168
{
169-
return x*x + y*y + z*z + w*w;
169+
return std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2) + std::pow(w, 2);
170170
}
171171

172172
template<typename T>

0 commit comments

Comments
 (0)