summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-06-13 16:02:07 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-06-13 16:02:07 +0200
commitf1c0772af07f0339bda5a9abeaf4f869de9868e1 (patch)
treef85ba8441d0727cd467591e37003f84fac9579ef
parent63ffd831743289112eb3dcf07aa3cc4afd6dc81b (diff)
Added vector- and matrix-classes.
-rw-r--r--matrix.cpp45
-rw-r--r--matrix.h26
-rw-r--r--vector.cpp133
-rw-r--r--vector.h58
4 files changed, 262 insertions, 0 deletions
diff --git a/matrix.cpp b/matrix.cpp
new file mode 100644
index 0000000..5970123
--- /dev/null
+++ b/matrix.cpp
@@ -0,0 +1,45 @@
+#include "matrix.h"
+
+#include <cmath>
+
+Matrix2::Matrix2() {
+ m[0][0] = m[0][1] =
+ m[1][0] = m[1][1] = 0;
+}
+
+Matrix2::Matrix2(float m00, float m01, float m10, float m11) {
+ m[0][0] = m00;
+ m[0][1] = m01;
+ m[1][0] = m10;
+ m[1][1] = m11;
+}
+
+Matrix2 Matrix2::operator*(const Matrix2& mat) {
+ return Matrix2(
+ m[0][0] * mat.m[0][0] + m[0][1] * mat.m[1][0],
+ m[0][0] * mat.m[0][1] + m[0][1] * mat.m[1][1],
+ m[1][0] * mat.m[0][0] + m[1][1] * mat.m[1][0],
+ m[1][0] * mat.m[0][1] + m[1][1] * mat.m[1][1]);
+}
+
+Matrix2 Matrix2::operator*(float f) {
+ return Matrix2(
+ m[0][0] * f,
+ m[0][1] * f,
+ m[1][0] * f,
+ m[1][1] * f);
+}
+
+Vector2 Matrix2::operator*(const Vector2& v) {
+ return Vector2(
+ m[0][0] * v.x + m[0][1] * v.y,
+ m[1][0] * v.x + m[1][1] * v.y);
+}
+
+Matrix2 rotation_matrix(float rot) {
+ float s = sinf(rot);
+ float c = cosf(rot);
+ return Matrix2(
+ c, -s,
+ s, c);
+} \ No newline at end of file
diff --git a/matrix.h b/matrix.h
new file mode 100644
index 0000000..8be483a
--- /dev/null
+++ b/matrix.h
@@ -0,0 +1,26 @@
+#ifndef MATRIX_H
+#define MATRIX_H
+
+#include "vector.h"
+
+template<int M, int N>
+class Matrix {
+ public:
+ float m[M][N];
+};
+
+class Matrix2 : Matrix<2, 2> {
+ public:
+ Matrix2();
+ Matrix2(float m00, float m01, float m10, float m11);
+
+ Matrix2 operator*(const Matrix2& mat);
+
+ Matrix2 operator*(float f);
+
+ Vector2 operator*(const Vector2& v);
+};
+
+Matrix2 rotation_matrix(float rot);
+
+#endif
diff --git a/vector.cpp b/vector.cpp
new file mode 100644
index 0000000..6c71991
--- /dev/null
+++ b/vector.cpp
@@ -0,0 +1,133 @@
+#include "vector.h"
+
+Vector2::Vector2() {
+ x = y = 0;
+}
+
+Vector2::Vector2(float p1, float p2) {
+ x = p1;
+ y = p2;
+}
+
+Vector2::Vector2(const Vector2& v) {
+ x = v.x;
+ y = v.y;
+}
+
+Vector2& Vector2::operator=(const Vector2& v) {
+ x = v.x;
+ y = v.y;
+ return *this;
+}
+
+Vector2& Vector2::operator+=(const Vector2& v) {
+ x += v.x;
+ y += v.y;
+ return *this;
+}
+
+Vector2& Vector2::operator+(const Vector2& v) {
+ return Vector2(*this) += v;
+}
+
+Vector2& Vector2::operator-=(const Vector2& v) {
+ x -= v.x;
+ y -= v.y;
+ return *this;
+}
+
+Vector2& Vector2::operator-(const Vector2& v) {
+ return Vector2(*this) -= v;
+}
+
+Vector3::Vector3() : Vector2() {
+ z = 0;
+}
+
+Vector3::Vector3(float p1, float p2, float p3) {
+ x = p1;
+ y = p2;
+ z = p3;
+}
+
+Vector3::Vector3(const Vector3& v) {
+ x = v.x;
+ y = v.y;
+ z = v.z;
+}
+
+Vector3& Vector3::operator=(const Vector3& v) {
+ x = v.x;
+ y = v.y;
+ z = v.z;
+ return *this;
+}
+
+Vector3& Vector3::operator+=(const Vector3& v) {
+ x += v.x;
+ y += v.y;
+ z += v.z;
+ return *this;
+}
+
+Vector3& Vector3::operator+(const Vector3& v) {
+ return Vector3(*this) += v;
+}
+
+Vector3& Vector3::operator-=(const Vector3& v) {
+ x -= v.x;
+ y -= v.y;
+ z -= v.z;
+ return *this;
+}
+
+Vector3& Vector3::operator-(const Vector3& v) {
+ return Vector3(*this) -= v;
+}
+
+Vector4::Vector4() : Vector3() {
+ w = 0;
+}
+
+Vector4::Vector4(float p1, float p2, float p3, float p4) : Vector3(p1, p2, p3) {
+ w = p4;
+}
+
+Vector4::Vector4(const Vector4& v) {
+ x = v.x;
+ y = v.y;
+ z = v.z;
+ w = v.w;
+}
+
+Vector4& Vector4::operator=(const Vector4& v) {
+ x = v.x;
+ y = v.y;
+ z = v.z;
+ w = v.w;
+ return *this;
+}
+
+Vector4& Vector4::operator+=(const Vector4& v) {
+ x += v.x;
+ y += v.y;
+ z += v.z;
+ w += v.w;
+ return *this;
+}
+
+Vector4& Vector4::operator+(const Vector4& v) {
+ return Vector4(*this) += v;
+}
+
+Vector4& Vector4::operator-=(const Vector4& v) {
+ x -= v.x;
+ y -= v.y;
+ z -= v.z;
+ w -= v.w;
+ return *this;
+}
+
+Vector4& Vector4::operator-(const Vector4& v) {
+ return Vector4(*this) -= v;
+}
diff --git a/vector.h b/vector.h
new file mode 100644
index 0000000..8d85684
--- /dev/null
+++ b/vector.h
@@ -0,0 +1,58 @@
+#ifndef _VECTOR_H_
+#define _VECTOR_H_
+
+class Vector2 {
+ public:
+ union {
+ float x, r;
+ };
+ union {
+ float y, g;
+ };
+
+ Vector2();
+ Vector2(float p1, float p2);
+ Vector2(const Vector2& v);
+
+ Vector2& operator=(const Vector2& v);
+ Vector2& operator+=(const Vector2& v);
+ Vector2& operator+(const Vector2& v);
+ Vector2& operator-=(const Vector2& v);
+ Vector2& operator-(const Vector2& v);
+};
+
+class Vector3 : public Vector2 {
+ public:
+ union {
+ float z, b;
+ };
+
+ Vector3();
+ Vector3(float p1, float p2, float p3);
+ Vector3(const Vector3& v);
+
+ Vector3& operator=(const Vector3& v);
+ Vector3& operator+=(const Vector3& v);
+ Vector3& operator+(const Vector3& v);
+ Vector3& operator-=(const Vector3& v);
+ Vector3& operator-(const Vector3& v);
+};
+
+class Vector4 : public Vector3 {
+ public:
+ union {
+ float w, a;
+ };
+
+ Vector4();
+ Vector4(float p1, float p2, float p3, float p4);
+ Vector4(const Vector4& v);
+
+ Vector4& operator=(const Vector4& v);
+ Vector4& operator+=(const Vector4& v);
+ Vector4& operator+(const Vector4& v);
+ Vector4& operator-=(const Vector4& v);
+ Vector4& operator-(const Vector4& v);
+};
+
+#endif