Blob Blame Raw

#ifndef GEOMETRY_H
#define GEOMETRY_H


#include <cmath>


typedef double Real;


class Vector2 {
public:
    union {
        struct { Real x, y; };
        struct { Real c[2]; };
    };
    
    explicit Vector2(Real x = 0, Real y = 0):
        x(x), y(y) { }
        
    Real len_sqr() const { return x*x + y*y; }
    Real len() const { return sqrt(len_sqr()); }
};


class Vector3 {
public:
    union {
        struct { Real x, y, z; };
        struct { Real r, g, b; };
        struct { Real c[3]; };
    };
    
    explicit Vector3(Real x = 0, Real y = 0, Real z = 0):
        x(x), y(y), z(z) { }
    
    Vector3 operator+(const Vector3 &v) const
        { return Vector3(x+v.x, y+v.y, z+v.z); }
    Vector3 operator-(const Vector3 &v) const
        { return Vector3(x-v.x, y-v.y, z-v.z); }

    Real operator*(const Vector3 &v) const
        { return x*v.x + y*v.y + z*v.z; }
    Vector3 operator*(Real k) const
        { return Vector3(x*k, y*k, z*k); }

    Vector3 cross(const Vector3 &v) const
        { return Vector3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); }

    Real len_sqr() const { return x*x + y*y + z*z; }
    Real len() const { return sqrt(len_sqr()); }
};


class Vector4 {
public:
    union {
        struct { Real x, y, z, w; };
        struct { Real r, g, b, a; };
        struct { Real c[4]; };
    };
    
    explicit Vector4(Real x = 0, Real y = 0, Real z = 0, Real w = 0):
        x(x), y(y), z(z), w(w) { }
};


#endif