#include #include #include #include #include #include template struct basic_vec : public std::array { static constexpr std::size_t dimensions = d; basic_vec(); basic_vec(const std::initializer_list l); template basic_vec(const basic_vec& other); T length() const; }; template basic_vec::basic_vec() : std::array() { this->fill(static_cast(0)); } template basic_vec::basic_vec(const std::initializer_list l) { basic_vec(); // why can't this sh*t be a constexpr with static_assert??? assert(l.size() <= d); std::copy(l.begin(), l.end(), this->begin()); } template template basic_vec::basic_vec(const basic_vec& other) { basic_vec(); static_assert(d >= n); for (std::size_t i = 0; i < n; i++) { this->at(i) = other.at(i); } } template T basic_vec::length() const { T res = static_cast(0); for (const T& val : *this) { res += val * val; } return res; } template basic_vec operator+(const basic_vec& rhs, const basic_vec& lhs) { basic_vec out; auto rit = rhs.cbegin(), lit = lhs.cbegin(); std::generate(out.begin(), out.end(), [rit, lit] () mutable { return *(rit++) + *(lit++); }); return out; } template basic_vec operator*(const T& rhs, const basic_vec& lhs) { basic_vec out; auto lit = lhs.cbegin(); std::generate(out.begin(), out.end(), [rhs, lit] () mutable { return rhs * *(lit++); }); return out; } template basic_vec operator-(const basic_vec& rhs, const basic_vec& lhs) { return rhs + static_cast(-1) * lhs; } template T operator*(const basic_vec& rhs, const basic_vec& lhs) { T res = static_cast(0); for (std::size_t i = 0; i < d; i++) { res += rhs[i] * lhs[i]; } return res; } template std::ostream& operator<<(std::ostream& os, const basic_vec& v) { os << "<"; for (std::size_t i = 0; i < d -1; i++) { os << v[i] << ", "; } os << v[d-1] << ">"; return os; } template class vec: public basic_vec { public: vec(std::initializer_list l) : basic_vec(l) {} template vec(const basic_vec& other) : basic_vec(other) {} }; template class vec3 : public basic_vec { public: vec3(std::initializer_list l) : basic_vec(l) {} template vec3(const basic_vec& other) : basic_vec(other) {} static vec3 cross(const vec3& rhs, const vec3& lhs); }; template vec3 vec3::cross(const vec3& rhs, const vec3& lhs) { vec3 res; res[0] = (rhs[1] * lhs[2]) - (rhs[2] * lhs[1]); res[1] = (rhs[2] * lhs[0]) - (rhs[0] * lhs[2]); res[2] = (rhs[0] * lhs[1]) - (rhs[1] * lhs[0]); return res; } template class vec2: public basic_vec { public: vec2(std::initializer_list l) : basic_vec(l) {} template vec2(const basic_vec& other) : basic_vec(other) {} T angle(); static vec3 cross(const vec2& rhs, const vec2& lhs); }; template vec3 vec2::cross(const vec2& rhs, const vec2& lhs) { return vec3::cross(vec3(rhs), vec3(lhs)); } int main(int argc, char *argv[]) { vec3 v{1, 2, 3}; vec3 u{3, 4, 5}; std::cout << v << std::endl; std::cout << v.length() << std::endl; std::cout << v + u << std::endl; std::cout << v - u << std::endl; std::cout << 2.0 * u << std::endl; std::cout << v * u << std::endl; return 0; }