-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
93 lines (76 loc) · 1.23 KB
/
Vector.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
#include "Vector.h"
Vector::Vector()
{
x = 0;
y = 0;
}
Vector::Vector(float newX, float newY)
{
x = newX;
y = newY;
}
Vector::~Vector()
{
}
float Vector::length()
{
return sqrt(x * x + y * y);
}
void Vector::normalize(float number)
{
float length = this->length();
if (length != 0) {
x = x / length;
y = y / length;
x = x * number;
y = y * number;
}
}
void Vector::add(Vector add)
{
x = x + add.x;
y = y + add.y;
}
void Vector::subtract(Vector subtract)
{
x = x - subtract.x;
y = y - subtract.y;
}
Vector Vector::add(Vector a, Vector b)
{
Vector toReturn = Vector();
toReturn.x = a.x + b.x;
toReturn.y = a.y + b.y;
return toReturn;
}
Vector Vector::subtract(Vector a, Vector b)
{
Vector toReturn = Vector();
toReturn.x = a.x - b.x;
toReturn.y = a.y - b.y;
return toReturn;
}
float Vector::distance(Vector a, Vector b)
{
Vector subtracted = Vector();
subtracted.x = a.x - b.x;
subtracted.y = a.y - b.y;
return sqrt(subtracted.x * subtracted.x + subtracted.y * subtracted.y);
}
void Vector::divide(float number)
{
x /= number;
y /= number;
}
void Vector::multiply(float number)
{
x *= number;
y *= number;
}
void Vector::limit(float number)
{
if (length() > number)
{
normalize(number);
}
}