nomlib
Loading...
Searching...
No Matches
Vector3.hpp
1#pragma once
2/*
3Class:Vector3
4Implements:
5Author:Rich Davison
6Description:VERY simple Vector3 class. Students are encouraged to modify this as necessary!
7
8-_-_-_-_-_-_-_,------,
9_-_-_-_-_-_-_-| /\_/\ NYANYANYAN
10-_-_-_-_-_-_-~|__( ^ .^) /
11_-_-_-_-_-_-_-"" ""
12
13*/
14#include <cmath>
15#include <iostream>
16
17class Vector3 {
18public:
19 Vector3(void) {
20 ToZero();
21 }
22
23 Vector3(const float x, const float y, const float z) {
24 this->x = x;
25 this->y = y;
26 this->z = z;
27 }
28
29 ~Vector3(void){}
30
31 float x;
32 float y;
33 float z;
34
35 void Normalise() {
36 float length = Length();
37
38 if(length != 0.0f) {
39 length = 1.0f / length;
40 x = x * length;
41 y = y * length;
42 z = z * length;
43 }
44 }
45
46 void ToZero() {
47 x = y = z = 0.0f;
48 }
49
50 float Length() const {
51 return sqrt((x*x)+(y*y)+(z*z));
52 }
53
54 void Invert() {
55 x = -x;
56 y = -y;
57 z = -z;
58 }
59
60 Vector3 Inverse() const{
61 return Vector3(-x,-y,-z);
62 }
63
64 static float Dot(const Vector3 &a, const Vector3 &b) {
65 return (a.x*b.x)+(a.y*b.y)+(a.z*b.z);
66 }
67
68 static Vector3 Cross(const Vector3 &a, const Vector3 &b) {
69 return Vector3((a.y*b.z) - (a.z*b.y) , (a.z*b.x) - (a.x*b.z) , (a.x*b.y) - (a.y*b.x));
70 }
71
72 inline friend std::ostream& operator<<(std::ostream& o, const Vector3& v) {
73 o << "Vector3(" << v.x << "," << v.y << "," << v.z <<")" << std::endl;
74 return o;
75 }
76
77 inline Vector3 operator+(const Vector3 &a) const{
78 return Vector3(x + a.x,y + a.y, z + a.z);
79 }
80
81 inline Vector3 operator-(const Vector3 &a) const{
82 return Vector3(x - a.x,y - a.y, z - a.z);
83 }
84
85 inline Vector3 operator-() const{
86 return Vector3(-x,-y,-z);
87 }
88
89 inline void operator+=(const Vector3 &a){
90 x += a.x;
91 y += a.y;
92 z += a.z;
93 }
94
95 inline void operator-=(const Vector3 &a){
96 x -= a.x;
97 y -= a.y;
98 z -= a.z;
99 }
100
101 inline Vector3 operator*(const float a) const{
102 return Vector3(x * a,y * a, z * a);
103 }
104
105 inline Vector3 operator*(const Vector3 &a) const{
106 return Vector3(x * a.x,y * a.y, z * a.z);
107 }
108
109 inline Vector3 operator/(const Vector3 &a) const{
110 return Vector3(x / a.x,y / a.y, z / a.z);
111 };
112
113 inline Vector3 operator/(const float v) const{
114 return Vector3(x / v,y / v, z / v);
115 };
116
117 inline bool operator==(const Vector3 &A)const {return (A.x == x && A.y == y && A.z == z) ? true : false;};
118 inline bool operator!=(const Vector3 &A)const {return (A.x == x && A.y == y && A.z == z) ? false : true;};
119};