aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/matrix.hpp
blob: cea5198b6a95456bafab1ba841a383d089994231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include "GraphBase.hpp"

#include <cmath>

namespace cru::platform {
struct Matrix {
  float m11;
  float m12;
  float m21;
  float m22;
  float m31;
  float m32;

  Matrix() {}

  Matrix(float m11, float m12, float m21, float m22, float m31, float m32) {
    this->m11 = m11;
    this->m12 = m12;
    this->m21 = m21;
    this->m22 = m22;
    this->m31 = m31;
    this->m32 = m32;
  }

  bool IsIdentity() const {
    return m11 == 1.0f && m12 == 0.0f && m21 == 0.0f && m22 == 1.0f &&
           m31 == 0.0f && m32 == 0.0f;
  }

  Matrix& operator*=(const Matrix& matrix) {
    *this = Product(*this, matrix);
    return *this;
  }

  Matrix operator*(const Matrix& matrix) const {
    return Product(*this, matrix);
  }

  Point TransformPoint(const Point& point) const {
    return Point{point.x * m11 + point.y * m21 + m31,
                 point.x * m12 + point.y * m22 + m32};
  }

  static Matrix Identity() {
    return Matrix{1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};
  }

  static Matrix Translation(float x, float y) {
    return Matrix{1.0f, 0.0f, 0.0f, 1.0f, x, y};
  }

  static Matrix Scale(float sx, float sy) {
    return Matrix{sx, 0.0f, 0.0f, sy, 0.0f, 0.0f};
  }

  static Matrix Rotation(float angle) {
    float r = AngleToRadian(angle);
    float s = std::sinf(r);
    float c = std::cosf(r);

    return Matrix{c, s, -s, c, 0.0f, 0.0f};
  }

  static Matrix Skew(float sx, float sy) {
    return Matrix{1.0f, sx, sy, 1.0f, 0.0f, 0.0f};
  }

  static Matrix Product(const Matrix& a, const Matrix& b) {
    return Matrix{a.m11 * b.m11 + a.m12 * b.m21,
                  a.m11 * b.m12 + a.m12 * b.m22,
                  a.m21 * b.m11 + a.m22 * b.m21,
                  a.m21 * b.m12 + a.m22 * b.m22,
                  a.m31 * b.m11 + a.m32 * b.m21 + b.m31,
                  a.m31 * b.m12 + a.m32 * b.m22 + b.m32};
  }

 private:
  static constexpr float PI = 3.1415926535f;

  static float AngleToRadian(float angle) { return angle / 180.f * PI; }
};
}  // namespace cru::platform