vector: add hypot function
calculates the root of the sum of the squares. much like std::hypot, but of arbitrary dimension.
This commit is contained in:
parent
d9713fe8b7
commit
4544a594c3
@ -7,6 +7,7 @@ using util::vector;
|
||||
using util::vector2f;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
test_polar (util::TAP::logger &tap)
|
||||
{
|
||||
@ -61,6 +62,7 @@ test_polar (util::TAP::logger &tap)
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
test_euler (util::TAP::logger &tap)
|
||||
{
|
||||
@ -100,6 +102,7 @@ test_euler (util::TAP::logger &tap)
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
int
|
||||
main ()
|
||||
{
|
||||
@ -111,5 +114,11 @@ main ()
|
||||
tap.expect (!is_normalised (util::vector3f::ZERO), "zero isn't normalised");
|
||||
tap.expect (!is_normalised (util::vector3f::ONES), "ones isn't normalised");
|
||||
|
||||
tap.expect_eq (
|
||||
util::hypot (util::vector3f{0,1,2}, util::vector3f{3,2,4}),
|
||||
std::sqrt (14.f),
|
||||
"vector3f hypot"
|
||||
);
|
||||
|
||||
return tap.status ();
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_VECTOR_HPP
|
||||
@ -50,6 +50,12 @@ namespace util {
|
||||
template <typename T> vector<2,T> to_euler (vector<3,T>);
|
||||
template <typename T> vector<3,T> from_euler (vector<2,T>);
|
||||
|
||||
// power functions
|
||||
template <size_t S, typename T>
|
||||
constexpr
|
||||
T
|
||||
hypot (util::vector<S,T>, util::vector<S,T>);
|
||||
|
||||
// output and serialisation operators
|
||||
template <size_t S, typename T>
|
||||
const json::tree::node&
|
||||
|
32
vector.ipp
32
vector.ipp
@ -11,7 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#if defined(__UTIL_VECTOR_IPP)
|
||||
@ -20,14 +20,26 @@
|
||||
#define __UTIL_VECTOR_IPP
|
||||
#endif
|
||||
|
||||
#include "./maths.hpp"
|
||||
|
||||
namespace util {
|
||||
template <size_t S, typename T>
|
||||
template <size_t D>
|
||||
vector<D,T>
|
||||
vector<S,T>::homog (void) const
|
||||
{
|
||||
static_assert (D > S, "reducing size loses data");
|
||||
return (*this).template redim<D> (0.f);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
template <size_t D>
|
||||
util::vector<D,T>
|
||||
util::vector<S,T>::homog (void) const
|
||||
{
|
||||
static_assert (D > S, "reducing size loses data");
|
||||
return (*this).template redim<D> (0.f);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
constexpr
|
||||
T
|
||||
util::hypot (util::vector<S,T> a, util::vector<S,T> b)
|
||||
{
|
||||
auto c = a - b;
|
||||
return std::sqrt (sum (c * c));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user