[930] | 1 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 2 | // VECTOR4D.cpp
|
---|
| 3 | // Function definitions for 4d vector class
|
---|
| 4 | // You may use this code however you wish, but if you do, please credit me and
|
---|
| 5 | // provide a link to my website in a readme file or similar
|
---|
| 6 | // Downloaded from: www.paulsprojects.net
|
---|
| 7 | // Created: 20th July 2002
|
---|
| 8 | // Modified: 15th August 2002 - prevent divide by zero in operator VECTOR3D()
|
---|
| 9 | // Modified: 8th November 2002 - Changed Constructor layout
|
---|
| 10 | // - Some speed Improvements
|
---|
| 11 | // - Corrected Lerp
|
---|
| 12 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 13 |
|
---|
| 14 | #include "Maths.h"
|
---|
| 15 |
|
---|
| 16 | void VECTOR4D::RotateX(double angle)
|
---|
| 17 | {
|
---|
| 18 | (*this)=GetRotatedX(angle);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | VECTOR4D VECTOR4D::GetRotatedX(double angle) const
|
---|
| 22 | {
|
---|
| 23 | VECTOR3D v3d(x, y, z);
|
---|
| 24 |
|
---|
| 25 | v3d.RotateX(angle);
|
---|
| 26 |
|
---|
| 27 | return VECTOR4D(v3d.x, v3d.y, v3d.z, w);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | void VECTOR4D::RotateY(double angle)
|
---|
| 31 | {
|
---|
| 32 | (*this)=GetRotatedY(angle);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | VECTOR4D VECTOR4D::GetRotatedY(double angle) const
|
---|
| 36 | {
|
---|
| 37 | VECTOR3D v3d(x, y, z);
|
---|
| 38 |
|
---|
| 39 | v3d.RotateY(angle);
|
---|
| 40 |
|
---|
| 41 | return VECTOR4D(v3d.x, v3d.y, v3d.z, w);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | void VECTOR4D::RotateZ(double angle)
|
---|
| 45 | {
|
---|
| 46 | (*this)=GetRotatedZ(angle);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | VECTOR4D VECTOR4D::GetRotatedZ(double angle) const
|
---|
| 50 | {
|
---|
| 51 | VECTOR3D v3d(x, y, z);
|
---|
| 52 |
|
---|
| 53 | v3d.RotateZ(angle);
|
---|
| 54 |
|
---|
| 55 | return VECTOR4D(v3d.x, v3d.y, v3d.z, w);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void VECTOR4D::RotateAxis(double angle, const VECTOR3D & axis)
|
---|
| 59 | {
|
---|
| 60 | (*this)=GetRotatedAxis(angle, axis);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | VECTOR4D VECTOR4D::GetRotatedAxis(double angle, const VECTOR3D & axis) const
|
---|
| 64 | {
|
---|
| 65 | VECTOR3D v3d(x, y, z);
|
---|
| 66 |
|
---|
| 67 | v3d.RotateAxis(angle, axis);
|
---|
| 68 |
|
---|
| 69 | return VECTOR4D(v3d.x, v3d.y, v3d.z, w);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | VECTOR4D operator*(float scaleFactor, const VECTOR4D & rhs)
|
---|
| 74 | {
|
---|
| 75 | return rhs*scaleFactor;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | bool VECTOR4D::operator==(const VECTOR4D & rhs) const
|
---|
| 79 | {
|
---|
| 80 | if(x==rhs.x && y==rhs.y && z==rhs.z && w==rhs.w)
|
---|
| 81 | return true;
|
---|
| 82 |
|
---|
| 83 | return false;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | VECTOR4D::operator VECTOR3D()
|
---|
| 87 | {
|
---|
| 88 | if(w==0.0f || w==1.0f)
|
---|
| 89 | return VECTOR3D(x, y, z);
|
---|
| 90 | else
|
---|
| 91 | return VECTOR3D(x/w, y/w, z/w);
|
---|
| 92 | }
|
---|