-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
111 lines (93 loc) · 2.3 KB
/
Vector2D.cpp
File metadata and controls
111 lines (93 loc) · 2.3 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "Vector2D.h"
Vector2D::Vector2D()
{
position[0] = 0;
position[1] = 0;
}
Vector2D::Vector2D(float x, float y) {
position[0] = x;
position[1] = y;
}
Vector2D& Vector2D::operator=(Vector2D rhs) {
if (this == &rhs) {
return *this;
}
// Copy data into this vector
position[0] = rhs.getX();
position[1] = rhs.getY();
return *this;
}
Vector2D& Vector2D::operator+(Vector2D rhs) {
// Add data to this vector
position[0] += rhs.getX();
position[1] += rhs.getY();
return *this;
}
Vector2D Vector2D::operator*(float rhs) {
// Multiply values into this vector
Vector2D result = Vector2D(rhs * position[0], rhs * position[1]);
return result;
}
Vector2D Vector2D::operator-() {
Vector2D v;
v.setX(-position[0]);
v.setY(-position[1]);
return v;
}
bool Vector2D::operator==(Vector2D rhs) {
if (rhs.getX() == position[0] && rhs.getY() == position[1]) {
return true;
}
return false;
}
Vector2D Vector2D::normalize() {
float mag = getMagnitude();
Vector2D result = Vector2D(position[0] / mag, position[1] / mag);
return result;
}
float Vector2D::dotProduct(Vector2D other) {
float value = position[0] * other.getX() + position[1] * other.getY();
return value;
}
float Vector2D::getDist(Vector2D v2) {
float result = sqrt((position[0] - v2.getX())*(position[0] - v2.getX()) + (position[1] - v2.getY())*(position[1] - v2.getY()));
return result;
}
float Vector2D::getX() {
return position[0];
}
float Vector2D::getY() {
return position[1];
}
Vector2D Vector2D::getPerpendicular(int type) {
// Swap x and y values and reverse one of them based on type parameter.
float temp;
Vector2D result = *this;
if (type) {
temp = -result.getX();
result.setX(result.getY());
result.setY(temp);
}
else {
temp = -result.getY();
result.setY(result.getX());
result.setX(temp);
}
return result;
}
void Vector2D::setX(float x) {
position[0] = x;
}
void Vector2D::setY(float y) {
position[1] = y;
}
float Vector2D::getMagnitude() {
return sqrt(position[0] * position[0] + position[1] * position[1]);
}
void Vector2D::printVector() {
printf("X: %f\nY: %f\n", position[0], position[1]);
}
Vector2D::~Vector2D()
{
//dtor
}