diff options
| author | Matthew Kosarek <mattkae@protonmail.com> | 2021-05-16 19:50:15 -0400 | 
|---|---|---|
| committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-05-16 19:50:15 -0400 | 
| commit | a00c0aab1eb5a7a55bef8ca08115bdd722ab5699 (patch) | |
| tree | 45b5c4cc8c380d0630a8e0185af7229f26dc754a /shared_cpp/mathlib.h | |
| parent | 4941a1874b6ca9d142d94df70b2aec5e0b35b94e (diff) | |
Moved the frontend directory up so that it no longer exists
Diffstat (limited to 'shared_cpp/mathlib.h')
| -rw-r--r-- | shared_cpp/mathlib.h | 227 | 
1 files changed, 227 insertions, 0 deletions
| diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h new file mode 100644 index 0000000..3ed61cd --- /dev/null +++ b/shared_cpp/mathlib.h @@ -0,0 +1,227 @@ +#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) + +struct Vector2 { +    float x = 0; +    float y = 0; + +    Vector2 operator+(Vector2 other) { +        return { x + other.x, y + other.y }; +    } + +    Vector2& operator+=(Vector2 other) { +        x += other.x; +        y += other.y; +        return *this; +    } + +    Vector2 operator-(Vector2 other) { +        return { x - other.x, y - other.y }; +    } + +    Vector2 operator*(float s) { +        return { x * s, y * s }; +    } + +    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 }; +    } + +    Vector2 rotate(float angle) { +        return { +            x * cosf(angle) - y * sinf(angle), +            x * sinf(angle) + y * cosf(angle) +        }; +    } + +    void printDebug(const char* name) { +        printf("%s=Vector2(%f, %f)\n", name, x, y); +    } + +    float determinant(Vector2 other) { +        // +        // [ a b ] +        // [ c d ] +        // +        // [ x other.x ] +        // [ y other.y ] +        // +        // det = a * d - b * c +        // det = x * other.y - other.x * y +        // +        return x * other.y - other.x * y; +    } +}; + +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; + +    float length() { +        return sqrtf(x * x + y * y + z * z + w * w); +    } + +    Vector4 normalize() { +        float len = length(); +        float inverseLength = len == 0 ? 1.0 : 1.0 / len; + +        return { x * inverseLength, y * inverseLength, z * inverseLength, w * inverseLength }; +    } + +    Vector4 fromColor(float r, float g, float b, float a) { +        float scale = 1.f / 255.f; +        return { r * scale, g * scale, b * scale, a * scale }; +    } + +	Vector4 toNormalizedColor() { +		return fromColor(x, y, z, w); +	} +}; + +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] +        }; +    } + +    Vector2 operator*(Vector2 v) { +        return multByVec2(v); +    } + +    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; +    } + +    Mat4x4 inverse() { +        Mat4x4 inv; + +        inv.m[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10]; +        inv.m[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10]; +        inv.m[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9]; +        inv.m[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9]; +        inv.m[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10]; +        inv.m[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10]; +        inv.m[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9]; +        inv.m[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9]; +        inv.m[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6]; +        inv.m[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6]; +        inv.m[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5]; +        inv.m[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5]; +        inv.m[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6]; +        inv.m[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6]; +        inv.m[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5]; +        inv.m[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5]; + +        float det = m[0] * inv.m[0] + m[1] * inv.m[4] + m[2] * inv.m[8] + m[3] * inv.m[12]; + +        if (det == 0) +            return Mat4x4(); + +        det = 1.f / det; + +        for (int i = 0; i < 16; i++) +            inv.m[i] = inv.m[i] * det; + +        return inv; +    } + +	void print() { +		printf("[ "); +		for (int idx = 0; idx < 16; idx++) { +			printf("%f, ", m[idx]); +		} +		printf(" ]\n"); +	} +}; | 
