#ifndef VECTOR_H #define VECTOR_H #include "utils/StringBuffer.h" template class Vector final { float values[N]; public: Vector() { for(int i = 0; i < N; i++) { values[i] = 0.0f; } } Vector(float, float) = delete; Vector(float, float, float) = delete; Vector& set(float, float) = delete; Vector& set(float, float, float) = delete; Vector& setAngles(float, float) = delete; Vector cross(const Vector&) const = delete; Vector& operator+=(const Vector& other) { for(int i = 0; i < N; i++) { values[i] += other.values[i]; } return *this; } Vector operator+(const Vector& other) const { Vector v = *this; v += other; return v; } Vector& operator-=(const Vector& other) { for(int i = 0; i < N; i++) { values[i] -= other.values[i]; } return *this; } Vector operator-() const { Vector v = *this; for(int i = 0; i < N; i++) { v.values[i] = -v.values[i]; } return v; } Vector operator-(const Vector& other) const { Vector v = *this; v -= other; return v; } Vector& operator*=(float factor) { for(int i = 0; i < N; i++) { values[i] *= factor; } return *this; } Vector operator*(float factor) const { Vector v = *this; v *= factor; return v; } float dot(const Vector& v) const { float length = 0.0f; for(int i = 0; i < N; i++) { length += values[i] * v.values[i]; } return length; } float squareLength() const { return dot(*this); } float length() const { return sqrtf(squareLength()); } Vector& normalize() { *this *= 1.0f / length(); return *this; } const float& operator[](int index) const { return values[index]; } template void toString(StringBuffer& s) const { s.append("["); for(int i = 0; i < N - 1; i++) { s.append(values[i]); s.append(", "); } if(N > 0) { s.append(values[N - 1]); } s.append("]"); } }; template Vector operator*(float factor, const Vector& v) { return v * factor; } template<> Vector<3>::Vector(float x, float y, float z); template<> Vector<2>::Vector(float x, float y); template<> Vector<3>& Vector<3>::set(float x, float y, float z); template<> Vector<2>& Vector<2>::set(float x, float y); template<> Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle); template<> Vector<3> Vector<3>::cross(const Vector<3>& other) const; typedef Vector<3> Vector3; typedef Vector<2> Vector2; #endif