vector: add difference(2) methods

This commit is contained in:
Danny Robson 2015-01-28 15:00:20 +11:00
parent 6abfd252d9
commit cee9951f82
2 changed files with 23 additions and 0 deletions

View File

@ -241,6 +241,26 @@ util::vector<S,T>::magnitude2 (void) const {
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
T
util::vector<S,T>::difference (const util::vector<S,T> &rhs) const {
// TODO: change the signature to ensure it does not truncate
return static_cast<T> (std::sqrt (difference2 (rhs)));
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
T
util::vector<S,T>::difference2 (const util::vector<S,T> &rhs) const {
T sum {0};
for (size_t i = 0; i < S; ++i)
sum += pow2 (this->data[i] - rhs.data[i]);
return sum;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::vector<S,T>&
util::vector<S,T>::normalise (void) {

View File

@ -67,6 +67,9 @@ namespace util {
T magnitude (void) const;
T magnitude2 (void) const;
T difference (const vector<S,T>&) const;
T difference2 (const vector<S,T>&) const;
T dot (const vector<S,T>&) const;
vector<S,T>& normalise (void);