-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathvector2d.h
135 lines (105 loc) · 5.4 KB
/
vector2d.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2019 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine" and was originally part of the "Irrlicht Engine"
// For conditions of distribution and use, see copyright notice in nabla.h
// See the original file in irrlicht source for authors
#ifndef __NBL_POINT_2D_H_INCLUDED__
#define __NBL_POINT_2D_H_INCLUDED__
#include "dimension2d.h"
namespace nbl
{
namespace core
{
//! 2d vector template class with lots of operators and methods.
/** As of Irrlicht 1.6, this class supercedes position2d, which should
be considered deprecated. */
template <class T>
class vector2d// : public AllocationOverrideDefault
{
public:
//! Default constructor (null vector)
vector2d() : X(0), Y(0) {}
//! Constructor with two different values
vector2d(T nx, T ny) : X(nx), Y(ny) {}
//! Constructor with the same value for both members
explicit vector2d(T n) : X(n), Y(n) {}
//! Copy constructor
vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
// operators
vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
bool operator==(const vector2d<T>& other) const { return !operator!=(other); }
bool operator!=(const vector2d<T>& other) const { return X!=other.X||Y!=other.Y; }
// functions
vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
//! Returns if this vector interpreted as a point is on a line between two other points.
/** It is assumed that the point is on the line.
\param begin Beginning vector to compare between.
\param end Ending vector to compare between.
\return True if this vector is between begin and end, false if not. */
bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
{
if (begin.X != end.X)
{
return ((begin.X <= X && X <= end.X) ||
(begin.X >= X && X >= end.X));
}
else
{
return ((begin.Y <= Y && Y <= end.Y) ||
(begin.Y >= Y && Y >= end.Y));
}
}
//! Sets this vector to the linearly interpolated vector between a and b.
/** \param a first vector to interpolate with, maximum at 1.0f
\param b second vector to interpolate with, maximum at 0.0f
\param d Interpolation value between 0.0f (all vector b) and 1.0f (all vector a)
Note that this is the opposite direction of interpolation to getInterpolated_quadratic()
*/
vector2d<T>& interpolate(const vector2d<T>& a, const vector2d<T>& b, double d)
{
X = (T)((double)b.X + ( ( a.X - b.X ) * d ));
Y = (T)((double)b.Y + ( ( a.Y - b.Y ) * d ));
return *this;
}
//! X coordinate of vector.
T X;
//! Y coordinate of vector.
T Y;
};
//! Typedef for float 2d vector.
typedef vector2d<float> vector2df;
//! Typedef for integer 2d vector.
typedef vector2d<int32_t> vector2di;
template<class S, class T>
vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
// These methods are declared in dimension2d, but need definitions of vector2d
template<class T>
dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
template<class T>
bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
} // end namespace core
} // end namespace nbl
#endif