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