Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Vector2 .h
- class Vector2
- {
- public:
- //Data
- union
- {
- struct { float x, y; };
- float data[2];
- };
- Vector2(); //Default constructor
- Vector2(float a_x, float a_y);
- ~Vector2();
- float& operator [] (int index); //Subscript operator
- operator float* (); //Casts the vector to an array of floats
- operator const float* (); //Casts the vector to a const array of floats
- //Magnitude
- float Magnitude() const { return sqrt(x*x + y * y); } //Returns the magnitude of the vector (Pythagoras)
- float MagnitudeSqr() const { return (x*x + y * y); } //Returns the squared magnitude of the vector (Pythagoras)
- float Distance(const Vector2& other) const; //Returns the distance between two points
- void Normalize(); //Make vector unit length
- //Dot
- float Dot(const Vector2& other) const; //Returns the dot product
- float AngleBetween(const Vector2& other) const; //Normalizes both vectors and returns the dot product
- Vector2 GetPerpendicularRH() const { return { -y, x }; }
- Vector2 GetPerpendicularLH() const { return { y, -x }; }
- //Equality
- bool operator == (const Vector2& other); //Checks for exact equality
- bool operator != (const Vector2& other); //Checks for exact inequality
- bool Equal(const Vector2& other, float tolerance = 0.005f); //Checks for equality within the tolerance difference
- bool NotEqual(const Vector2& other, float tolerance = 0.005f); //Checks for inequality within the tolerance difference
- Vector2 Inverse(); //Flips the vector
- //Addition
- Vector2 operator + (const Vector2& other) const;
- Vector2 operator += (const Vector2& other);
- //Subtraction
- Vector2 operator - (const Vector2& other) const;
- Vector2 operator -= (const Vector2& other);
- //Multiplication
- Vector2 operator * (float scalar) const;
- Vector2 operator *= (float scalar);
- //float * Vector2
- friend Vector2 operator * (float scalar, const Vector2& v);
- friend Vector2 operator *= (float scalar, const Vector2& v);
- //Division
- Vector2 operator / (float scalar) const;
- Vector2 operator /= (float scalar);
- //Assignment
- Vector2 operator = (const Vector2& other);
- };
- //Vector2 .cpp
- //Constructors
- Vector2::Vector2() //Default
- {
- x = 0; y = 0;
- }
- Vector2::Vector2(float a_x, float a_y)
- {
- x = a_x;
- y = a_y;
- }
- Vector2::~Vector2()
- {
- }
- float & Vector2::operator[](int index) { return data[index]; }
- Vector2::operator float*() { return data; }
- Vector2::operator const float*() { return data; }
- float Vector2::Distance(const Vector2 & other) const
- {
- float diffX = x - other.x;
- float diffY = y - other.y;
- return sqrt(diffX*diffX + diffY * diffY); //Use Pythagoras theorum to find the distance between the two vectors
- }
- void Vector2::Normalize()
- {
- float mag = Magnitude();
- x /= mag;
- y /= mag;
- }
- float Vector2::Dot(const Vector2 & other) const
- {
- return x * other.x + y * other.y;
- }
- float Vector2::AngleBetween(const Vector2& other) const
- {
- //Normalize the vectors first
- Vector2 a; a.Normalize();
- Vector2 b; b.Normalize();
- return acos(a.Dot(b));
- }
- //Equality
- bool Vector2::operator==(const Vector2& other)
- {
- //Check if all elements are the same
- if (x == other.x && y == other.y) return true;
- return false;
- }
- bool Vector2::operator!=(const Vector2 & other)
- {
- //If all parts are the same, the vectors are equal
- if (x == other.x && y == other.y) return false;
- return true;
- }
- bool Vector2::Equal(const Vector2 & other, float tolerance)
- {
- //Checks if the absolute value of the difference between this element and the other element is more than the tolerance
- //If it is, then the vectors are not equal
- if (fabs(x - other.x) > tolerance) return false;
- if (fabs(y - other.y) > tolerance) return false;
- return true;
- }
- bool Vector2::NotEqual(const Vector2 & other, float tolerance)
- {
- //Checks if the absolute value of the difference between all the elements and all the other elements is less than the tolerance
- //If they are all less than the tolerance, the vectors are equal
- if (fabs(x - other.x) < tolerance && fabs(y - other.y) < tolerance) return false;
- return true;
- }
- //Inverse
- Vector2 Vector2::Inverse()
- {
- x *= -1; y *= -1;
- return *this;
- }
- //Addition
- Vector2 Vector2::operator+(const Vector2 & other) const
- {
- return { x + other.x, y + other.y };
- }
- Vector2 Vector2::operator+=(const Vector2 & other)
- {
- x += other.x; y += other.y;
- return *this;
- }
- //Subtraction
- Vector2 Vector2::operator-(const Vector2 & other) const
- {
- return { x - other.x, y - other.y };
- }
- Vector2 Vector2::operator-=(const Vector2 & other)
- {
- x -= other.x; y -= other.y;
- return *this;
- }
- //Multiplication
- Vector2 Vector2::operator*(float scalar) const
- {
- return { x * scalar, y * scalar };
- }
- Vector2 Vector2::operator*=(float scalar)
- {
- x *= scalar; y *= scalar;
- return *this;
- }
- //float * Vector2
- Vector2 operator*(float scalar, const Vector2 & v)
- {
- return { v.x * scalar, v.y * scalar };
- }
- Vector2 operator*=(float scalar, const Vector2 & v)
- {
- Vector2 vec = v;
- vec.x *= scalar; vec.y *= scalar;
- return vec;
- }
- //Division
- Vector2 Vector2::operator/(float scalar) const
- {
- return { x / scalar, y / scalar };
- }
- Vector2 Vector2::operator/=(float scalar)
- {
- x /= scalar; y /= scalar;
- return *this;
- }
- //Assignment
- Vector2 Vector2::operator=(const Vector2 & other)
- {
- x = other.x; y = other.y;
- return *this;
- }
- //Vector3 .h
- class Vector3
- {
- public:
- //Data
- union
- {
- struct
- {
- float x, y;
- union { float z, w; }; //Vector3 can be used as a homogeneous Vector2
- };
- struct { float r, g, b; };
- float data[3];
- };
- Vector3(); //Default constructor
- Vector3(float a_x, float a_y, float a_z);
- ~Vector3();
- float& operator [] (int index) { return data[index]; } //Subscript operator
- operator float* () { return data; } //Casts the vector to an array of floats
- operator const float* () { return data; } //Casts the vector to a const array of floats
- //Magnitude
- float Magnitude() const { return sqrt(x*x + y * y + z * z); } //Returns the magnitude of the vector (Pythagoras)
- float MagnitudeSqr() const { return (x*x + y * y + z * z); } //Returns the squared magnitude of the vector (Pythagoras)
- float Distance(const Vector3& other) const; //Returns the distance between two points
- void Normalize(); //Makes the vector unit length
- //Dot
- float Dot(const Vector3& other) const; //Returns the dot product
- float AngleBetween(const Vector3& other) const; //Normalizes both vectors and returns the dot product
- //Cross
- Vector3 Cross(const Vector3& other) const; //Returns the cross product
- //Equality
- bool operator == (const Vector3& other); //Checks for exact equality
- bool operator != (const Vector3& other); //Checks for exact inequality
- bool Equal(const Vector3& other, float tolerance = 0.005f); //Checks for equality within the tolerance difference
- bool NotEqual(const Vector3& other, float tolerance = 0.005f); //Checks for inequality within the tolerance difference
- Vector3 Inverse(); //Flips the vector
- //Addition
- Vector3 operator + (const Vector3& other) const;
- Vector3 operator += (const Vector3& other);
- //Subtraction
- Vector3 operator - (const Vector3& other) const;
- Vector3 operator -= (const Vector3& other);
- //Multiplication
- Vector3 operator * (float scalar) const;
- Vector3 operator *= (float scalar);
- //float * Vector3
- friend Vector3 operator * (float scalar, const Vector3& v);
- friend Vector3 operator *= (float scalar, const Vector3& v);
- //Division
- Vector3 operator / (float scalar) const;
- Vector3 operator /= (float scalar);
- //Assignment
- Vector3 operator = (const Vector3& other);
- };
- //Vector3 .cpp
- Vector3::Vector3() //Default
- {
- x = 0; y = 0; z = 0;
- }
- Vector3::Vector3(float a_x, float a_y, float a_z)
- {
- x = a_x;
- y = a_y;
- z = a_z;
- }
- Vector3::~Vector3()
- {
- }
- float Vector3::Distance(const Vector3 & other) const
- {
- float diffX = x - other.x;
- float diffY = y - other.y;
- float diffZ = z - other.z;
- return sqrt(diffX*diffX + diffY * diffY + diffZ * diffZ); //Use Pythagoras theorum to find the distance between the two vectors
- }
- void Vector3::Normalize()
- {
- float mag = Magnitude();
- x /= mag;
- y /= mag;
- z /= mag;
- }
- float Vector3::Dot(const Vector3 & other) const
- {
- return x * other.x + y * other.y + z * other.z;
- }
- float Vector3::AngleBetween(const Vector3 & other) const
- {
- //Normalize the vectors first
- Vector3 a; a.Normalize();
- Vector3 b; b.Normalize();
- return acos(a.Dot(b));
- }
- Vector3 Vector3::Cross(const Vector3 & other) const
- {
- return { y * other.z - z * other.y,
- z * other.x - x * other.z,
- x * other.y - y * other.x };
- }
- //Equality
- bool Vector3::operator==(const Vector3 & other)
- {
- //If any of these are false, the vectors are not equal
- if (x != other.x) return false;
- if (y != other.y) return false;
- if (z != other.z) return false;
- return true;
- }
- bool Vector3::operator!=(const Vector3 & other)
- {
- //If all parts are the same, the vectors are equal
- if (x == other.x && y == other.y && z == other.z) return false;
- return true;
- }
- bool Vector3::Equal(const Vector3 & other, float tolerance)
- {
- //Checks if the absolute value of the difference between this element and the other element is more than the tolerance
- //If it is, then the vectors are not equal
- if (fabs(x - other.x) > tolerance) return false;
- if (fabs(y - other.y) > tolerance) return false;
- if (fabs(z - other.z) > tolerance) return false;
- return true;
- }
- bool Vector3::NotEqual(const Vector3 & other, float tolerance)
- {
- //Checks if the absolute value of the difference between all the elements and all the other elements is less than the tolerance
- //If they are all less than the tolerance, the vectors are equal
- if (fabs(x - other.x) < tolerance && fabs(y - other.y) < tolerance && fabs(z - other.z) < tolerance) return false;
- return true;
- }
- //Inverse
- Vector3 Vector3::Inverse()
- {
- x *= -1; y *= -1; z *= -1;
- return *this;
- }
- //Addition
- Vector3 Vector3::operator+(const Vector3 & other) const
- {
- return { x + other.x, y + other.y, z + other.z };
- }
- Vector3 Vector3::operator+=(const Vector3 & other)
- {
- x += other.x; y += other.y; z += other.z;
- return *this;
- }
- //Subtraction
- Vector3 Vector3::operator-(const Vector3 & other) const
- {
- return { x - other.x, y - other.y, z - other.z };
- }
- Vector3 Vector3::operator-=(const Vector3 & other)
- {
- x -= other.x; y -= other.y; z -= other.z;
- return *this;
- }
- //Multiplication
- Vector3 Vector3::operator*(float scalar) const
- {
- return { x * scalar, y * scalar, z * scalar };
- }
- Vector3 Vector3::operator*=(float scalar)
- {
- x *= scalar; y *= scalar; z *= scalar;
- return *this;
- }
- //float * Vector3
- Vector3 operator*(float scalar, const Vector3 & v)
- {
- return { v.x * scalar, v.y * scalar, v.z * scalar };
- }
- Vector3 operator*=(float scalar, const Vector3 & v)
- {
- Vector3 vec = v;
- vec.x *= scalar; vec.y *= scalar; vec.z *= scalar;
- return vec;
- }
- //Division
- Vector3 Vector3::operator/(float scalar) const
- {
- return { x / scalar, y / scalar, z / scalar };
- }
- Vector3 Vector3::operator/=(float scalar)
- {
- x /= scalar; y /= scalar; z /= scalar;
- return *this;
- }
- //Assignment
- Vector3 Vector3::operator=(const Vector3 & other)
- {
- x = other.x; y = other.y; z = other.z;
- return *this;
- }
- //Vector4 .h
- class Vector4
- {
- public:
- //Data
- union
- {
- struct { float x, y, z, w; };
- float data[4];
- };
- Vector4(); //Default constructor
- Vector4(float a_x, float a_y, float a_z, float a_w);
- ~Vector4();
- float& operator [] (int index) { return data[index]; } //Subscript operator
- operator float* () { return data; } //Casts the vector to an array of floats
- operator const float* () { return data; } //Casts the vector to a const array of floats
- //Magnitude
- float Magnitude() const { return sqrt(x*x + y * y + z * z + w * w); } //Returns the magnitude of the vector (Pythagoras)
- float MagnitudeSqr() const { return (x*x + y * y + z * z + w * w); } //Returns the squared magnitude of the vector (Pythagoras)
- float Distance(const Vector4& other) const; //Returns the distance between two points
- void Normalize(); //Makes the vector unit length
- //Dot
- float Dot(const Vector4& other) const; //Returns the dot product
- float AngleBetween(const Vector4& other) const; //Normalizes both vectors and returns the dot product
- //Cross
- Vector4 Cross(const Vector4& other) const; //Returns the cross product
- //Equality
- bool operator == (const Vector4& other); //Checks for exact equality
- bool operator != (const Vector4& other); //Checks for exact inequality
- bool Equal(const Vector4& other, float tolerance = 0.005f); //Checks for equality within the tolerance difference
- bool NotEqual(const Vector4& other, float tolerance = 0.005f); //Checks for inequality within the tolerance difference
- Vector4 Inverse(); //Flips the vector
- //Addition
- Vector4 operator + (const Vector4& other) const;
- Vector4 operator += (const Vector4& other);
- //Subtraction
- Vector4 operator - (const Vector4& other) const;
- Vector4 operator -= (const Vector4& other);
- //Multiplication
- Vector4 operator * (float scalar) const;
- Vector4 operator *= (float scalar);
- //float * Vector4
- friend Vector4 operator * (float scalar, const Vector4& v);
- friend Vector4 operator *= (float scalar, const Vector4& v);
- //Division
- Vector4 operator / (float scalar) const;
- Vector4 operator /= (float scalar);
- //Assignment
- Vector4 operator = (const Vector4& other);
- };
- //Vector4 .cpp
- Vector4::Vector4() //Default
- {
- x = 0; y = 0; z = 0; w = 0;
- }
- Vector4::Vector4(float a_x, float a_y, float a_z, float a_w)
- {
- x = a_x;
- y = a_y;
- z = a_z;
- w = a_w;
- }
- Vector4::~Vector4()
- {
- }
- float Vector4::Distance(const Vector4 & other) const
- {
- float diffX = x - other.x;
- float diffY = y - other.y;
- float diffZ = z - other.z;
- float diffT = w - other.w;
- return sqrt(diffX*diffX + diffY * diffY + diffZ * diffZ + diffT * diffT); //Use Pythagoras theorum to find the distance between the two vectors
- }
- void Vector4::Normalize()
- {
- float mag = Magnitude();
- x /= mag;
- y /= mag;
- z /= mag;
- w /= mag;
- }
- float Vector4::Dot(const Vector4 & other) const
- {
- return x * other.x + y * other.y + z * other.z + w * other.w;
- }
- float Vector4::AngleBetween(const Vector4 & other) const
- {
- //Normalize the vectors first
- Vector4 a; a.Normalize();
- Vector4 b; b.Normalize();
- return acos(a.Dot(b));
- }
- Vector4 Vector4::Cross(const Vector4 & other) const
- {
- return { y * other.z - z * other.y,
- z * other.x - x * other.z,
- x * other.y - y * other.x,
- 0 };
- }
- //Equality
- bool Vector4::operator==(const Vector4 & other)
- {
- //If any of these are false, the vectors are not equal
- if (x != other.x) return false;
- if (y != other.y) return false;
- if (z != other.z) return false;
- if (w != other.w) return false;
- return true;
- }
- bool Vector4::operator!=(const Vector4 & other)
- {
- //If all parts are the same, the vectors are equal
- if (x == other.x && y == other.y && z == other.z && w == other.w) return false;
- return true;
- }
- bool Vector4::Equal(const Vector4 & other, float tolerance)
- {
- //Checks if the absolute value of the difference between this element and the other element is more than the tolerance
- //If it is, then the vectors are not equal
- if (fabs(x - other.x) > tolerance) return false;
- if (fabs(y - other.y) > tolerance) return false;
- if (fabs(z - other.z) > tolerance) return false;
- if (fabs(w - other.w) > tolerance) return false;
- return true;
- }
- bool Vector4::NotEqual(const Vector4 & other, float tolerance)
- {
- //Checks if the absolute value of the difference between all the elements and all the other elements is less than the tolerance
- //If they are all less than the tolerance, the vectors are equal
- if (fabs(x - other.x) < tolerance && fabs(y - other.y) < tolerance && fabs(z - other.z) < tolerance && fabs(w - other.w) < tolerance) return false;
- return true;
- }
- //Inverse
- Vector4 Vector4::Inverse()
- {
- x *= -1; y *= -1; z *= -1; w *= -1;
- return *this;
- }
- //Addition
- Vector4 Vector4::operator+(const Vector4 & other) const
- {
- return { x + other.x, y + other.y, z + other.z, w + other.w };
- }
- Vector4 Vector4::operator+=(const Vector4 & other)
- {
- x += other.x; y += other.y; z += other.z; w += other.w;
- return *this;
- }
- //Subtraction
- Vector4 Vector4::operator-(const Vector4 & other) const
- {
- return { x - other.x, y - other.y, z - other.z, w - other.w };
- }
- Vector4 Vector4::operator-=(const Vector4 & other)
- {
- x -= other.x; y -= other.y; z -= other.z; w -= other.w;
- return *this;
- }
- //Multiplication
- Vector4 Vector4::operator*(float scalar) const
- {
- return { x * scalar, y * scalar, z * scalar, w * scalar };
- }
- Vector4 Vector4::operator*=(float scalar)
- {
- x *= scalar; y *= scalar; z *= scalar; w *= scalar;
- return *this;
- }
- //float * Vector4
- Vector4 operator*(float scalar, const Vector4 & v)
- {
- return { v.x * scalar, v.y * scalar, v.z * scalar, v.w * scalar };
- }
- Vector4 operator*=(float scalar, const Vector4 & v)
- {
- Vector4 vec = v;
- vec.x *= scalar; vec.y *= scalar; vec.z *= scalar; vec.w *= scalar;
- return vec;
- }
- //Division
- Vector4 Vector4::operator/(float scalar) const
- {
- return { x / scalar, y / scalar, z / scalar, w / scalar };
- }
- Vector4 Vector4::operator/=(float scalar)
- {
- x /= scalar; y /= scalar; z /= scalar; w /= scalar;
- return *this;
- }
- //Assignment
- Vector4 Vector4::operator=(const Vector4 & other)
- {
- x = other.x; y = other.y; z = other.z; w = other.w;
- return *this;
- }
- //Matrix2 .h
- class Matrix2
- {
- public:
- //Data
- union
- {
- struct
- {
- Vector2 xAxis;
- Vector2 yAxis;
- };
- Vector2 axis[2];
- float data[2][2];
- };
- static const Matrix2 identity; //Identity matrix
- Matrix2(); //Default constructor
- Matrix2(float x1, float y1, float x2, float y2);
- ~Matrix2();
- Vector2& operator [] (int index) { return axis[index]; } //Subscript operator
- const Vector2& operator [] (int index) const { return axis[index]; } //Read-only subscript operator
- operator float* () { return *data; } //Casts the matrix to an array of floats
- operator const float* () { return *data; } //Casts the matrix to a const array of floats
- Matrix2 Transpose() const; //Switch between Row-major and Column-major
- //Scale
- void SetScaled(float x, float y); //Scale the matrix
- void SetScaled(const Vector2& v); //Scale the matrix
- void Scale(float x, float y); //Scale an existing matrix
- void Scale(const Vector2& v); //Scale an existing matrix
- //Multiply by a Matrix
- Matrix2 operator * (const Matrix2& other) const;
- Matrix2 operator *= (const Matrix2& other);
- //Multiply by a Vector
- Vector2 operator * (const Vector2& other) const;
- Vector2& operator *= (const Vector2& other);
- //Assignment
- Matrix2 operator =(const Matrix2& other);
- };
- //Matrix2 .cpp
- //Identity Matrix
- const Matrix2 Matrix2::identity = Matrix2(1, 0,
- 0, 1);
- //Constructors
- Matrix2::Matrix2()
- {
- for (int r = 0; r < 2; r++)
- for (int c = 0; c < 2; c++)
- data[c][r] = 0;
- }
- Matrix2::Matrix2(float x1, float y1, float x2, float y2)
- {
- xAxis = { x1, y1 };
- yAxis = { x2, y2 };
- }
- Matrix2::~Matrix2()
- {
- }
- Matrix2 Matrix2::Transpose() const
- {
- Matrix2 result;
- for (int r = 0; r < 2; r++)
- for (int c = 0; c < 2; c++)
- result.data[r][c] = data[c][r];
- return result;
- }
- //Scaling
- void Matrix2::SetScaled(float x, float y)
- {
- xAxis = { x, 0 };
- yAxis = { 0, y };
- }
- void Matrix2::SetScaled(const Vector2 & v)
- {
- xAxis = { v.x, 0 };
- yAxis = { 0, v.y };
- }
- void Matrix2::Scale(float x, float y)
- {
- Matrix2 m;
- m.SetScaled(x, y);
- *this *= m;
- }
- void Matrix2::Scale(const Vector2 & v)
- {
- Matrix2 m;
- m.SetScaled(v.x, v.y);
- *this *= m;
- }
- //Multiplication
- Matrix2 Matrix2::operator*(const Matrix2& other) const
- {
- Matrix2 result;
- for (int r = 0; r < 2; r++)
- for (int c = 0; c < 2; c++)
- result.data[c][r] = data[0][r] * other.data[c][0] +
- data[1][r] * other.data[c][1];
- return result;
- }
- Matrix2 Matrix2::operator*=(const Matrix2 & other)
- {
- Matrix2 result;
- for (int r = 0; r < 2; r++)
- for (int c = 0; c < 2; c++)
- result.data[c][r] = data[0][r] * other.data[c][0] +
- data[1][r] * other.data[c][1];
- *this = result;
- return *this;
- }
- Vector2 Matrix2::operator*(const Vector2& other) const
- {
- Vector2 result;
- Vector2 v = other;
- for (int r = 0; r < 2; r++)
- result[r] = data[0][r] * v[0] +
- data[1][r] * v[1];
- return result;
- }
- Vector2& Matrix2::operator*=(const Vector2 & other)
- {
- Vector2 result;
- Vector2 v = other;
- for (int r = 0; r < 2; r++)
- result[r] = data[0][r] * v[0] +
- data[1][r] * v[1];
- return result;
- }
- //Assignment
- Matrix2 Matrix2::operator=(const Matrix2 & other)
- {
- for (int r = 0; r < 2; r++)
- for (int c = 0; c < 2; c++)
- data[c][r] = other.data[c][r];
- return *this;
- }
- //Matrix3 .h
- class Matrix3
- {
- public:
- //Data
- union
- {
- struct
- {
- Vector3 xAxis;
- Vector3 yAxis;
- union //Matrix3 can be used as a homogeneous Matrix2
- {
- Vector3 zAxis;
- Vector3 translation;
- };
- };
- Vector3 axis[3];
- float data[3][3];
- };
- static const Matrix3 identity; //Identity matrix
- Matrix3(); //Default constructor
- Matrix3(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3);
- ~Matrix3();
- Vector3& operator [] (int index) { return axis[index]; } //Subscript operator
- const Vector3& operator [] (int index) const { return axis[index]; } //Read-only subscript operator
- operator float* () { return *data; } //Casts the matrix to an array of floats
- operator const float* () { return *data; } //Casts the matrix to a const array of floats
- Matrix3 Transpose() const; //Switch between Row-major and Column-major
- //Scale
- void SetScaled(float x, float y, float z); //Scale the matrix
- void SetScaled(const Vector3& v); //Scale the matrix
- void Scale(float x, float y, float z); //Scale an existing matrix
- void Scale(const Vector3& v); //Scale an existing matrix
- //Rotate
- void SetEuler(float pitch, float yaw, float roll); //Rotate around all axis
- void SetRotateX(float radians); //Rotate matrix around the X axis
- void SetRotateY(float radians); //Rotate matrix around the Y axis
- void SetRotateZ(float radians); //Rotate matrix around the Z axis
- void RotateX(float radians); //Rotate an existing matrix around the X axis
- void RotateY(float radians); //Rotate an existing matrix around the Y axis
- void RotateZ(float radians); //Rotate an existing matrix around the Z axis
- //Translate (For homogeneous Matrices)
- void Translate(float x, float y); //Translate the matrix
- void Translate(const Vector3& other); //Translate the matrix
- //Multiply by a Matrix
- Matrix3 operator * (const Matrix3& other) const;
- Matrix3 operator *= (const Matrix3& other);
- //Multiply by a Vector
- Vector3 operator * (const Vector3& other) const;
- Vector3& operator *= (const Vector3& other);
- //Assignment
- Matrix3 operator =(const Matrix3& other);
- };
- //Matrix3 .cpp
- //Identity Matrix
- const Matrix3 Matrix3::identity = Matrix3(1, 0, 0,
- 0, 1, 0,
- 0, 0, 1);
- //Constructors
- Matrix3::Matrix3()
- {
- for (int r = 0; r < 3; r++)
- for (int c = 0; c < 3; c++)
- data[c][r] = 0;
- }
- Matrix3::Matrix3(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
- {
- xAxis = { x1, y1, z1 };
- yAxis = { x2, y2, z2 };
- zAxis = { x3, y3, z3 };
- }
- Matrix3::~Matrix3()
- {
- }
- Matrix3 Matrix3::Transpose() const
- {
- Matrix3 result;
- for (int r = 0; r < 3; r++)
- for (int c = 0; c < 3; c++)
- result.data[r][c] = data[c][r];
- return result;
- }
- //Scaling
- void Matrix3::SetScaled(float x, float y, float z)
- {
- xAxis = { x, 0, 0 };
- yAxis = { 0, y, 0 };
- zAxis = { 0, 0, z };
- }
- void Matrix3::SetScaled(const Vector3 & v)
- {
- xAxis = { v.x, 0, 0 };
- yAxis = { 0, v.y, 0 };
- zAxis = { 0, 0, v.z };
- }
- void Matrix3::Scale(float x, float y, float z)
- {
- Matrix3 m;
- m.SetScaled(x, y, z);
- *this = *this * m;
- }
- void Matrix3::Scale(const Vector3 & v)
- {
- Matrix3 m;
- m.SetScaled(v.x, v.y, v.z);
- *this = *this * m;
- }
- //Rotation
- void Matrix3::SetEuler(float pitch, float yaw, float roll)
- {
- Matrix3 x, y, z;
- x.SetRotateX(pitch);
- y.SetRotateX(yaw);
- z.SetRotateX(roll);
- //Combine rotations in order Z, Y, X
- *this = z * y * x;
- }
- void Matrix3::SetRotateX(float radians)
- {
- xAxis = { 1, 0, 0 }; //leave X axis unchanged
- yAxis = { 0, cosf(radians), sinf(radians) };
- zAxis = { 0, -sinf(radians), cosf(radians) };
- }
- void Matrix3::SetRotateY(float radians)
- {
- xAxis = { cosf(radians), 0, -sinf(radians) };
- yAxis = { 0, 1, 0 }; //leave Y axis unchanged
- zAxis = { sinf(radians), 0, cosf(radians) };
- }
- void Matrix3::SetRotateZ(float radians)
- {
- xAxis = { cosf(radians), sinf(radians), 0 };
- yAxis = { -sinf(radians), cosf(radians), 0 };
- zAxis = { 0, 0, 1 }; //leave Z axis unchanged
- }
- void Matrix3::RotateX(float radians)
- {
- Matrix3 m;
- m.SetRotateX(radians);
- *this *= m;
- }
- void Matrix3::RotateY(float radians)
- {
- Matrix3 m;
- m.SetRotateY(radians);
- *this *= m;
- }
- void Matrix3::RotateZ(float radians)
- {
- Matrix3 m;
- m.SetRotateZ(radians);
- *this *= m;
- }
- //Translation
- void Matrix3::Translate(float x, float y)
- {
- translation += Vector3(x, y, 0);
- }
- void Matrix3::Translate(const Vector3 & other)
- {
- translation += other;
- }
- //Multiplication
- Matrix3 Matrix3::operator*(const Matrix3 & other) const
- {
- Matrix3 result;
- for (int r = 0; r < 3; r++)
- for (int c = 0; c < 3; c++)
- result.data[c][r] = data[0][r] * other.data[c][0] +
- data[1][r] * other.data[c][1] +
- data[2][r] * other.data[c][2];
- return result;
- }
- Matrix3 Matrix3::operator*=(const Matrix3 & other)
- {
- Matrix3 result;
- for (int r = 0; r < 3; r++)
- for (int c = 0; c < 3; c++)
- result.data[c][r] = data[0][r] * other.data[c][0] +
- data[1][r] * other.data[c][1] +
- data[2][r] * other.data[c][2];
- *this = result;
- return *this;
- }
- Vector3 Matrix3::operator*(const Vector3 & other) const
- {
- Vector3 result;
- Vector3 v = other;
- for (int r = 0; r < 3; r++)
- result[r] = data[0][r] * v[0] +
- data[1][r] * v[1] +
- data[2][r] * v[2];
- return result;
- }
- Vector3& Matrix3::operator*=(const Vector3 & other)
- {
- Vector3 result;
- Vector3 v = other;
- for (int r = 0; r < 3; r++)
- result[r] = data[0][r] * v[0] +
- data[1][r] * v[1] +
- data[2][r] * v[2];
- return result;
- }
- //Assignment
- Matrix3 Matrix3::operator=(const Matrix3 & other)
- {
- for (int r = 0; r < 3; r++)
- for (int c = 0; c < 3; c++)
- data[c][r] = other.data[c][r];
- return *this;
- }
- //Matrix4 .h
- class Matrix4
- {
- public:
- //Data
- union
- {
- struct
- {
- Vector4 xAxis;
- Vector4 yAxis;
- Vector4 zAxis;
- Vector4 translation;
- };
- Vector4 axis[4];
- float data[4][4];
- };
- static const Matrix4 identity; //Identity matrix
- Matrix4(); //Default constructor
- Matrix4(float x1, float y1, float z1, float t1, float x2, float y2, float z2, float t2, float x3, float y3, float z3, float t3, float x4, float y4, float z4, float t4);
- ~Matrix4();
- Vector4& operator [] (int index) { return axis[index]; } //Subscript operator
- const Vector4& operator [] (int index) const { return axis[index]; } //Read-only subscript operator
- operator float* () { return *data; } //Casts the matrix to an array of floats
- operator const float* () { return *data; } //Casts the matrix to a const array of floats
- Matrix4 Transpose() const; //Switch between Row-major and Column-major
- //Scale
- void SetScaled(float x, float y, float z); //Scale the matrix
- void SetScaled(const Vector4& v); //Scale the matrix
- void Scale(float x, float y, float z); //Scale an existing matrix
- void Scale(const Vector4& v); //Scale an existing matrix
- //Rotate
- void SetEuler(float pitch, float yaw, float roll); //Rotate around all axis
- void SetRotateX(float radians); //Rotate matrix around the X axis
- void SetRotateY(float radians); //Rotate matrix around the Y axis
- void SetRotateZ(float radians); //Rotate matrix around the Z axis
- void RotateX(float radians); //Rotate an existing matrix around the X axis
- void RotateY(float radians); //Rotate an existing matrix around the Y axis
- void RotateZ(float radians); //Rotate an existing matrix around the Z axis
- //Translate
- void Translate(float x, float y, float z); //Translate the matrix
- void Translate(const Vector4& other); //Translate the matrix
- //Multiply by a Matrix
- Matrix4 operator * (const Matrix4& other) const;
- Matrix4 operator *= (const Matrix4& other);
- //Multiply by a Vector
- Vector4 operator * (const Vector4& other) const;
- Vector4& operator *= (const Vector4& other);
- //Assignment
- Matrix4 operator =(const Matrix4& other);
- };
- //Matrix4 .cpp
- //Identity Matrix
- const Matrix4 Matrix4::identity = Matrix4(1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1);
- //Constructors
- Matrix4::Matrix4()
- {
- for (int r = 0; r < 4; r++)
- for (int c = 0; c < 4; c++)
- data[c][r] = 0;
- }
- Matrix4::Matrix4(float x1, float y1, float z1, float t1, float x2, float y2, float z2, float t2, float x3, float y3, float z3, float t3, float x4, float y4, float z4, float t4)
- {
- xAxis = { x1, y1, z1, t1 };
- yAxis = { x2, y2, z2, t2 };
- zAxis = { x3, y3, z3, t3 };
- translation = { x4, y4, z4, t4 };
- }
- Matrix4::~Matrix4()
- {
- }
- Matrix4 Matrix4::Transpose() const
- {
- Matrix4 result;
- for (int r = 0; r < 4; r++)
- for (int c = 0; c < 4; c++)
- result.data[r][c] = data[c][r];
- return result;
- }
- //Scaling
- void Matrix4::SetScaled(float x, float y, float z)
- {
- xAxis = { x, 0, 0, 0 };
- yAxis = { 0, y, 0, 0 };
- zAxis = { 0, 0, z, 0 };
- translation = { 0, 0, 0, 1 };
- }
- void Matrix4::SetScaled(const Vector4 & v)
- {
- xAxis = { v.x, 0, 0, 0 };
- yAxis = { 0, v.y, 0, 0 };
- zAxis = { 0, 0, v.z, 0 };
- translation = { 0, 0, 0, 1 };
- }
- void Matrix4::Scale(float x, float y, float z)
- {
- Matrix4 m;
- m.SetScaled(x, y, z);
- *this *= m;
- }
- void Matrix4::Scale(const Vector4 & v)
- {
- Matrix4 m;
- m.SetScaled(v.x, v.y, v.z);
- *this *= m;
- }
- //Rotation
- void Matrix4::SetEuler(float pitch, float yaw, float roll)
- {
- Matrix4 x, y, z;
- x.SetRotateX(pitch);
- y.SetRotateX(yaw);
- z.SetRotateX(roll);
- //Combine rotations in order Z, Y, X
- *this = z * y * x;
- }
- void Matrix4::SetRotateX(float radians)
- {
- xAxis = { 1, 0, 0, 0 }; //leave X axis unchanged
- yAxis = { 0, cosf(radians), sinf(radians), 0 };
- zAxis = { 0, -sinf(radians), cosf(radians), 0 };
- translation = { 0, 0, 0, 1 };
- }
- void Matrix4::SetRotateY(float radians)
- {
- xAxis = { cosf(radians), 0, -sinf(radians), 0 };
- yAxis = { 0, 1, 0, 0 }; //leave Y axis unchanged
- zAxis = { sinf(radians), 0, cosf(radians), 0 };
- translation = { 0, 0, 0, 1 };
- }
- void Matrix4::SetRotateZ(float radians)
- {
- xAxis = { cosf(radians), sinf(radians), 0, 0 };
- yAxis = { -sinf(radians), cosf(radians), 0, 0 };
- zAxis = { 0, 0, 1, 0 }; //leave Z axis unchanged
- translation = { 0, 0, 0, 1 };
- }
- void Matrix4::RotateX(float radians)
- {
- Matrix4 m;
- m.SetRotateX(radians);
- *this *= m;
- }
- void Matrix4::RotateY(float radians)
- {
- Matrix4 m;
- m.SetRotateY(radians);
- *this *= m;
- }
- void Matrix4::RotateZ(float radians)
- {
- Matrix4 m;
- m.SetRotateZ(radians);
- *this *= m;
- }
- //Translation
- void Matrix4::Translate(float x, float y, float z)
- {
- translation += Vector4(x, y, z, 0);
- }
- void Matrix4::Translate(const Vector4 & other)
- {
- translation += other;
- }
- //Multiplication
- Matrix4 Matrix4::operator*(const Matrix4 & other) const
- {
- Matrix4 result;
- for (int r = 0; r < 4; r++)
- for (int c = 0; c < 4; c++)
- result.data[c][r] = data[0][r] * other.data[c][0] +
- data[1][r] * other.data[c][1] +
- data[2][r] * other.data[c][2] +
- data[3][r] * other.data[c][3];
- return result;
- }
- Matrix4 Matrix4::operator*=(const Matrix4 & other)
- {
- Matrix4 result;
- for (int r = 0; r < 4; r++)
- for (int c = 0; c < 4; c++)
- result.data[c][r] = data[0][r] * other.data[c][0] +
- data[1][r] * other.data[c][1] +
- data[2][r] * other.data[c][2] +
- data[3][r] * other.data[c][3];
- *this = result;
- return *this;
- }
- Vector4 Matrix4::operator*(const Vector4 & other) const
- {
- Vector4 result;
- Vector4 v = other;
- for (int r = 0; r < 4; r++)
- result[r] = data[0][r] * v[0] +
- data[1][r] * v[1] +
- data[2][r] * v[2] +
- data[3][r] * v[3];
- return result;
- }
- Vector4& Matrix4::operator*=(const Vector4 & other)
- {
- Vector4 result;
- Vector4 v = other;
- for (int r = 0; r < 4; r++)
- result[r] = data[0][r] * v[0] +
- data[1][r] * v[1] +
- data[2][r] * v[2] +
- data[3][r] * v[3];
- return result;
- }
- //Assignment
- Matrix4 Matrix4::operator=(const Matrix4 & other)
- {
- for (int r = 0; r < 4; r++)
- for (int c = 0; c < 4; c++)
- data[c][r] = other.data[c][r];
- return *this;
- }
- //Binary .h
- class Binary
- {
- public:
- static void DecimalToBinary(char* binaryString, int len, int value); //Convert a decimal number to a binary number
- static int BinaryToDecimal(const char* binaryString); //Convert a binary number to a decimal number
- static void SetBit(char& bitfield, char bit, char value); //Set a bit in a bitfield
- static char CheckBit(char& bitfield, char bit); //Check if a bit is set in a bitfield
- };
- //Binary .cpp
- void Binary::DecimalToBinary(char * binaryString, int len, int value)
- {
- int i = 0;
- while (value > 0)
- {
- if (i > len) //If i is larger than the length of the array, i is out of bounds
- break;
- binaryString[i] = value % 2 + '0'; //Stores the remainder (either 1 or 0)
- value /= 2; //Reduces value to half its size
- i++;
- }
- }
- int Binary::BinaryToDecimal(const char * binaryString)
- {
- int decimalValue = 0, length = 0, indexCounter = 0;
- //Get length of string
- int i = 0;
- while (binaryString[i] == '0' || binaryString[i] == '1')
- {
- length++;
- i++;
- }
- for (int i = 0; i < length; i++)
- {
- //0 in binary is just 0, so it only needs to add on the 1s
- if (binaryString[i] == '1')
- decimalValue += pow(2, indexCounter);
- indexCounter++;
- }
- return decimalValue;
- }
- void Binary::SetBit(char & bitfield, char bit, char value)
- {
- bitfield |= (bit << value);
- }
- char Binary::CheckBit(char & bitfield, char bit)
- {
- return bitfield & bit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement