Skip to content

Commit

Permalink
feat(sourcepp): add more vector methods
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Aug 3, 2024
1 parent 4a154b0 commit 5ccae77
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/sourcepp/math/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,39 @@ struct Vec {
return out;
}

[[nodiscard]] constexpr float magf() const {
float out = 0.0;
for (uint8_t i = 0; i < S; i++) {
out += std::pow((*this)[i], 2);
}
return std::sqrt(out);
}

[[nodiscard]] constexpr double mag() const {
double out = 0.0;
for (uint8_t i = 0; i < S; i++) {
out += std::pow((*this)[i], 2);
}
return std::sqrt(out);
}

[[nodiscard]] constexpr P sum() const {
P out{};
for (uint8_t i = 0; i < S; i++) {
out += (*this)[i];
}
return out;
}

template<Arithmetic PO>
[[nodiscard]] constexpr P dot(const Vec<S, PO>& other) const {
Vec out;
for (uint8_t i = 0; i < S; i++) {
out[i] = (*this)[i] * static_cast<P>(other[i]);
}
return out.sum();
}

[[nodiscard]] constexpr Vec abs() const {
auto out = *this;
for (uint8_t i = 0; i < S; i++) {
Expand Down

0 comments on commit 5ccae77

Please sign in to comment.