diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-07-20 20:41:16 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-07-20 20:41:16 -0400 |
commit | 26d073d768c1a0560fa4358246acd1e308eff7b6 (patch) | |
tree | 75c3ee2ec030de8698743ad03078bad45fa80f57 /shared_cpp | |
parent | 62817a0737a7ea7e0a6d54647648b9ab07280f44 (diff) |
(mkosarek) Decent mathlib
Diffstat (limited to 'shared_cpp')
-rw-r--r-- | shared_cpp/mathlib.cpp | 236 | ||||
-rw-r--r-- | shared_cpp/mathlib.h | 59 | ||||
-rw-r--r-- | shared_cpp/types.h | 2 |
3 files changed, 281 insertions, 16 deletions
diff --git a/shared_cpp/mathlib.cpp b/shared_cpp/mathlib.cpp index 4dd3dd0..b7748fe 100644 --- a/shared_cpp/mathlib.cpp +++ b/shared_cpp/mathlib.cpp @@ -1,3 +1,12 @@ +// mathlib.cpp +// +// Created by Matt Kosarek +// +// A file containing some common math functionality that I find +// useful in my projects. I don't like that I don't know what's happening +// in glm, so I wrote most of this myself. All mistakes are my own. +// + #include "mathlib.h" #include <cstdlib> @@ -71,17 +80,147 @@ void Vector2::printDebug(const char* name) { // *************************************** // Vector3 +Vector3::Vector3() { }; + +Vector3::Vector3(float value) { + x = value; + y = value; + z = value; +} + +Vector3::Vector3(float inX, float inY, float inZ) { + x = inX; + y = inY; + z = inZ; +} + float Vector3::length() { return sqrtf(x * x + y * y + z * z); } -Vector3 Vector3::operator+(const Vector3& other) { - return { x + other.x, y + other.y, z + other.z }; +float Vector3::dot(const Vector3& other) { + return x * other.x + y * other.y + z * other.z; +} + +Vector3 Vector3::scale(float scalar) { + return { + x * scalar, + y * scalar, + z * scalar + }; +} + +Vector3 Vector3::add(const Vector3& other) { + return { + x + other.x, + y + other.y, + z + other.z + }; +} + +Vector3 Vector3::subtract(const Vector3& other) { + return { + x - other.x, + y - other.y, + z - other.z + }; +} + +Vector3 Vector3::negate() { + return { + -x, + -y, + -z + }; +} + +Vector3 Vector3::cross(const Vector3& other) { + return { + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x + }; +} + +Vector3 Vector3::normalize() { + float len = 1.f / length(); + return { + x * len, + y * len, + z * len + }; +} + +Vector3 Vector3::operator+(const Vector3& v2) { + return add(v2); +} + +Vector3 Vector3::operator-(const Vector3& v2) { + return subtract(v2); +} + +Vector3 Vector3::operator-() { + return negate(); +} + +Vector3 Vector3::operator*(float value) { + return scale(value); +} + +Vector3 Vector3::operator*(const Vector3& v2) { + return { + x * v2.x, + y * v2.y, + z * v2.z + }; +} + +float Vector3::operator [](int index) { + switch (index) { + case 0: + return x; + case 1: + return y; + case 2: + return z; + default: + return 0; + } +} + + +void Vector3::printDebug(const char* name) { + printf("%s=Vector3(%f, %f, %f)\n", name, x, y, z); } // *************************************** // Vector4 +Vector4::Vector4() { } + +Vector4::Vector4(float value) { + x = value; + y = value; + z = value; + w = value; +} + +Vector4::Vector4(float inX, float inY, float inZ, float inW) { + x = inX; + y = inY; + z = inZ; + w = inW; +} + +Vector4 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 Vector4::toNormalizedColor() { + return fromColor(x, y, z, w); +} + float Vector4::length() { return sqrtf(x * x + y * y + z * z + w * w); } @@ -93,13 +232,92 @@ Vector4 Vector4::normalize() { return { x * inverseLength, y * inverseLength, z * inverseLength, w * inverseLength }; } -Vector4 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 }; +float Vector4::dot(const Vector4& other) { + return x * other.x + y * other.y + z * other.z + w * other.w; } -Vector4 Vector4::toNormalizedColor() { - return fromColor(x, y, z, w); +Vector4 Vector4::scale(float scalar) { + return { + x * scalar, + y * scalar, + z * scalar, + w * scalar + }; +} + +Vector4 Vector4::add(const Vector4& other) { + return { + x + other.x, + y + other.y, + z + other.z, + w + other.w + }; +} + +Vector4 Vector4::subtract(const Vector4& other) { + return { + x - other.x, + y - other.y, + z - other.z, + w - other.w + }; +} + +Vector4 Vector4::negate() { + return { -x, -y, -z, -w }; +} + +Vector4 Vector4::cross(const Vector4& other) { + return { + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x, + 1.f + }; +} + +Vector4 Vector4::operator+(const Vector4& v2) { + return add(v2); +} + +Vector4 Vector4::operator-(const Vector4& v2) { + return subtract(v2); +} + +Vector4 Vector4::operator-() { + return negate(); +} + +Vector4 Vector4::operator*(float value) { + return scale(value); +} + +Vector4 Vector4::operator*(const Vector4& v2) { + return { + x * v2.x, + y * v2.y, + z * v2.z, + w * v2.w + }; +} + +float Vector4::operator[](int index) { + switch (index) { + case 0: + return x; + case 1: + return y; + case 2: + return z; + case 3: + return w; + default: + return 0; + } +} + +void Vector4::printDebug(const char* name) { + printf("%s=Vector4(%f, %f, %f, %f)\n", name, x, y, z, w); } @@ -402,6 +620,6 @@ Mat4x4 Quaternion::toMatrix() const { }; } -float Quaternion::dot(const Quaternion& second) const { - return w * second.w + x * second.x + y * second.y + z * second.z; +float Quaternion::dot(const Quaternion& other) const { + return w * other.w + x * other.x + y * other.y + z * other.z; } diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h index cff9ece..d550e90 100644 --- a/shared_cpp/mathlib.h +++ b/shared_cpp/mathlib.h @@ -1,3 +1,12 @@ +// mathlib.h +// +// Created by Matt Kosarek +// +// A file containing some common math functionality that I find +// useful in my projects. I don't like that I don't know what's happening +// in glm, so I wrote most of this myself. All mistakes are my own. +// + #pragma once #include <cstdio> #include <cstdlib> @@ -8,6 +17,7 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define ABS(x) (x < 0 ? -x : x) #define SIGN(x) (x < 0 ? -1 : 1) +#define PI 3.141592653589793238463 struct Vector2 { float x = 0; @@ -24,8 +34,9 @@ struct Vector2 { Vector2 negate(); Vector2 getPerp(); Vector2 rotate(float angle); - void printDebug(const char* name); float determinant(Vector2 other); + + void printDebug(const char* name); }; struct Vector3 { @@ -33,20 +44,58 @@ struct Vector3 { float y = 0.f; float z = 0.f; + Vector3(); + Vector3(float value); + Vector3(float x, float y, float z); + float length(); - Vector3 operator+(const Vector3& other); + float dot(const Vector3& other); + Vector3 scale(float scalar); + Vector3 add(const Vector3& other); + Vector3 subtract(const Vector3& other); + Vector3 negate(); + Vector3 cross(const Vector3& other); + Vector3 normalize(); + + Vector3 operator+(const Vector3& v2); + Vector3 operator-(const Vector3& v2); + Vector3 operator-(); + Vector3 operator*(float value); + Vector3 operator*(const Vector3& v2); + float operator[](int index); + + void printDebug(const char* name); }; struct Vector4 { float x = 0.f; float y = 0.f; float z = 0.f; - float w = 0.f; + float w = 1.f; - float length(); - Vector4 normalize(); + Vector4(); + Vector4(float value); + Vector4(float inX = 0.f, float inY = 0.f, float inZ = 0.f, float inW = 1.f); Vector4 fromColor(float r, float g, float b, float a); Vector4 toNormalizedColor(); + + float length(); + Vector4 normalize(); + float dot(const Vector4& other); + Vector4 scale(float scalar); + Vector4 add(const Vector4& other); + Vector4 subtract(const Vector4& other); + Vector4 negate(); + Vector4 cross(const Vector4& other); + + Vector4 operator+(const Vector4& v2); + Vector4 operator-(const Vector4& v2); + Vector4 operator-(); + Vector4 operator*(float value); + Vector4 operator*(const Vector4& v2); + float operator[](int index); + + void printDebug(const char* name); }; struct Mat4x4 { diff --git a/shared_cpp/types.h b/shared_cpp/types.h index 3739699..7bac7f3 100644 --- a/shared_cpp/types.h +++ b/shared_cpp/types.h @@ -14,5 +14,3 @@ typedef unsigned long uint64; typedef float float32; typedef double float64; - -#define PI 3.14159265358979323846
\ No newline at end of file |