Skip to content

Commit

Permalink
feat(sourcepp): add more math operators to vector structs
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jul 25, 2024
1 parent cdc809e commit eaf83cc
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions include/sourcepp/math/Vector.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <cmath>

#include "Integer.h"

namespace sourcepp::math {
Expand All @@ -13,14 +15,62 @@ struct Vec2 {
return {this->x + other.x, this->y + other.y};
}

void operator+=(const Vec2& other) {
this->x += other.x;
this->y += other.y;
}

[[nodiscard]] Vec2 operator-(const Vec2& other) const {
return {this->x - other.x, this->y - other.y};
}

void operator-=(const Vec2& other) {
this->x -= other.x;
this->y -= other.y;
}

[[nodiscard]] Vec2 operator*(Arithmetic auto scalar) const {
return {this->x * scalar, this->y * scalar};
}

void operator*=(Arithmetic auto scalar) {
this->x *= scalar;
this->y *= scalar;
}

[[nodiscard]] Vec2 operator/(Arithmetic auto scalar) const {
return {this->x / scalar, this->y / scalar};
}

void operator/=(Arithmetic auto scalar) {
this->x /= scalar;
this->y /= scalar;
}

[[nodiscard]] bool operator==(const Vec2& other) const {
return this->x == other.x && this->y == other.y;
}

[[nodiscard]] Vec2 mul(const Vec2& other) const {
return {this->x * other.x, this->y * other.y};
}

[[nodiscard]] Vec2 div(const Vec2& other) const {
return {this->x / other.x, this->y / other.y};
}

[[nodiscard]] Vec2 mod(const Vec2& other) const {
if constexpr (std::floating_point<P>) {
return {std::fmod(this->x, other.x), std::fmod(this->y, other.y)};
} else {
return {this->x % other.x, this->y % other.y};
}
}

[[nodiscard]] Vec2 abs() const {
return {std::abs(this->x), std::abs(this->y)};
}

[[nodiscard]] static constexpr Vec2 zero() {
return {{}, {}};
}
Expand Down Expand Up @@ -52,14 +102,66 @@ struct Vec3 {
return {this->x + other.x, this->y + other.y, this->z + other.z};
}

void operator+=(const Vec3& other) {
this->x += other.x;
this->y += other.y;
this->z += other.z;
}

[[nodiscard]] Vec3 operator-(const Vec3& other) const {
return {this->x - other.x, this->y - other.y, this->z - other.z};
}

void operator-=(const Vec3& other) {
this->x -= other.x;
this->y -= other.y;
this->z -= other.z;
}

[[nodiscard]] Vec3 operator*(Arithmetic auto scalar) const {
return {this->x * scalar, this->y * scalar, this->z * scalar};
}

void operator*=(Arithmetic auto scalar) {
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
}

[[nodiscard]] Vec3 operator/(Arithmetic auto scalar) const {
return {this->x / scalar, this->y / scalar, this->z / scalar};
}

void operator/=(Arithmetic auto scalar) {
this->x /= scalar;
this->y /= scalar;
this->z /= scalar;
}

[[nodiscard]] bool operator==(const Vec3& other) const {
return this->x == other.x && this->y == other.y && this->z == other.z;
}

[[nodiscard]] Vec3 mul(const Vec3& other) const {
return {this->x * other.x, this->y * other.y, this->z * other.z};
}

[[nodiscard]] Vec3 div(const Vec3& other) const {
return {this->x / other.x, this->y / other.y, this->z / other.z};
}

[[nodiscard]] Vec3 mod(const Vec3& other) const {
if constexpr (std::floating_point<P>) {
return {std::fmod(this->x, other.x), std::fmod(this->y, other.y), std::fmod(this->z, other.z)};
} else {
return {this->x % other.x, this->y % other.y, this->z % other.z};
}
}

[[nodiscard]] Vec3 abs() const {
return {std::abs(this->x), std::abs(this->y), std::abs(this->z)};
}

[[nodiscard]] static constexpr Vec3 zero() {
return {{}, {}, {}};
}
Expand Down Expand Up @@ -92,14 +194,70 @@ struct Vec4 {
return {this->x + other.x, this->y + other.y, this->z + other.z, this->w + other.w};
}

void operator+=(const Vec4& other) {
this->x += other.x;
this->y += other.y;
this->z += other.z;
this->w += other.w;
}

[[nodiscard]] Vec4 operator-(const Vec4& other) const {
return {this->x - other.x, this->y - other.y, this->z - other.z, this->w - other.w};
}

void operator-=(const Vec4& other) {
this->x -= other.x;
this->y -= other.y;
this->z -= other.z;
this->w -= other.w;
}

[[nodiscard]] Vec4 operator*(Arithmetic auto scalar) const {
return {this->x * scalar, this->y * scalar, this->z * scalar, this->w * scalar};
}

void operator*=(Arithmetic auto scalar) {
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
this->w *= scalar;
}

[[nodiscard]] Vec4 operator/(Arithmetic auto scalar) const {
return {this->x / scalar, this->y / scalar, this->z / scalar, this->w / scalar};
}

void operator/=(Arithmetic auto scalar) {
this->x /= scalar;
this->y /= scalar;
this->z /= scalar;
this->w /= scalar;
}

[[nodiscard]] bool operator==(const Vec4& other) const {
return this->x == other.x && this->y == other.y && this->z == other.z && this->w == other.w;
}

[[nodiscard]] Vec4 mul(const Vec4& other) const {
return {this->x * other.x, this->y * other.y, this->z * other.z, this->z * other.z};
}

[[nodiscard]] Vec4 div(const Vec4& other) const {
return {this->x / other.x, this->y / other.y, this->z / other.z, this->w / other.w};
}

[[nodiscard]] Vec4 mod(const Vec4& other) const {
if constexpr (std::floating_point<P>) {
return {std::fmod(this->x, other.x), std::fmod(this->y, other.y), std::fmod(this->z, other.z), std::fmod(this->w, other.w)};
} else {
return {this->x % other.x, this->y % other.y, this->z % other.z, this->w % other.w};
}
}

[[nodiscard]] Vec4 abs() const {
return {std::abs(this->x), std::abs(this->y), std::abs(this->z), std::abs(this->w)};
}

[[nodiscard]] static constexpr Vec4 zero() {
return {{}, {}, {}, {}};
}
Expand Down

0 comments on commit eaf83cc

Please sign in to comment.