From 0408d33694635cdc28db23c9c6388bd916626bde Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Mon, 9 Nov 2020 23:28:49 +0300 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D1=8B=D0=B5=204=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task04/Guschin/Matrix.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 task04/Guschin/Matrix.h (limited to 'task04/Guschin/Matrix.h') diff --git a/task04/Guschin/Matrix.h b/task04/Guschin/Matrix.h new file mode 100644 index 0000000..3aa0bbf --- /dev/null +++ b/task04/Guschin/Matrix.h @@ -0,0 +1,98 @@ +#pragma once + +class vec2 +{ +public: + float x, y; + vec2() {} + vec2(float a, float b) : x(a), y(b) {} +}; + +class vec3 +{ +public: + float x, y, z; + vec3() {} + 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; + } + + float& operator[](int i) + { + return ((float*)this)[i]; + } +}; + +float dot(vec3 v1, vec3 v2) +{ + vec3 tmp = v1 * v2; + return tmp.x + tmp.y + tmp.z; +} + +class mat3 +{ +public: + vec3 row1, row2, row3; + mat3() {} + mat3(vec3 r1, vec3 r2, vec3 r3) : row1(r1), row2(r2), row3(r3) {} + mat3(float a) + { + row1 = vec3(a, 0.f, 0.f); + row2 = vec3(0.f, a, 0.f); + row3 = vec3(0.f, 0.f, a); + } + + vec3& operator[](int i) + { + return ((vec3*)this)[i]; + } + + mat3 transpose() + { + mat3 tmp(*this); + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + (*this)[i][j] = tmp[j][i]; + return *this; + } + + const vec3 operator*(const vec3& v) + { + vec3* res = new(vec3); + for (int i = 0; i < 3; ++i) + (*res)[i] = dot((*this)[i], v); + return *res; + } + + mat3& operator*=(const mat3& m) + { + mat3 A(*this), B(m); + B.transpose(); + for (int i = 0; i < 3; ++i) + (*this)[i] = A * B[i]; + return (*this).transpose(); + } + + const mat3 operator*(const mat3& m) + { + return mat3(*this) *= m; + } +}; + +vec2 normalize(vec3 v) +{ + return vec2(v.x / v.z, v.y / v.z); +} + -- cgit v1.2.3