Skip to content

Instantly share code, notes, and snippets.

@KeithBrown39423
Created February 27, 2023 23:35
Show Gist options
  • Select an option

  • Save KeithBrown39423/85d091fe4124fff3786255076fccaeee to your computer and use it in GitHub Desktop.

Select an option

Save KeithBrown39423/85d091fe4124fff3786255076fccaeee to your computer and use it in GitHub Desktop.
#include <cmath>
#include <string>
class Vector3 {
public:
Vector3(float a);
Vector3(float x, float y, float z);
float x, y, z;
std::string toString() const;
Vector3 operator+(const Vector3& other) const;
Vector3 operator-(const Vector3& other) const;
Vector3 operator*(const Vector3& other) const;
Vector3 operator/(const Vector3& other) const;
Vector3& operator+=(const Vector3& other);
Vector3& operator-=(const Vector3& other);
Vector3& operator*=(const Vector3& other);
Vector3& operator/=(const Vector3& other);
bool operator==(const Vector3& other) const;
bool operator!=(const Vector3& other) const;
float length() const;
float lengthSquared() const;
float distance(const Vector3& other) const;
Vector3& normalize();
float dot(const Vector3& other) const;
Vector3 cross(const Vector3& other) const;
static Vector3 Zero;
static Vector3 One;
static Vector3 Up;
static Vector3 Down;
static Vector3 Left;
static Vector3 Right;
static Vector3 Forward;
static Vector3 Back;
};
Vector3::Vector3(float a) {
this->x = a;
this->y = a;
this->z = a;
}
Vector3::Vector3(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
std::string Vector3::toString() const {
return "Vector3(" + std::to_string(this->x) + ", " + std::to_string(this->y) + ", " + std::to_string(this->z) + ")";
}
Vector3 Vector3::operator+(const Vector3& other) const {
return Vector3(this->x + other.x, this->y + other.y, this->z + other.z);
}
Vector3 Vector3::operator-(const Vector3& other) const {
return Vector3(this->x - other.x, this->y - other.y, this->z - other.z);
}
Vector3 Vector3::operator*(const Vector3& other) const {
return Vector3(this->x * other.x, this->y * other.y, this->z * other.z);
}
Vector3 Vector3::operator/(const Vector3& other) const {
return Vector3(this->x / other.x, this->y / other.y, this->z / other.z);
}
Vector3& Vector3::operator+=(const Vector3& other) {
*this = *this + other;
return *this;
}
Vector3& Vector3::operator-=(const Vector3& other) {
*this = *this - other;
return *this;
}
Vector3& Vector3::operator*=(const Vector3& other) {
*this = *this * other;
return *this;
}
Vector3& Vector3::operator/=(const Vector3& other) {
*this = *this / other;
return *this;
}
bool Vector3::operator==(const Vector3& other) const {
return this->x == other.x && this->y == other.y && this->z == other.z;
}
bool Vector3::operator!=(const Vector3& other) const {
return !(*this == other);
}
float Vector3::length() const {
return sqrt(pow(this->x, 2) + pow(this->y, 2) + pow(this->z, 2));
}
float Vector3::lengthSquared() const {
return pow(this->x, 2) + pow(this->y, 2) + pow(this->z, 2);
}
float Vector3::distance(const Vector3& other) const {
return sqrt(pow(this->x - other.x, 2) + pow(this->y - other.y, 2) + pow(this->z - other.z, 2));
}
Vector3& Vector3::normalize() {
float length = this->length();
*this /= Vector3(length);
return *this;
}
float Vector3::dot(const Vector3& other) const {
return this->x * other.x + this->y * other.y + this->z * other.z;
}
Vector3 Vector3::cross(const Vector3& other) const {
return Vector3(
this->y * other.z - this->z * other.y,
this->z * other.x - this->x * other.z,
this->x * other.y - this->y * other.x);
}
Vector3 Vector3::Zero = Vector3(0);
Vector3 Vector3::One = Vector3(1);
Vector3 Vector3::Up = Vector3(0, 1, 0);
Vector3 Vector3::Down = Vector3(0, -1, 0);
Vector3 Vector3::Left = Vector3(-1, 0, 0);
Vector3 Vector3::Right = Vector3(1, 0, 0);
Vector3 Vector3::Forward = Vector3(0, 0, 1);
Vector3 Vector3::Back = Vector3(0, 0, -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment