-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector2D.cpp
99 lines (79 loc) · 1.4 KB
/
Vector2D.cpp
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
#include "Vector2D.h"
Vector2D::Vector2D()
{
m_x = 0;
m_y = 0;
}
Vector2D::Vector2D(float x, float y)
{
m_x = x;
m_y = y;
}
Vector2D Vector2D::operator+(const Vector2D& v2)
{
return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
}
Vector2D Vector2D::operator+=(const Vector2D& v2)
{
m_x += v2.m_x;
m_y += v2.m_y;
return *this; //we return the self object.
}
Vector2D Vector2D::operator-(const Vector2D& v2)
{
return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
}
Vector2D Vector2D::operator-=(const Vector2D& v2)
{
m_x -= v2.m_x;
m_y -= v2.m_y;
return *this;
}
Vector2D Vector2D::operator*(float scalar)
{
return Vector2D(m_x * scalar, m_y * scalar);
}
Vector2D Vector2D::operator*=(float scalar)
{
m_x *= scalar;
m_y *= scalar;
return *this;
}
Vector2D Vector2D::operator/(float scalar)
{
return Vector2D(m_x / scalar, m_y / scalar);
}
Vector2D Vector2D::operator/=(float scalar)
{
m_x /= scalar;
m_y /= scalar;
return *this;
}
float Vector2D::getX() const
{
return m_x;
}
float Vector2D::getY() const
{
return m_y;
}
float Vector2D::length() const
{
return sqrt(m_x * m_x + m_y * m_y);
}
void Vector2D::setX(float x)
{
m_x = x;
}
void Vector2D::setY(float y)
{
m_y = y;
}
void Vector2D::normalize()
{
float aux_length = length();
if (aux_length > 0) //don't divide 0!
{
(*this) *= 1 / aux_length;
}
}