GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


vector class by taby
#ifndef vector_3_h
#define vector_3_h

#include <cmath>

class vector_3
{
public:
    inline vector_3(const float src_x, const float src_y, const float src_z) : x(src_x), y(src_y), z(src_z) {};
    inline vector_3(void) : x(0.0f), y(0.0), z(0.0) {};

    void set(const float src_x, const float src_y, const float src_z)
    {
        x = src_x;
        y = src_y;
        z = src_z;
    }

    void normalize(void)
    {
        float len = sqrtf(x*x + y*y + z*z);

        if(len != 1.0)
        {
            x /= len;
            y /= len;
            z /= len;
        }
    }

    float dot(const vector_3 &rhs)
    {
        return x*rhs.x + y*rhs.y + z*rhs.z;
    }

    vector_3 cross(const vector_3 &rhs)
    {
        vector_3 temp;

        temp.x = y*rhs.z - z*rhs.y;
        temp.y = z*rhs.x - x*rhs.z;
        temp.z = x*rhs.y - y*rhs.x;

        return temp;
    }

    float x, y, z;
};

#endif