Repository navigation
Expand file tree
/
Copy pathVector.hpp
More file actions
42 lines (36 loc) · 1.64 KB
/
Copy pathVector.hpp
File metadata and controls
42 lines (36 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#include <math.h>
class Vector {
public:
float x = 0.0f,
y = 0.0f,
z = 0.0f;
Vector() = default;
Vector(const float& x, const float& y, const float& z) :
x(x),
y(y),
z(z)
{}
inline Vector operator+(const Vector& other) const { return Vector(x + other.x, y + other.y, z + other.z); }
inline Vector operator-(const Vector& other) const { return Vector(x - other.x, y - other.y, z - other.z); }
inline Vector operator*(const Vector& other) const { return Vector(x * other.x, y * other.y, z * other.z); }
inline Vector operator*(const float& scalar) const { return Vector(x * scalar, y * scalar, z * scalar); }
inline Vector operator/(const float& scalar) const { return Vector(x / scalar, y / scalar, z / scalar); }
inline Vector& operator+=(const Vector& other) { return *this = *this + other; }
inline Vector& operator-=(const Vector& other) { return *this = *this - other; }
inline Vector& operator*=(const float& scalar) { return *this = *this * scalar; }
inline Vector& operator/=(const float& scalar) { return *this = *this / scalar; }
inline Vector Opposite() const { return Vector(-x, -y, -z); }
inline float Length() const { return std::sqrt(x * x + y * y + z * z); }
inline float Length_Squared() const { return x * x + y * y + z * z; }
inline Vector Normalize() const { return *this / Length(); }
static float Dot(const Vector& u, const Vector& v) { return u.x * v.x + u.y * v.y + u.z * v.z; }
static Vector Cross(const Vector& u, const Vector& v) {
return (Vector){
u.y * v.z - u.z * v.y,
u.z * v.x - u.x * v.z,
u.x * v.y - u.y * v.x
};
}
};
using Color = Vector;