Created
December 20, 2016 12:27
-
-
Save attashe/268a7cfa8567590d1aead878933bf1ab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <iostream> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| #include <math.h> | |
| using namespace std; | |
| class Quat { | |
| public: | |
| double fi, x, y, z; | |
| Quat() {}; | |
| Quat(double ifi, double ix, double iy, double iz) { | |
| fi = ifi; x = ix; y = iy; z = iz; | |
| } | |
| ~Quat() {}; | |
| Quat& operator=(const Quat& obj) { | |
| x = obj.x; y = obj.y; z = obj.z; fi = obj.fi; | |
| return *this; | |
| } | |
| Quat(const Quat& obj) { | |
| (*this) = obj; | |
| } | |
| Quat operator +() { | |
| return *this; | |
| } | |
| Quat operator - () const { | |
| return Quat(-fi, -x, -y, -z); | |
| } | |
| Quat operator+=(const Quat& obj) { | |
| x += obj.x; y += obj.y; z += obj.z; fi += obj.fi; | |
| return *this; | |
| } | |
| Quat operator-=(const Quat& obj) { | |
| return (*this) += (-obj); | |
| } | |
| Quat operator + (const Quat& b) const { | |
| return Quat(*this) += b; | |
| //return Quat(fi + b.fi, x + b.x, y + b.y, z + b.z); | |
| } | |
| Quat operator-(const Quat& b) { | |
| return Quat(*this) -= b; | |
| } | |
| Quat operator*(double r) const { | |
| return Quat(*this) *= r; | |
| //return Quat(fi*r, x*r, y*r, z*r); | |
| } | |
| Quat& operator*=(double r) { | |
| x *= r; y *= r; z *= r; fi *= r; | |
| return *this; | |
| } | |
| Quat operator*(const Quat& r) const { | |
| return Quat(fi*r.fi - x*r.x - y*r.y - z*r.z, | |
| y*r.z - z*r.y + r.x*fi + x*r.fi, | |
| z*r.x - x*r.z + r.y*fi + y*r.fi, | |
| x*r.y - y*r.x + r.z*fi + z*r.fi); | |
| } | |
| Quat conjugate() { | |
| return Quat(fi, -x, -y, -z); | |
| } | |
| double magnitude() { | |
| return sqrt(x*x + y*y + z*z + fi*fi); | |
| } | |
| private: | |
| }; | |
| ostream& operator<<(ostream& out, const Quat& obj) { | |
| return out << "[" << obj.fi << ", (" << obj.x << "," << obj.y << "," << obj.z << ")]"; | |
| } | |
| int main() { | |
| Quat q1 = Quat(5, 23, 1, -2); | |
| Quat q2 = Quat(12, 2, -13, 3); | |
| cout << q1 << endl; | |
| cout << q1 * 5 << endl; | |
| cout << q1.magnitude() << endl; | |
| cout << -q1 << endl; | |
| cout << q1 + q2 << endl; | |
| cout << q1 * q2 << endl; | |
| cin.get(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment