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/mathlib.cpp | |
parent | 62817a0737a7ea7e0a6d54647648b9ab07280f44 (diff) |
(mkosarek) Decent mathlib
Diffstat (limited to 'shared_cpp/mathlib.cpp')
-rw-r--r-- | shared_cpp/mathlib.cpp | 236 |
1 files changed, 227 insertions, 9 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; } |