summaryrefslogtreecommitdiff
path: root/shared_cpp/mathlib.h
blob: cff9eced26dd9b8090c590b0d629a7162fcb07e8 (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
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define ABS(x) (x < 0 ? -x : x)
#define SIGN(x) (x < 0 ? -1 : 1)

struct Vector2 {
    float x = 0;
    float y = 0;

    Vector2 operator+(Vector2 other);
    Vector2& operator+=(Vector2 other);
    Vector2 operator-(Vector2 other);
    Vector2 operator*(float s);
    Vector2 operator/(float s);
    float dot(Vector2 other);
    float length();
    Vector2 normalize();
    Vector2 negate();
    Vector2 getPerp();
    Vector2 rotate(float angle);
    void printDebug(const char* name);
    float determinant(Vector2 other);
};

struct Vector3 {
    float x = 0.f;
    float y = 0.f;
    float z = 0.f;

    float length();
    Vector3 operator+(const Vector3& other);
};

struct Vector4 {
    float x = 0.f;
    float y = 0.f;
    float z = 0.f;
    float w = 0.f;

    float length();
    Vector4 normalize();
    Vector4 fromColor(float r, float g, float b, float a);
	Vector4 toNormalizedColor();
};

struct Mat4x4 {
    float m[16] = {
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
    };

    Mat4x4 copy();
    Mat4x4 scale(Vector3 v);
    Mat4x4 translate(Vector3 v);
	Mat4x4 translateByVec2(Vector2 v);
    Mat4x4 rotate2D(float angle);
    Mat4x4 getXRotationMatrix(float angleRadians);
    Mat4x4 getYRotationMatrix(float angleRadians);
    Mat4x4 getZRotationMatrix(float angleRadians);
    Mat4x4 rotate(float xRadians, float yRadians, float zRadians);
    Vector2 multByVec2(Vector2 v);
    Vector2 operator*(Vector2 v);
    Mat4x4 multMat4x4(const Mat4x4& other);
    Mat4x4 operator*(const Mat4x4& other);
    Mat4x4 getOrthographicMatrix(float left, float right, float bottom, float top);
    Mat4x4 inverse();
	Mat4x4 getPerspectiveProjection(float near, float far, float fieldOfViewRadians, float aspectRatio);
	void print();
};

struct Quaternion {
    float x = 0;
    float y = 0;
    float z = 0;
    float w = 0;

    float operator [](int index);
	Quaternion operator*(const Quaternion& other) const;
	Quaternion operator*(const float& scale) const;
	Quaternion operator+(const Quaternion& other) const;
	Quaternion operator-(const Quaternion& other) const;
	Quaternion interpolate(const Quaternion& other, const float factor);
	Mat4x4 toMatrix() const;
	Quaternion normalize() const;
	float length() const;
	float dot(const Quaternion& other) const;
};