27#include <initializer_list>
39template <
typename Type,
size_t Dimension>
class Vector {
41 std::array<Type, Dimension> _data;
58 Vector(std::initializer_list<Type> values) {
60 for (
const auto &value : values) {
81 const Type &
operator[](
size_t index)
const {
return _data[index]; }
92 for (
size_t i = 0; i < Dimension; ++i) {
93 result[i] = _data[i] + other[i];
106 for (
size_t i = 0; i < Dimension; ++i) {
107 result[i] = _data[i] - other[i];
120 for (
size_t i = 0; i < Dimension; ++i) {
121 result[i] = _data[i] * scalar;
133 Type result = Type{};
134 for (
size_t i = 0; i < Dimension; ++i) {
135 result += _data[i] * other[i];
157 return (mag != Type{}) ? (*
this * (Type{1} / mag)) : *
this;
165 constexpr size_t size()
const {
return Dimension; }
A generic N-dimensional vector class template.
Type magnitude() const
Calculate the magnitude (length) of the vector.
Vector operator-(const Vector &other) const
Vector subtraction operator.
const Type & operator[](size_t index) const
Const element access operator.
Type dot(const Vector &other) const
Compute dot product with another vector.
Vector(std::initializer_list< Type > values)
Constructor from initializer list.
Vector operator*(Type scalar) const
Scalar multiplication operator.
Vector normalize() const
Create a normalized (unit) vector.
Vector()
Default constructor - initializes all components to zero.
Type & operator[](size_t index)
Non-const element access operator.
constexpr size_t size() const
Get the number of dimensions/components.
Vector operator+(const Vector &other) const
Vector addition operator.