-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec2.h
74 lines (73 loc) · 1.85 KB
/
vec2.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <stdint.h>
#include <cmath>
#include <limits>
class alignas(8) vec2 //2^3, <= 0x4 + 0x4 == 0x8
{
public:
~vec2() {};
vec2() {};
vec2(float x, float y) {
this->_x = x;
this->_y = y;
}
vec2(const vec2& vOther) {
this->_x = vOther._x;
this->_y = vOther._y;
}
inline float x() {
return this->_x;
}
inline float y() {
return this->_y;
}
inline double magnitude() {
return sqrt(pow(this->_x, 2) + pow(this->_y, 2));
}
inline vec2 operator+(const vec2& vOther) {
return vec2(this->_x + vOther._x, this->_y + vOther._y);
}
inline vec2 operator-(const vec2& vOther) {
return vec2(this->_x - vOther._x, this->_y - vOther._y);
}
inline vec2 operator*(const vec2& vOther) {
return vec2(this->_x * vOther._x, this->_y * vOther._y);
}
inline vec2 operator/(const vec2& vOther) {
if (!vOther._x || !vOther._y)
{
return infinity_vec();
}
return vec2(this->_x / vOther._x, this->_y / vOther._y);
}
inline vec2 operator+(float value) {
return vec2(this->_x + value, this->_y + value);
}
inline vec2 operator-(float value) {
return vec2(this->_x - value, this->_y - value);
}
inline vec2 operator*(float value) {
return vec2(this->_x * value, this->_y * value);
}
inline vec2 operator/(float value) {
if (value == 0.0f) {
return infinity_vec();
}
return vec2(this->_x / value, this->_y / value);
}
inline static vec2 infinity_vec() {
return vec2(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
}
inline float& operator[](size_t idx) {
return *(float*)((uintptr_t)(this) + idx * sizeof size_t);
}
inline bool equals(const vec2& vOther) {
return this->_x == vOther._x && this->_y == vOther._y;
}
inline bool valid() {
return this->_x == std::numeric_limits<float>::infinity() || this->_y == std::numeric_limits<float>::infinity();
}
private:
float _x;
float _y;
};