From 806f8ac2238318d58e2f9715bdb446af3d1c3989 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Sat, 28 Nov 2020 20:18:42 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B2=20Matrix.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task06/Guschin/Matrix.h | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'task06') 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 + 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))); +} -- cgit v1.2.3