GCP Assignment 1
Vec3.h
Go to the documentation of this file.
1 //DISCLAMER - This is a modified version of code from one of my other assignments.
2 
3 #pragma once
4 
5 #include "math.h"
6 
10 namespace Maths
11 {
12 
16  struct Vec3
17  {
19  float x, y, z;
20 
24  Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
25 
32  Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
33 
40  Vec3(int x, int y, int z) : x((float)x), y((float)y), z((float)z) {}
41 
48  {
49  x += vecIn.x;
50  y += vecIn.y;
51  z += vecIn.y;
52  return this;
53  }
54 
61  {
62  x -= vecIn.x;
63  y -= vecIn.y;
64  z -= vecIn.z;
65  return this;
66  }
67 
72  float getLength()
73  {
74  return float(sqrt((x*x) + (y*y) + (z*z)));
75  }
76 
82  {
83  float magnitude = getLength();
84  Vec3 normalised = Vec3(x / magnitude, y / magnitude, z / magnitude);
85  return normalised;
86  }
87  };
88 
94  inline Vec3 operator - (Vec3 vecIn)
95  {
96  Vec3 vecOut;
97  vecOut.x = -vecIn.x;
98  vecOut.y = -vecIn.y;
99  vecOut.z = -vecIn.z;
100  return vecOut;
101  }
102 
109  inline Vec3 operator - (Vec3 vecInA, Vec3 vecInB)
110  {
111  Vec3 vecOut;
112  vecOut.x = vecInA.x - vecInB.x;
113  vecOut.y = vecInA.y - vecInB.y;
114  vecOut.z = vecInA.z - vecInB.z;
115  return vecOut;
116  }
117 
124  inline Vec3 operator + (Vec3 vecInA, Vec3 vecInB)
125  {
126  Vec3 vecOut;
127  vecOut.x = vecInA.x + vecInB.x;
128  vecOut.y = vecInA.y + vecInB.y;
129  vecOut.z = vecInA.z + vecInB.z;
130  return vecOut;
131  }
132 
139  inline Vec3 operator / (Vec3 vecInA, float scalar)
140  {
141  Vec3 vecOut;
142  vecOut.x = vecInA.x / scalar;
143  vecOut.y = vecInA.y / scalar;
144  vecOut.z = vecInA.z / scalar;
145  return vecOut;
146  }
147 
154  inline Vec3 operator * (Vec3 vecInA, float scalar)
155  {
156  Vec3 vecOut;
157  vecOut.x = vecInA.x * scalar;
158  vecOut.y = vecInA.y * scalar;
159  vecOut.z = vecInA.z * scalar;
160  return vecOut;
161  }
162 
169  inline Vec3 operator * (Vec3 vecInA, Vec3 vecInB)
170  {
171  Vec3 vecOut;
172  vecOut.x = vecInA.x * vecInB.x;
173  vecOut.y = vecInA.y * vecInB.y;
174  vecOut.z = vecInA.z * vecInB.z;
175  return vecOut;
176  }
177 
178 }// End of Maths namespace
Contains the Vec3 structure with functions and overloaded operators.
Definition: Vec3.h:16
Vec3 getNormalised()
Returns a normalised version of the Vec3.
Definition: Vec3.h:81
Vec3(int x, int y, int z)
Constructs the Vec3 setting the values to the input coordinates.
Definition: Vec3.h:40
Mat4 operator-(Mat4 mInA, float s)
Overloads the - operator allowing a Mat4 to be added to a scalar.
Definition: Mat4.h:216
float y
Definition: Vec3.h:19
Vec3 * operator-=(Vec3 vecIn)
Overloads the -= operator.
Definition: Vec3.h:60
Vec2 operator/(Vec2 vecInA, float scalar)
Overloads the / operator allowing a Vec2 to be divided by a scalar.
Definition: Vec2.h:121
Mat4 operator*(Mat4 mInA, float s)
Overloads the * operator allowing a Mat4 to be multiplied to a scalar.
Definition: Mat4.h:248
Vec3()
Constructs the Vec3 setting the values to 0,0,0.
Definition: Vec3.h:24
Mat4 operator+(Mat4 mIn, float s)
Overloads the + operator allowing a Mat4 to be added to a scalar.
Definition: Mat4.h:184
Vec3 * operator+=(Vec3 vecIn)
Overloads the += operator.
Definition: Vec3.h:47
float getLength()
Returns the length of the Vec3.
Definition: Vec3.h:72
The namespace for all maths code.
Definition: Convert.cpp:5
float z
Definition: Vec3.h:19
Vec3(float x, float y, float z)
Constructs the Vec3 setting the values to the input coordinates.
Definition: Vec3.h:32
float x
Position variables.
Definition: Vec3.h:19