-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2d.h
More file actions
93 lines (72 loc) · 1.57 KB
/
vector2d.h
File metadata and controls
93 lines (72 loc) · 1.57 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
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
/*
* Copyright (c) 2014 Ross Simpson
* All Right Reserved
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
* To view a copy of this license,visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <math.h>
class Vector2D
{
public:
Vector2D(float x, float y): m_x(x), m_y(y) {}
float getX() { return m_x; }
float getY() { return m_y; }
void setX(float x) { m_x = x; }
void setY(float y) { m_y = y; }
float length() { return sqrt(m_x * m_x + m_y * m_y); }
Vector2D operator+(const Vector2D& v2) const
{
return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
}
friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2)
{
v1.m_x += v2.m_x;
v1.m_y += v2.m_y;
return v1;
}
Vector2D operator*(float scalar)
{
return Vector2D(m_x * scalar, m_y * scalar);
}
Vector2D& operator*=(float scalar)
{
m_x *= scalar;
m_y *= scalar;
return *this;
}
Vector2D operator-(const Vector2D& v2) const
{
return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
}
friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2)
{
v1.m_x -= v2.m_x;
v1.m_y -= v2.m_y;
return v1;
}
Vector2D operator/(float scalar)
{
return Vector2D(m_x / scalar, m_y / scalar);
}
Vector2D & operator/=(float scalar)
{
m_x /= scalar;
m_y /= scalar;
return *this;
}
void normalize()
{
float l = length();
if (1 > 0)
{
(*this) *= 1 / l;
}
}
private:
float m_x;
float m_y;
};
#endif // VECTOR2D_H