Created
October 6, 2018 23:07
-
-
Save WillWetzel/346aebc9dfea6dd5a66cec70084726f8 to your computer and use it in GitHub Desktop.
Programming for Games - Project 1 - Vectors and Quaternions
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
/*Name: William Wetzel - 130251255*/ | |
/*Programming for Games - Project 1*/ | |
/*2016*/ | |
/*Quaternion.cpp file. Has all methods declared in the Quaternion header file.*/ | |
#include "Quaternion.h" | |
Quaternion::Quaternion() { | |
coord = new float[4]; | |
for (int i = 0; i < 4; i++) { | |
coord[i] = 1.0f; | |
} | |
} | |
Quaternion::Quaternion(const float w, const float x, const float y, const float z) { | |
coord = new float[4]; | |
coord[0] = w; | |
coord[1] = x; | |
coord[2] = y; | |
coord[3] = z; | |
} | |
/*Copy constructor for quaternion*/ | |
Quaternion::Quaternion(const Quaternion& q) { | |
coord = new float[4]; | |
coord[0] = q.coord[0]; | |
coord[1] = q.coord[1]; | |
coord[2] = q.coord[2]; | |
coord[3] = q.coord[3]; | |
} | |
Quaternion::~Quaternion() { | |
delete[] coord; | |
for (int i = 0; i < 4; i++) { | |
coord[i] = NULL; | |
} | |
//for to delete each | |
//set to null coords[i] = NULL; | |
} | |
Quaternion Quaternion::operator+(const Quaternion& q) { | |
return Quaternion((coord[0] + q.coord[0]), (coord[1] + q.coord[1]), (coord[2] + q.coord[2]), (coord[3] + q.coord[3])); | |
} | |
Quaternion Quaternion::operator-(Quaternion& q) { | |
return Quaternion((coord[0] - q.coord[0]), (coord[1] - q.coord[1]), (coord[2] - q.coord[2]), (coord[3] - q.coord[3])); | |
} | |
Quaternion Quaternion::operator*(Quaternion& q) { | |
return Quaternion( | |
(coord[0] * q.coord[0] - (coord[1] * q.coord[1] + coord[2] * q.coord[2] + coord[3] * q.coord[3])), | |
(coord[1] = coord[0] * q.coord[1] + q.coord[0] * coord[1] + coord[2] * q.coord[3] - coord[3] * q.coord[2]), | |
(coord[2] = coord[0] * q.coord[2] + q.coord[0] * coord[2] + coord[3] * q.coord[1] - coord[1] * q.coord[3]), | |
(coord[3] = coord[0] * q.coord[3] + q.coord[0] * coord[3] + coord[1] * q.coord[2] - coord[2] * q.coord[1])); | |
} | |
float Quaternion::getMagnitude() const { | |
return sqrt((coord[0] * coord[0]) + (coord[1] * coord[1]) + (coord[2] * coord[2]) + (coord[3] * coord[3])); | |
} | |
//for division use multiplication lol wat | |
//cout statement overload | |
std::ostream& operator<<(std::ostream& outStream, const Quaternion* q) { | |
outStream << "(" << q->getCoordW() << ", " << q->getCoordX() << ", " << | |
q->getCoordY() << ", " << q->getCoordZ() << ")"; | |
return outStream; | |
} | |
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
/*Name: William Wetzel - 130251255*/ | |
/*Programming for Games - Project 1*/ | |
/*2016*/ | |
/*Quaternion header file. Declares all methods and fields to be used in Quaternion.cpp file */ | |
#include <iostream> | |
class Quaternion { | |
public: | |
Quaternion(); //Quaternion constructor | |
Quaternion(const float w, const float x, const float y, const float z); //Assignment Constructor | |
Quaternion(const Quaternion& q); //Copy Constructor | |
~Quaternion(); //Destructor | |
Quaternion operator+(const Quaternion& q); //Overloaded addition | |
Quaternion operator-(Quaternion& q); //Overloaded subtraction | |
Quaternion operator*(Quaternion& q); //Overloaded multiplication | |
float getMagnitude() const; | |
//Getters | |
inline float getCoordW() const { | |
return coord[0]; | |
} | |
inline float getCoordX() const { | |
return coord[1]; | |
} | |
inline float getCoordY() const { | |
return coord[2]; | |
} | |
inline float getCoordZ() const { | |
return coord[3]; | |
} | |
//Setters | |
inline void setCoordW(const float new_w) { | |
coord[0] = new_w; | |
} | |
inline void setCoordX(const float new_x) { | |
coord[1] = new_x; | |
} | |
inline void setCoordY(const float new_y) { | |
coord[2] = new_y; | |
} | |
inline void setCoordZ(const float new_z) { | |
coord[3] = new_z; | |
} | |
private: | |
float* coord; // :) | |
}; | |
/*cout statement overload*/ | |
std::ostream& operator<<(std::ostream& outStream, const Quaternion* q); |
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
/*Name: William Wetzel - 130251255*/ | |
/*Programming for Games - Project 1*/ | |
/*2016*/ | |
/*Test file to test both classes, Vector and Quaternion.*/ | |
#include "Vector3D.h" | |
#include "Quaternion.h" | |
#include <chrono> | |
int main(void) { | |
/*Declaring and starting system clock for efficency measurement*/ | |
std::chrono::time_point<std::chrono::system_clock> start, end; | |
start = std::chrono::system_clock::now(); | |
/*******************************Vector Class Testing**************************************/ | |
std::cout << "****************Vector Class Testing******************" << std::endl; | |
Vector a(1, 2, 3); | |
Vector b(3, 2, 1); | |
/*Testing Vectors are declared*/ | |
std::cout << "Vector a:" << a << "Vector b:" << b << std::endl; | |
/*Testing Vector Magnitude works*/ | |
std::cout << "Vector Magnitude: " << a.getMagnitude() << "\n"; | |
/*Testing Vector Addition*/ | |
Vector newVectorAdd(a.operator+(b)); | |
std::cout << "Testing new Vector Addition: (1, 2, 3) + (3, 2, 1) = " << newVectorAdd << std::endl; | |
/*Testing Vector Subtraction*/ | |
Vector newVectorSub(a.operator-(b)); | |
std::cout << "Testing new Vector Subtraction: (1, 2, 3) - (3, 2, 1) = " << newVectorSub << std::endl; | |
/*Testing Vector Multiplication by scalar*/ | |
Vector newVectorMult(a.operator*(2)); | |
std::cout << "Testing vector mult by 2: (1, 2, 3) * 2 = " << newVectorMult << std::endl; | |
/*Testing Vector Division by scalar*/ | |
Vector newVectorDiv(a.operator/(2)); | |
std::cout << "Testing vector div by 2: (1, 2, 3) / 2 = " << newVectorDiv << std::endl; | |
/*Testing VectorProduct*/ | |
float product = a.operator*(b); | |
std::cout << "Testing dot Product: (1, 2, 3) dot (3, 2, 1) " << product << std::endl; | |
/*Testing Vector cross Product*/ | |
Vector crossVector = a.operator%(b); | |
std::cout << "Testing vector cross product: (1, 2, 3) cross (3, 2, 1) = " << crossVector << std::endl; | |
/*Testing Overloaded methods*/ | |
a.operator+=(b); | |
std::cout << "Testing Overloaded Method: (1, 2, 3) + (3, 2, 1) = " << a << std::endl; | |
a.operator-=(b); | |
std::cout << "Testing Overloaded Method: (4, 4, 4) - (3, 2, 1) = " << a << "\n"; | |
a.operator/=(2); | |
std::cout << "Testing Overloaded Method: (1, 2, 3) / 2 = " << a << "\n"; | |
a.operator*=(2); //To set Vector a back to 1,2,3 for testing cross product | |
/*Testing UnitVector*/ | |
Vector test(1, 2, 3); | |
Vector unitVector = test.unitVector(); | |
std::cout << "Testing unit Vector method: Unit Vector of (1, 2, 3) = " << test << "\n"; | |
/*Testing orthogonalVector method*/ | |
Vector orthTestA(10, 5, 2); | |
Vector orthTestB(1, 1, 1); | |
Vector orthogonal = orthTestA.orthogonalVector(orthTestB); | |
std::cout << "Testing orthogonalVector Method: (10, 5, 2) and (1, 1, 1) = " << orthogonal << "\n"; | |
/**************************Quaternion Testing ***********************************/ | |
std::cout << "****************Quaternion Class Testing******************" << std::endl; | |
/*Quaternion Constructor Testing*/ | |
Quaternion *test1 = new Quaternion(1, 1, 1, 1); | |
std::cout << "Testing constructor and ostream override work, expecting (1, 1, 1, 1) = " << test1 << std::endl; | |
/*Quaternion Destructor test*/ | |
test1->~Quaternion(); | |
std::cout << "Testing Destructor, expecting (0, 0, 0, 0) = " << test1 << std::endl; | |
/*Quaternion Addition Test*/ | |
Quaternion *test2 = new Quaternion(1, 2, 3, 4); | |
Quaternion *test3 = new Quaternion(4, 3, 2, 1); | |
Quaternion *test4 = new Quaternion(test2->operator+(*(test3))); | |
std::cout << "Addition Test, (1, 2, 3, 4) + (4, 3, 2, 1) = " << test4 << std::endl; | |
test4->~Quaternion(); | |
/*Quaternion Subtraction Test*/ | |
Quaternion *test5 = new Quaternion(test2->operator-(*(test3))); | |
std::cout << "Subtraction Test: (1, 2, 3, 4) - (4, 3, 2, 1) = " << test5 << std::endl; | |
test5->~Quaternion(); | |
/*Quaternion Multiplication Test*/ | |
Quaternion *test6 = new Quaternion(test2->operator*(*(test3))); | |
std::cout << "Multiplication Test: (1, 2, 3, 4) * (4, 3, 2, 1) = " << test6 << std::endl; | |
test6->~Quaternion(); | |
/*Quaternion Division Test*/ | |
Quaternion *test7 = new Quaternion(); | |
std::cout << "Division Test: (1, 2, 3, 4) / (4, 3, 2, 1) = " << test7 << std::endl; | |
test7->~Quaternion(); | |
/*Quaternion Magnitude Test*/ | |
std::cout << "Quaternion Magnitude test, Quaternion = 1,2,3,4 = " << test2->getMagnitude() << std::endl; | |
/*Finding elapsed time for efficency measurement*/ | |
end = std::chrono::system_clock::now(); | |
std::chrono::duration<double> elapsed_seconds = end - start; | |
std::cout << "Elapsed time for efficency measurement: " << elapsed_seconds.count() << std::endl; | |
getchar(); | |
return 0; | |
} |
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
/*Name: William Wetzel - 130251255*/ | |
/*Programming for Games - Project 1*/ | |
/*2016*/ | |
/*Vector3D.cpp Has all methods declared in the Vector3D.h file. | |
*/ | |
#include "Vector3D.h" | |
/*Constructor*/ | |
Vector::Vector(const float x, const float y, const float z) { | |
coord[0] = x; | |
coord[1] = y; | |
coord[2] = z; | |
} | |
/*Copy Constructor*/ | |
Vector::Vector(const Vector& v) { | |
coord[0] = v.coord[0]; | |
coord[1] = v.coord[1]; | |
coord[2] = v.coord[2]; | |
} | |
/*Destructor*/ | |
Vector::~Vector() { | |
} | |
/*Get Magnitude Method*/ | |
float Vector::getMagnitude() const { | |
return sqrt((coord[0] * coord[0]) + (coord[1] * coord[1]) + (coord[2] * coord[2])); | |
} | |
/*Addition operator Method*/ | |
Vector Vector::operator+(const Vector& v) { | |
Vector newVector; | |
newVector.coord[0] = (coord[0] + v.coord[0]); | |
newVector.coord[1] = (coord[1] + v.coord[1]); | |
newVector.coord[2] = (coord[2] + v.coord[2]); | |
return newVector; | |
} | |
/*Subtraction operator Method*/ | |
Vector Vector::operator-(const Vector& v) { | |
Vector newVector; | |
newVector.coord[0] = (coord[0] - v.coord[0]); | |
newVector.coord[1] = (coord[1] - v.coord[1]); | |
newVector.coord[2] = (coord[2] - v.coord[2]); | |
return newVector; | |
} | |
/*Multiplication operator Method*/ | |
Vector Vector::operator*(const float scalar) { | |
Vector newVector; | |
newVector.coord[0] = (coord[0] * scalar); | |
newVector.coord[1] = (coord[1] * scalar); | |
newVector.coord[2] = (coord[2] * scalar); | |
return newVector; | |
} | |
/*Division operator Method*/ | |
Vector Vector::operator/(const float scalar) { | |
Vector newVector; | |
if (scalar != 0) { | |
newVector.coord[0] = (coord[0] / scalar); | |
newVector.coord[1] = (coord[1] / scalar); | |
newVector.coord[2] = (coord[2] / scalar); | |
} | |
return newVector; | |
} | |
/*Dot Product Method*/ | |
float Vector::operator*(const Vector v) { | |
float product = 0; | |
for (int i = 0; i <= v.getMagnitude() - 1; i++) | |
product += (coord[i])*(v.coord[i]); // += means add to product | |
return product; | |
} | |
/*Cross Product Method*/ | |
Vector Vector::operator%(const Vector v) { | |
Vector product; | |
product.coord[0] = ((coord[1] * v.coord[2]) - (coord[2] * v.coord[1])); | |
product.coord[1] = ((coord[2] * v.coord[0]) - (coord[0] * v.coord[2])); | |
product.coord[2] = ((coord[0] * v.coord[1]) - (coord[1] * v.coord[0])); | |
return product; | |
} | |
//Overloading operators | |
/*Addition operator Method*/ | |
Vector& Vector::operator+=(const Vector& v) { | |
coord[0] = coord[0] + v.coord[0]; | |
coord[1] = coord[1] + v.coord[1]; | |
coord[2] = coord[2] + v.coord[2]; | |
return *this; | |
} | |
/*Subtraction operator Method*/ | |
Vector& Vector::operator-=(const Vector& v) { | |
coord[0] = (coord[0] - v.coord[0]); | |
coord[1] = (coord[1] - v.coord[1]); | |
coord[2] = (coord[2] - v.coord[2]); | |
return *this; | |
} | |
/*Division by a scalar*/ | |
Vector& Vector::operator/=(const float scalar) { | |
if (scalar != 0) { | |
coord[0] = coord[0] / scalar; | |
coord[1] = coord[1] / scalar; | |
coord[2] = coord[2] / scalar; | |
} | |
return *this; | |
} | |
/*Multiplication by a scalar*/ | |
Vector& Vector::operator*=(const float scalar) { | |
coord[0] = (coord[0] * scalar); | |
coord[1] = (coord[1] * scalar); | |
coord[2] = (coord[2] * scalar); | |
return *this; | |
} | |
/*Producing unit vector of mag 1*/ | |
Vector Vector::unitVector() { | |
Vector newVector; | |
float magnitude = getMagnitude(); | |
if (magnitude != 0) { | |
newVector.coord[0] = coord[0] / magnitude; | |
newVector.coord[1] = coord[1] / magnitude; | |
newVector.coord[2] = coord[2] / magnitude; | |
} | |
return newVector; | |
} | |
/*Method for producing a orthogonal vector to two given vectors*/ | |
Vector Vector::orthogonalVector(const Vector& v) { | |
float dotProd = operator*(v); //Check if vectors are already orthogonal | |
if (dotProd != 0) { //If not, find orthogonal vector and return | |
Vector newVector = operator%(v); | |
return newVector; | |
} | |
else { | |
return -1; //Else, end. | |
} | |
} | |
/*Cout Overload*/ | |
std::ostream& operator<< (std::ostream& outStream, Vector v) { | |
outStream << "(" << v.getCoordX() << ", " << v.getCoordY() << ", " << v.getCoordZ() << ")"; | |
return outStream; | |
} |
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
/*Name: William Wetzel - 130251255*/ | |
/*Programming for Games - Project 1*/ | |
/*2016*/ | |
/*Vector3D Header file. Declares methods and fields to be used in Vector.cpp*/ | |
#include <iostream> | |
class Vector { | |
public: | |
Vector(const float x = 0.0f, const float y = 0.0f, const float z = 0.0f); //Cpnstructor | |
Vector(const Vector& v); //Copy Constructor | |
~Vector(); //Destructor | |
/*Operator methods*/ | |
Vector operator+(const Vector& v); | |
Vector operator-(const Vector& v); | |
Vector operator*(const float scalar); | |
Vector operator/(const float scalar); | |
float operator*(const Vector v); //Dot Product | |
Vector operator%(const Vector v); //Cross Product | |
/*Overloading Operaters*/ | |
Vector& operator+=(const Vector& v); | |
Vector& operator-=(const Vector& v); | |
Vector& operator*=(const float scalar); | |
Vector& operator/=(const float scalar); | |
Vector& operator%=(const Vector& v); | |
/*Unit vector*/ | |
Vector unitVector(); | |
/*Producing orthogonalUnitVector*/ | |
Vector orthogonalVector(const Vector& v); | |
//Getters | |
inline float getCoordX() const { | |
return coord[0]; | |
} | |
inline float getCoordY() const { | |
return coord[1]; | |
} | |
inline float getCoordZ() const { | |
return coord[2]; | |
} | |
float getMagnitude() const; | |
//Setters | |
inline void setCoordX(const float new_x) { | |
coord[0] = new_x; | |
} | |
inline void setCoordY(const float new_y) { | |
coord[1] = new_y; | |
} | |
inline void setCoordZ(const float new_z) { | |
coord[2] = new_z; | |
} | |
private: | |
float coord[3]; | |
}; | |
/*cout override*/ | |
std::ostream& operator<< (std::ostream& outStream, Vector vector); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment