Last active
February 18, 2024 13:30
-
-
Save MichaelEstes/35c26ccb04f657d753a3 to your computer and use it in GitHub Desktop.
Geometry Vector Classes in C++ (2D & 3D, Length, Normalize, Dot Product & Cross Product(3D), angles)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Start of Vectors.h file | |
#include <cmath> | |
#ifndef FLYNNVECTOR_H | |
#define FLYNNVECTOR_H | |
#define pi 3.14159 | |
class FlynnVector3; | |
class FlynnVector2{ | |
public: | |
float x, y; | |
FlynnVector2() : x(0), y(0){}; | |
FlynnVector2(float X, float Y) : x(X), y(Y){} | |
float GetLength() const { return sqrt((x * x) + (y * y)); } | |
void Normalize(){ *this /= GetLength(); } | |
FlynnVector2 Normalized(){ return *this /= GetLength(); } | |
float Dot(FlynnVector2 rightVec) const { return (x * rightVec.x) + (y * rightVec.y); } | |
float Angle(FlynnVector2); | |
float GreaterAngle(FlynnVector2 rightVec){ return float((2 * pi) - Angle(rightVec)); } | |
void Vec3ToVec2(FlynnVector3); | |
FlynnVector2 operator+ (FlynnVector2) const; | |
FlynnVector2 operator- (FlynnVector2) const; | |
FlynnVector2 operator* (float) const; | |
FlynnVector2 operator/ (float) const; | |
FlynnVector2& operator+= (FlynnVector2); | |
FlynnVector2& operator-= (FlynnVector2); | |
FlynnVector2& operator*= (float); | |
FlynnVector2& operator/= (float); | |
FlynnVector2& operator= (FlynnVector2); | |
bool operator== (const FlynnVector2&) const; | |
bool operator!= (const FlynnVector2&) const; | |
}; | |
class FlynnVector3{ | |
public: | |
float x, y, z; | |
FlynnVector3() : x(0), y(0), z(0){}; | |
FlynnVector3(float X, float Y, float Z) : x(X), y(Y), z(Z) {} | |
float GetLength() const { return sqrt((x * x) + (y * y) + (z * z)); } | |
void Normalize(){ *this /= GetLength(); } | |
FlynnVector3 Normalized(){ return *this /= GetLength(); } | |
float Dot(FlynnVector3 rightVec) const { return (x * rightVec.x) + (y * rightVec.y) + (z * rightVec.z); } | |
FlynnVector3 Cross(FlynnVector3); | |
float Angle(FlynnVector3); | |
float GreaterAngle(FlynnVector3 rightVec){ return float((2 * pi) - Angle(rightVec)); } | |
void Vec2ToVec3(FlynnVector2 rightVec){ x = rightVec.x, y = rightVec.y, z = 0; } | |
FlynnVector3 operator+ (FlynnVector3) const; | |
FlynnVector3 operator- (FlynnVector3) const; | |
FlynnVector3 operator* (float) const; | |
FlynnVector3 operator/ (float) const; | |
FlynnVector3& operator+= (FlynnVector3); | |
FlynnVector3& operator-= (FlynnVector3); | |
FlynnVector3& operator*= (float); | |
FlynnVector3& operator/= (float); | |
FlynnVector3& operator= (FlynnVector3); | |
bool operator== (const FlynnVector3&) const; | |
bool operator!= (const FlynnVector3&) const; | |
}; | |
class FlynnVector4 : public FlynnVector3{ | |
public: | |
float w; | |
}; | |
#endif | |
//Start of Vectors.cpp file | |
#include "FlynnVector.h" | |
float FlynnVector2::Angle(FlynnVector2 rightVec){ | |
FlynnVector2 tempVec = Normalized(); | |
float temp = tempVec.Dot(rightVec.Normalized()); | |
return float(acos(temp)); | |
} | |
void FlynnVector2::Vec3ToVec2(FlynnVector3 rightVec){ | |
x = rightVec.x; | |
y = rightVec.y; | |
} | |
FlynnVector2 FlynnVector2::operator+(FlynnVector2 rightVec)const{ | |
return FlynnVector2(*this) += rightVec; | |
} | |
FlynnVector2 FlynnVector2::operator-(FlynnVector2 rightVec) const{ | |
return FlynnVector2(*this) -= rightVec; | |
} | |
FlynnVector2 FlynnVector2::operator*(float scalar) const{ | |
return FlynnVector2(*this) *= scalar; | |
} | |
FlynnVector2 FlynnVector2::operator/(float scalar) const{ | |
return FlynnVector2(*this) /= scalar; | |
} | |
FlynnVector2& FlynnVector2::operator+=(FlynnVector2 rightVec){ | |
x += rightVec.x; | |
y += rightVec.y; | |
return *this; | |
} | |
FlynnVector2& FlynnVector2::operator-=(FlynnVector2 rightVec){ | |
x -= rightVec.x; | |
y -= rightVec.y; | |
return *this; | |
} | |
FlynnVector2& FlynnVector2::operator*=(float scalar){ | |
x *= scalar; | |
y *= scalar; | |
return *this; | |
} | |
FlynnVector2& FlynnVector2::operator/=(float scalar){ | |
x /= scalar; | |
y /= scalar; | |
return *this; | |
} | |
FlynnVector2& FlynnVector2::operator=(FlynnVector2 rightVec){ | |
x = rightVec.x; | |
y = rightVec.y; | |
return *this; | |
} | |
bool FlynnVector2::operator==(const FlynnVector2& rightVec) const{ | |
return (x == rightVec.x && y == rightVec.y); | |
} | |
bool FlynnVector2::operator!=(const FlynnVector2& rightVec) const{ | |
return !operator==(rightVec); | |
} | |
FlynnVector3 FlynnVector3::Cross(FlynnVector3 rightVec){ | |
return FlynnVector3((y * rightVec.z) - (z * rightVec.y), | |
(z * rightVec.x) - (x * rightVec.z), | |
(x * rightVec.y) - (y * rightVec.x)); | |
} | |
float FlynnVector3::Angle(FlynnVector3 rightVec){ | |
FlynnVector3 tempVec = Normalized(); | |
float temp = tempVec.Dot(rightVec.Normalized()); | |
return float(acos(temp)); | |
} | |
FlynnVector3 FlynnVector3::operator+(FlynnVector3 rightVec)const{ | |
return FlynnVector3(*this) += rightVec; | |
} | |
FlynnVector3 FlynnVector3::operator-(FlynnVector3 rightVec) const{ | |
return FlynnVector3(*this) -= rightVec; | |
} | |
FlynnVector3 FlynnVector3::operator*(float scalar) const{ | |
return FlynnVector3(*this) *= scalar; | |
} | |
FlynnVector3 FlynnVector3::operator/(float scalar) const{ | |
return FlynnVector3(*this) /= scalar; | |
} | |
FlynnVector3& FlynnVector3::operator+=(FlynnVector3 rightVec){ | |
x += rightVec.x; | |
y += rightVec.y; | |
z += rightVec.z; | |
return *this; | |
} | |
FlynnVector3& FlynnVector3::operator-=(FlynnVector3 rightVec){ | |
x -= rightVec.x; | |
y -= rightVec.y; | |
z -= rightVec.z; | |
return *this; | |
} | |
FlynnVector3& FlynnVector3::operator*=(float scalar){ | |
x *= scalar; | |
y *= scalar; | |
z *= scalar; | |
return *this; | |
} | |
FlynnVector3& FlynnVector3::operator/=(float scalar){ | |
x /= scalar; | |
y /= scalar; | |
z /= scalar; | |
return *this; | |
} | |
FlynnVector3& FlynnVector3::operator=(FlynnVector3 rightVec){ | |
x = rightVec.x; | |
y = rightVec.y; | |
z = rightVec.z; | |
return *this; | |
} | |
bool FlynnVector3::operator==(const FlynnVector3& rightVec) const{ | |
return (x == rightVec.x && | |
y == rightVec.y && | |
z == rightVec.z); | |
} | |
bool FlynnVector3::operator!=(const FlynnVector3& rightVec) const{ | |
return !operator==(rightVec); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really helpful! Thanks for sharing your code.