Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdint>
- #include <vector>
- #include <cmath>
- #include <numeric>
- using namespace std;
- template<typename T, size_t dim>
- class Vector{
- T coords[dim];
- public:
- Vector() {
- for(size_t i = 0; i < dim; i++){
- coords[i] = T(0);
- }
- }
- T operator[](size_t index) const {
- return coords[index];
- }
- T& operator[](size_t index) {
- return coords[index];
- }
- T& operator=(const T& other){
- if(this != other){
- for(size_t i = 0; i < dim; i++){
- coords[i] = other[i];
- }
- }
- return *this;
- }
- double length(){
- return sqrt(accumulate(coords, coords + dim, 0));
- }
- friend ostream& operator<<(ostream& os, const Vector<T, dim> &other){
- os << "(";
- for(size_t i = 0; i < dim; i++){
- os << other[i];
- if(i != dim - 1){
- os << ",";
- }
- }
- os << ")";
- return os;
- }
- };
- #define vDIM 2
- template<typename T>
- class V2 : public Vector<T, vDIM> {
- public:
- V2(T x, T y) {
- Vector<T, vDIM>::operator[](0) = x;
- Vector<T, vDIM>::operator[](1) = y;
- }
- T x(){ return Vector<T, vDIM>::operator[](0);}
- void x(T ix){ Vector<T, vDIM>::operator[](0) = ix;}
- T y(){ return Vector<T, vDIM>::operator[](1);}
- void y(T iy){ Vector<T, vDIM>::operator[](1) = iy;}
- };
- using V2d = V2<double> ;
- #define vDIM 3
- template<typename T>
- class V3 : public Vector<T, vDIM> {
- public:
- V3(T x, T y, T z) {
- Vector<T, vDIM>::operator[](0) = x;
- Vector<T, vDIM>::operator[](1) = y;
- Vector<T, vDIM>::operator[](2) = z;
- }
- T x(){ return Vector<T, vDIM>::operator[](0);}
- void x(T ix){ Vector<T, vDIM>::operator[](0) = ix;}
- T y(){ return Vector<T, vDIM>::operator[](1);}
- void y(T iy){ Vector<T, vDIM>::operator[](1) = iy;}
- T z(){ return Vector<T, vDIM>::operator[](2);}
- void z(T iz){ Vector<T, vDIM>::operator[](2) = iz;}
- };
- using V3d = V3<double>;
- #define vDIM 4
- template<typename T>
- class V4 : public Vector<T, vDIM> {
- public:
- V4(T x, T y, T z, T w) {
- Vector<T, vDIM>::operator[](0) = x;
- Vector<T, vDIM>::operator[](1) = y;
- Vector<T, vDIM>::operator[](2) = z;
- Vector<T, vDIM>::operator[](3) = w;
- }
- T x(){ return Vector<T, vDIM>::operator[](0);}
- void x(T ix){ Vector<T, vDIM>::operator[](0) = ix;}
- T y(){ return Vector<T, vDIM>::operator[](1);}
- void y(T iy){ Vector<T, vDIM>::operator[](1) = iy;}
- T z(){ return Vector<T, vDIM>::operator[](2);}
- void z(T iz){ Vector<T, vDIM>::operator[](2) = iz;}
- T w(){ return Vector<T, vDIM>::operator[](3);}
- void w(T iw){ Vector<T, vDIM>::operator[](3) = iw;}
- };
- using V4d = V4<double>;
- int main()
- {
- cout << "Test assignment" << endl;
- V2<float> v(4.0, 5.0);
- cout << v << endl;
- v.x(7.0);
- cout << v << endl;
- v[0] = 8.0;
- cout << v << endl;
- cout << "Test length:" << endl;
- V2d vet{1.0, 2.0};
- cout << vet << endl;
- cout << vet.length() << endl;
- V4d v4{0, 1, 2, 3};
- cout << v4 << endl;
- cout << v4.length() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement