summaryrefslogtreecommitdiff
path: root/frontend/shared_cpp/mathlib.h
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-03-22 20:54:51 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-03-22 20:54:51 -0400
commitc36d05d5aed2f8f7c6342b174692146e2d11c386 (patch)
tree8e54047d7b6db7e3d21ccfad6b8c4965d42c09fa /frontend/shared_cpp/mathlib.h
parentc29a911cd1a3f23f66478f205cace14487aadc56 (diff)
Refactored frontend, beginnings of general cpp layer, and beginning roadmap
Diffstat (limited to 'frontend/shared_cpp/mathlib.h')
-rw-r--r--frontend/shared_cpp/mathlib.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/frontend/shared_cpp/mathlib.h b/frontend/shared_cpp/mathlib.h
new file mode 100644
index 0000000..93ddbbd
--- /dev/null
+++ b/frontend/shared_cpp/mathlib.h
@@ -0,0 +1,130 @@
+#pragma once
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+struct Vector2 {
+ float x = 0;
+ float y = 0;
+
+ Vector2 operator+(Vector2 other) {
+ return { x + other.x, y + other.y };
+ }
+
+ Vector2 operator-(Vector2 other) {
+ return { x - other.x, y - other.y };
+ }
+
+ Vector2 operator*(float s) {
+ return { x * s, y * s };
+ }
+
+ float dot(Vector2 other) {
+ return x * other.x + y * other.y;
+ }
+
+ float length() {
+ return sqrtf(x * x + y * y);
+ }
+
+ Vector2 normalize() {
+ float len = length();
+ float inverseLength = len == 0 ? 1.0 : 1.0 / len;
+
+ return { x * inverseLength, y * inverseLength };
+ }
+
+ Vector2 negate() {
+ return { -x, -y };
+ }
+
+ Vector2 getPerp() {
+ return { -y, x };
+ }
+};
+
+struct Vector3 {
+ float x = 0.f;
+ float y = 0.f;
+ float z = 0.f;
+};
+
+struct Vector4 {
+ float x = 0.f;
+ float y = 0.f;
+ float z = 0.f;
+ float w = 0.f;
+};
+
+struct Mat4x4 {
+ float m[16] = {
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ };
+
+ Mat4x4 copy() {
+ Mat4x4 result;
+ memcpy(result.m, m, sizeof(float) * 16);
+ return result;
+ }
+
+ Mat4x4 scale(Vector3 v) {
+ Mat4x4 result = copy();
+ result.m[0] = result.m[0] * v.x;
+ result.m[5] = result.m[5] *v.y;
+ result.m[10] = result.m[10] * v.z;
+ return result;
+ }
+
+ Mat4x4 translate(Vector3 v) {
+ Mat4x4 result = copy();
+ result.m[12] += v.x;
+ result.m[13] += v.y;
+ result.m[14] += v.z;
+ return result;
+ }
+
+ Mat4x4 translateByVec2(Vector2 v) {
+ Mat4x4 result = copy();
+ result.m[12] += v.x;
+ result.m[13] += v.y;
+ return result;
+ }
+
+ Mat4x4 rotate2D(float angle) {
+ Mat4x4 result = copy();
+ result.m[0] = cos(angle);
+ result.m[1] = -sin(angle);
+ result.m[4] = sin(angle);
+ result.m[5] = cos(angle);
+ return result;
+ }
+
+ Vector2 multByVec2(Vector2 v) {
+ Vector4 vec4 = { v.x, v.y, 0.0, 1.0 };
+ return {
+ vec4.x * m[0] + vec4.y * m[4] + vec4.z * m[8] + vec4.w * m[12],
+ vec4.x * m[1] + vec4.y * m[5] + vec4.z * m[9] + vec4.w * m[13]
+ };
+ }
+
+ Mat4x4 getOrthographicMatrix(float left, float right, float bottom, float top) {
+ Mat4x4 result;
+ result.m[0] = 2.0 / (right - left);
+ result.m[5] = 2.0 / (top - bottom);
+ result.m[10] = 1.0;
+ result.m[12] = -(right + left) / (right - left);
+ result.m[13] = -(top + bottom) / (top - bottom);
+ return result;
+ }
+
+ void print() {
+ printf("[ ");
+ for (int idx = 0; idx < 16; idx++) {
+ printf("%d, ", idx);
+ }
+ printf(" ]\n");
+ }
+};