Last active
May 30, 2016 08:46
-
-
Save native-m/1603cb89847806fa6066b7731e2a842d to your computer and use it in GitHub Desktop.
Simple 3d vector code: Muhammad Gusti Nurfathin :)
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
// This code is licensed with: GPL or LGPL | |
#include <cmath> | |
#include <iostream> | |
template <class T> | |
class Vector3 | |
{ | |
public: | |
T x, y, z; | |
public: | |
// Constructor | |
Vector3() { x = y = z = 0; } | |
Vector3(T nx, T ny, T nz) : x(nx), y(ny), z(nz) | |
{} | |
template <class otherT> | |
Vector3(otherT otherX, otherT otherY, otherT otherZ) : x(static_cast<T>(otherX)), y(static_cast<T>(otherY)), z(static_cast<T>(otherZ)) | |
{} | |
Vector3(const Vector3<T>& other) : x(other.x), y(other.y), z(other.z) | |
{} | |
template <class otherT> | |
Vector3(const Vector3<otherT>& otherVector) : x(static_cast<T>(otherVector.x)), y(static_cast<T>(otherVector.y)), z(static_cast<T>(otherVector.z)) | |
{} | |
// Set | |
void setX(T nx) { x = nx; } | |
void setY(T ny) { y = ny; } | |
void setZ(T nz) { z = nz; } | |
void setXYZ(T nx, T ny, T nz) { x = nx; y = ny; z = nz; } | |
void setXYZ(T nArr[2]) | |
{ | |
// Failed to set if nArr size is out of range | |
if(sizeof(nArr) >= 2) | |
cerr << "nArr: Out of range"; | |
x = nArr[0]; | |
y = nArr[1]; | |
z = nArr[2]; | |
} | |
// Get | |
const T getX() { return x; } | |
const T getY() { return y; } | |
const T getZ() { return z; } | |
void setZero() { x = y = z = 0; } | |
// Access operator | |
inline Vector3<T> operator=(const Vector3<T>& v) | |
{ | |
x = v.x; | |
y = v.y; | |
z = v.z; | |
return *this; | |
} | |
template <class otherT> | |
inline Vector3<T> operator=(const Vector3<otherT>& v) | |
{ | |
x = static_cast<T>(v.x); | |
y = static_cast<T>(v.y); | |
z = static_cast<T>(v.z); | |
return *this; | |
} | |
inline T& operator[](int index) | |
{ | |
assert(n >= 0 && n <= 2); | |
if(num == 0) | |
return x; | |
else if(num == 1) | |
return y; | |
else | |
return z; | |
} | |
inline const T& operator[](int index) const | |
{ | |
assert(n >= 0 && n <= 2); | |
if(num == 0) | |
return x; | |
else if(num == 1) | |
return y; | |
else | |
return z; | |
} | |
//--------------------------------------- [Vector by Vector Operation] ---------------------------------------// | |
inline Vector3<T> operator+(const Vector3<T>& v) const | |
{ | |
return Vector3<T>(x + v.x, y + v.y, z + v.z); | |
} | |
inline Vector3<T> operator-(const Vector3<T>& v) const | |
{ | |
return Vector3<T>(x - v.x, y - v.y, z - v.z); | |
} | |
inline Vector3<T> operator*(const Vector3<T>& v) const | |
{ | |
return Vector3<T>(x * v.x, y * v.y, z * v.z); | |
} | |
inline Vector3<T> operator/(const Vector3<T>& v) const | |
{ | |
return Vector3<T>(x / v.x, y / v.y, z / v.z); | |
} | |
inline Vector3<T>& operator+=(const Vector3<T>& v) | |
{ | |
x += v.x; | |
y += v.y; | |
z += v.z; | |
return *this; | |
} | |
inline Vector3<T>& operator-=(const Vector3<T>& v) | |
{ | |
x -= v.x; | |
y -= v.y; | |
z -= v.z; | |
return *this; | |
} | |
inline Vector3<T>& operator*=(const Vector3<T>& v) | |
{ | |
x *= v.x; | |
y *= v.y; | |
z *= v.z; | |
return *this; | |
} | |
inline Vector3<T>& operator/=(const Vector3<T>& v) | |
{ | |
x /= v.x; | |
y /= v.y; | |
z /= v.z; | |
return *this; | |
} | |
//--------------------------------------- [Vector by Scalar Operation] ---------------------------------------// | |
inline Vector3<T>& operator+(const T val) const | |
{ | |
return Vector3<T>(x + val, y + val, z + val); | |
} | |
inline Vector3<T>& operator-(const T val) const | |
{ | |
return Vector3<T>(x - val, y - val, z - val); | |
} | |
inline Vector3<T>& operator*(const T val) const | |
{ | |
return Vector3<T>(x * val, y * val, z * val); | |
} | |
inline Vector3<T>& operator/(const T val) const | |
{ | |
return Vector3<T>(x / val, y / val, z / val); | |
} | |
inline Vector3<T>& operator+=(const T val) | |
{ | |
x += val; | |
y += val; | |
z += val; | |
return *this; | |
} | |
inline Vector3<T>& operator-=(const T val) | |
{ | |
x -= val; | |
y -= val; | |
z -= val; | |
return *this; | |
} | |
inline Vector3<T>& operator*=(const T val) | |
{ | |
x *= val; | |
y *= val; | |
z *= val; | |
return *this; | |
} | |
inline Vector3<T>& operator/=(const T val) | |
{ | |
x /= val; | |
y /= val; | |
z /= val; | |
return *this; | |
} | |
//--------------------------------------- Print out vector ---------------------------------------// | |
inline friend std::ostream& operator<<(std::ostream& str, const Vector3<T>& v) | |
{ | |
str << "[" << v.x << " ," << v.y << " ," << v.z << "]"; | |
return str; | |
} | |
std::string toString() const | |
{ | |
std::ostringstream toStr; | |
toStr << *this; | |
return toStr; | |
} | |
//--------------------------------------- Misc Function ---------------------------------------// | |
void addX(T val) { x += val; } | |
void addY(T val) { y += val; } | |
void addZ(T val) { z += val; } | |
void subX(T val) { x -= val; } | |
void subY(T val) { y -= val; } | |
void subZ(T val) { z -= val; } | |
bool checkManhattanDistance(const Vector3<T>& v, T distance) | |
{ | |
T dx, dy, dz; | |
dx = std::abs(v.x - x); | |
if(dx < distance) | |
return false; | |
dy = std::abs(v.y - y); | |
if(dy < distance) | |
return false; | |
dz = std::abs(v.z - z); | |
if(dz < distance) | |
return false; | |
return true; // we're within the cube | |
} | |
T getManhattanDistance() | |
{ | |
T dx, dy, dz; | |
dx = std::abs(v.x - x); | |
dy = std::abs(v.y - y); | |
dz = std::abs(v.z - z); | |
return dx + dy + dz; | |
} | |
void normalize() | |
{ | |
T mag = std::sqrt((x * x) + (y * y) + (z * z)); | |
if(mag != 0) | |
x /= mag; y /= mag; z /= mag; | |
} | |
static T dotProduct(const Vector3<T>& v1, const Vector3<T>& v2) | |
{ | |
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); | |
} | |
T dotProduct(const Vector3<T>& v) | |
{ | |
return (x * v.x) + (y * v.y) + (z * v.z); | |
} | |
static Vector3<T> crossProduct(const Vector3<T>& v1, const Vector3<T>& v2) | |
{ | |
return Vector3<T>(v1.y * v2.z - v1.z * v2.y, | |
v1.z * v2.x - v1.x * v1.z, | |
v1.x * v2.y - v1.y * v2.x); | |
} | |
Vector3<T> crossProduct(const Vector3<T>& v) | |
{ | |
return Vector3<T>(y * v.z - z * v.y, | |
z * v.x - x * v.z, | |
x * v.y - y * v.x); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment