summaryrefslogtreecommitdiff
path: root/task06/Guschin/Matrix.h
diff options
context:
space:
mode:
Diffstat (limited to 'task06/Guschin/Matrix.h')
-rw-r--r--task06/Guschin/Matrix.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/task06/Guschin/Matrix.h b/task06/Guschin/Matrix.h
index 2334edf..aedfa7f 100644
--- a/task06/Guschin/Matrix.h
+++ b/task06/Guschin/Matrix.h
@@ -1,5 +1,7 @@
#pragma once
+#include <math.h>
+
class vec3;
class vec4;
@@ -8,6 +10,7 @@ class vec2
public:
float x, y;
vec2() {}
+ vec2(vec3 v);
vec2(float a, float b) : x(a), y(b) {}
vec2& operator*=(const vec2& v)
@@ -33,9 +36,36 @@ class vec3
public:
float x, y, z;
vec3() {}
+ vec3(vec4 v);
vec3(float a, float b, float c) : x(a), y(b), z(c) {}
vec3(vec2 v, float c) : vec3(v.x, v.y, c) {}
+ vec3& operator+=(const vec3& v)
+ {
+ x += v.x;
+ y += v.y;
+ z += v.z;
+ return *this;
+ }
+
+ const vec3 operator+(const vec3& v)
+ {
+ return vec3(*this) += v;
+ }
+
+ vec3& operator-=(const vec3& v)
+ {
+ x -= v.x;
+ y -= v.y;
+ z -= v.z;
+ return *this;
+ }
+
+ const vec3 operator-(const vec3& v)
+ {
+ return vec3(*this) -= v;
+ }
+
vec3& operator*=(const vec3& v)
{
x *= v.x;
@@ -44,11 +74,22 @@ public:
return *this;
}
+ vec3& operator*=(const float& n)
+ {
+ (*this) *= vec3(n, n, n);
+ return *this;
+ }
+
const vec3 operator*(const vec3& v)
{
return vec3(*this) *= v;
}
+ const vec3 operator*(const float& n)
+ {
+ return vec3(*this) *= n;
+ }
+
float& operator[](int i)
{
return ((float*)this)[i];
@@ -83,6 +124,9 @@ public:
}
};
+vec2::vec2(vec3 v) : x(v.x), y(v.y) {};
+vec3::vec3(vec4 v) : x(v.x), y(v.y), z(v.z) {};
+
float dot(vec2 v1, vec2 v2)
{
vec2 tmp = v1 * v2;
@@ -205,6 +249,33 @@ public:
{
return mat3(*this) *= m;
}
+
+ mat3& operator+=(const mat3 &m)
+ {
+ mat3 B(m);
+ for (int i = 0; i < 3; i++)
+ (*this)[i] += B[i];
+
+ return *this;
+ }
+
+ const mat3 operator+(const mat3 &m)
+ {
+ return mat3(*this) += m;
+ }
+
+ mat3& operator*=(const float &n)
+ {
+ for (int i = 0; i < 3; i++)
+ (*this)[i] *= n;
+
+ return *this;
+ }
+
+ const mat3 operator*(const float &n)
+ {
+ return mat3(*this) *= n;
+ }
};
class mat2
@@ -275,3 +346,24 @@ vec3 normalize(vec4 v)
return vec3(v.x / v.a, v.y / v.a, v.z / v.a);
}
+mat3 crossM(vec3 p)
+{
+ return mat3(vec3( 0.f, -p.z, p.y),
+ vec3( p.z, 0.f, -p.x),
+ vec3(-p.y, p.x, 0.f));
+}
+
+vec3 cross(vec3 p, vec3 q)
+{
+ return crossM(p) * q;
+}
+
+float length(vec3 p)
+{
+ return sqrtf(dot(p, p));
+}
+
+vec3 norm(vec3 p)
+{
+ return normalize(vec4(p, length(p)));
+}