-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec2.cpp
71 lines (60 loc) · 983 Bytes
/
vec2.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
#include "vec2.h"
vec2::vec2()
{
this->x = 1;
this->y = 1;
}
vec2::vec2(float x, float y)
{
this->x = x;
this->y = y;
}
void vec2::add(vec2 vec)
{
this->x += vec.x;
this->y += vec.y;
}
vec2 vec2::add(vec2 a, vec2 b)
{
vec2 result = vec2(a.getX() + b.getX(), a.getY() + b.getY());
return result;
}
void vec2::mult(float scaler)
{
this->x *= scaler;
this->y *= scaler;
}
void vec2::div(float div)
{
if (div != 0)
{
this->x /= div;
this->y /= div;
}
}
void vec2::sub(vec2 vec)
{
this->x -= vec.x;
this->y -= vec.y;
}
vec2 vec2::sub(vec2 a, vec2 b)
{
vec2 result = vec2(a.getX() - b.getX(), a.getY() - b.getY());
return result;
}
void vec2::normalize()
{
float mag = this->getMag();
if (mag)
div(mag);
}
void vec2::setMag(float mag)
{
this->normalize();
this->mult(mag);
}
void vec2::limit(float limit)
{
if (this->getMag() > limit)
this->setMag(limit);
}