-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.cpp
74 lines (58 loc) · 961 Bytes
/
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
#include "Vector2D.h"
float Vector2D::getX()
{
return m_x;
}
float Vector2D::getY()
{
return m_y;
}
void Vector2D::setX(float x)
{
m_x = x;
}
void Vector2D::setY(float y)
{
m_y = y;
}
float Vector2D::length()
{
return sqrt(m_x * m_x + m_y * m_y);
}
Vector2D Vector2D::operator+(const Vector2D& v2) const
{
return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
}
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-(const Vector2D& v2) const
{
return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
}
//Division
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;
}
void Vector2D::normalize()
{
float l = length();
if (l > 0)
{
(*this) *= 1/l;
}
}