-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEPoint2.h
109 lines (95 loc) · 3.02 KB
/
EPoint2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*=============================================================================*/
// Copyright 2019 Elite Engine 2.0
// Authors: Matthieu Delaere
/*=============================================================================*/
// EPoint.h: Point2D struct
/*=============================================================================*/
#ifndef ELITE_MATH_POINT2
#define ELITE_MATH_POINT2
#include "EPoint.h"
#include "EVector.h"
#include "EMathUtilities.h"
namespace Elite
{
//=== POINT2 SPECIALIZATION ===
template<typename T>
struct Point<2, T>
{
//=== Data ===
#pragma warning(disable : 4201)
union
{
T data[2];
struct { T x, y; };
};
#pragma warning(default : 4201)
//=== Constructors ===
#pragma region Constructors
Point<2, T>() = default;
Point<2, T>(T _x, T _y)
: x(_x), y(_y) {}
Point<2, T>(const Point<2, T>& p)
: x(p.x), y(p.y) {}
Point<2, T>(Point<2, T>&& p) noexcept
:x(std::move(p.x)), y(std::move(p.y)) {}
explicit Point<2, T>(const Vector<2, T>& v)
: x(v.x), y(v.y) {}
explicit Point<2, T>(const Point<3, T>& p)
: x(p.x), y(p.y) {}
explicit Point<2, T>(const Point<4, T>& p)
: x(p.x), y(p.y) {}
#pragma endregion
//=== Conversion Operator ===
#pragma region ConversionOperator
template<typename U>
operator Point<2, U>() const //Implicit conversion to different types of Point2
{
return Point<2, U>(
static_cast<U>(this->x),
static_cast<U>(this->y));
}
#pragma endregion
//=== Arithmetic Operators ===
#pragma region ArithmeticOperators
template<typename U>
inline Point<2, T> operator+(const Vector<2, U>& v) const
{ return Point<2, T>(x + static_cast<T>(v.x), y + static_cast<T>(v.y)); }
template<typename U>
inline Point<2, T> operator-(const Vector<2, U>& v) const
{ return Point<2, T>(x - static_cast<T>(v.x), y - static_cast<T>(v.y)); }
template<typename U>
inline Vector<2, T> operator-(const Point<2, U>& p) const
{ return Vector<2, T>(x - static_cast<T>(p.x), y - static_cast<T>(p.y)); }
#pragma endregion
//=== Compound Assignment Operators ===
#pragma region CompoundAssignmentOperators
inline Point<2, T>& operator=(const Point<2, T>& p)
{ x = p.x; y = p.y; return *this; }
inline Point<2, T>& operator+=(const Vector<2, T>& v)
{ x += v.x; y += v.y; return *this; }
inline Point<2, T>& operator-=(const Vector<2, T>& v)
{ x -= v.x; y -= v.y; return *this; }
#pragma endregion
//=== Relational Operators ===
#pragma region RelationalOperators
inline bool operator==(const Point<2, T>& p) const
{ return AreEqual<T>(x, p.x) && AreEqual<T>(y, p.y); }
inline bool operator!=(const Point<2, T>& p) const
{ return !(*this == p); }
#pragma endregion
//=== Member Access Operators ===
#pragma region MemberAccessOperators
inline T operator[](uint8_t i) const
{
assert((i < 2) && "ERROR: index of Point2 [] operator is out of bounds!");
return data[i];
}
inline T& operator[](uint8_t i)
{
assert((i < 2) && "ERROR: index of Point2 [] operator is out of bounds!");
return data[i];
}
#pragma endregion
};
}
#endif