G43riko
5/31/2018 - 2:20 PM

Vector3f

#include <iostream>

using namespace std;
int created = 0;
int deleted = 0;
class Vector3f{
    private:
        float x, y, z;
    public:
        inline Vector3f(void): x(0), y(0), z(0) {
            created++;
        };
        inline Vector3f(const float x, const float y, const float z) : x(x), y(y), z(z) {
            created++;
        };
        inline ~Vector3f() {
            deleted++;
        }
        
        inline Vector3f& operator *= (const float v) { 
            x *= v;
            y *= v;
            z *= v;
            return *this;
        }
        inline Vector3f& operator *= (const Vector3f& v) { 
            x *= v.x;
            y *= v.y;
            z *= v.z;
            return *this;
        }
        inline Vector3f operator * (const Vector3f &v) const { 
            return Vector3f(x * v.x, y * v.y, z * v.z);
        }
        inline Vector3f operator * (const float v) const { 
            return Vector3f(x * v, y * v, z * v);
        }
        inline bool operator == (const Vector3f &v) const {
            return v.x == x && v.y == y && v.z == z;
        }
        inline bool operator != (const Vector3f &v) const {
            return v.x != x || v.y != y || v.z != z;
        }
        inline Vector3f& operator = (const Vector3f& v) {
            x = v.x; 
            y = v.y;
            z = v.z;
            return *this;
        }
        inline Vector3f& show(void) const{
            cout << "[ " << x << ", " << y << ", " << z << " ]\n";
            return *this;
        }
        friend std::ostream& operator << (std::ostream& os, const Vector3f& v){
            os << "[ " << v.x << ", " << v.y << ", " << v.z << " ]";
            return os;
        }
        inline Vector3f& set(float i_x, float i_y, float i_z){
            x = i_x;
            y = i_y;
            z = i_z;
            return *this;
        }
        inline float dot(const Vector3f& vec) const{
            return x * vec.x + y * vec.y + z * vec.z;
            
        }
        inline float lengthSquar(void) const {
            return x * x + y * y + z * z + w * w;
        }
};

inline void testVectors() {
    Vector3f a;
    a *= Vector3f(99, 99, 99);
    Vector3f b = {1, 2, 3};
    Vector3f c = Vector3f(4, 5, 6) * 2.0f;
    
    cout << a << "\n";
    b.show();
    c.show();
}

int main() {
    cout << "Hello World\n";
    testVectors();
    
    cout << "created: " << created << ", deleted: " << deleted << "\n";
    return 0;
}