Vivid
Loading...
Searching...
No Matches
Vec.h
1#pragma once
2
3#include <cmath>
4#include <glm/glm.hpp>
5
12namespace Vivid::Maths
13{
20 struct Vec4
21 {
22 float x, y, z, w;
23
24 Vec4()
25 : x(0.0f)
26 , y(0.0f)
27 , z(0.0f)
28 , w(0.0f)
29 {
30 }
31
32 Vec4(float x, float y, float z, float w)
33 : x(x)
34 , y(y)
35 , z(z)
36 , w(w)
37 {
38 }
39
40 Vec4 operator*(float scalar) { return Vec4(x * scalar, y * scalar, z * scalar, w * scalar); }
41 Vec4 operator+(Vec4 other) { return Vec4(x + other.x, y + other.y, z + other.z, w + other.w); }
42 };
43
50 struct Vec3
51 {
52 float x, y, z;
53
54 Vec3()
55 : x(0.0f)
56 , y(0.0f)
57 , z(0.0f)
58 {
59 }
60
61 Vec3(float x)
62 {
63 this->x = x;
64 this->y = x;
65 this->z = x;
66 }
67
68 Vec3(float x, float y, float z)
69 : x(x)
70 , y(y)
71 , z(z)
72 {
73 }
74
75 Vec3(const Vec3& other)
76 : x(other.x)
77 , y(other.y)
78 , z(other.z)
79 {
80 }
81
82 Vec3 operator*(float scalar) { return Vec3(x * scalar, y * scalar, z * scalar); }
83 Vec3 operator+(Vec3 other) { return Vec3(x + other.x, y + other.y, z + other.z); }
84 Vec3 operator-(Vec3 other) { return Vec3(x - other.x, y - other.y, z - other.z); }
85 void operator+=(Vec3 other)
86 {
87 x += other.x;
88 y += other.y;
89 z += other.z;
90 }
91 void operator-=(Vec3 other)
92 {
93 x -= other.x;
94 y -= other.y;
95 z -= other.z;
96 }
97
98 glm::vec3 ToGLM() { return glm::vec3(x, y, z); }
99 };
100
107 struct Vec2
108 {
109 float x, y;
110
111 Vec2()
112 : x(0.0f)
113 , y(0.0f)
114 {
115 }
116
117 Vec2(float x, float y)
118 : x(x)
119 , y(y)
120 {
121 }
122
123 Vec2 operator*(float scalar) { return Vec2(x * scalar, y * scalar); }
124
125 Vec2 operator*(double scalar) { return Vec2(x * scalar, y * scalar); }
126
127 Vec2 operator+(Vec2 other) { return Vec2(x + other.x, y + other.y); }
128
129 Vec2 operator-(Vec2 other) { return Vec2(x - other.x, y - other.y); }
130
131 Vec2 Perpendicular() { return Vec2(-y, x); }
132
133 Vec2 Normalize()
134 {
135 float length = sqrt(x * x + y * y);
136 return Vec2(x / length, y / length);
137 }
138 };
139}
Contains a 2D vector.
Definition: Vec.h:108
Contains a 3D vector.
Definition: Vec.h:51
Contains a 4D vector.
Definition: Vec.h:21