-
Notifications
You must be signed in to change notification settings - Fork 1
/
vec2d.cpp
81 lines (69 loc) · 1.58 KB
/
vec2d.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
/*
* vec2d.cpp
*
* Created on: 2013-07-14
* Author: Liam
*/
#include "vec2d.h"
vec2d &vec2d::operator+=(const vec2d &rhs) {
point.x += rhs.point.x;
point.y += rhs.point.y;
return *this;
}
vec2d &vec2d::operator*=(const float &f) {
point.x *= f;
point.y *= f;
return *this;
}
vec2d &vec2d::operator/=(const float &f) {
point.x /= f;
point.y /= f;
return *this;
}
vec2d &vec2d::operator-=(const vec2d &rhs) {
point.x -= rhs.point.x;
point.y -= rhs.point.y;
return *this;
}
vec2d vec2d::operator+(const vec2d &rhs) {
vec2d vector;
vector.point.x = this->point.x + rhs.point.x;
vector.point.y = this->point.y + rhs.point.y;
return vector;
}
vec2d vec2d::operator*(const float &f) {
vec2d vector;
vector.point.x = this->point.x * f;
vector.point.y = this->point.y * f;
return vector;
}
vec2d vec2d::operator/(const float &f) {
vec2d vector;
vector.point.x = this->point.x / f;
vector.point.y = this->point.y / f;
return vector;
}
vec2d vec2d::operator-(const vec2d &rhs) {
vec2d vector;
vector.point.x = this->point.x - rhs.point.x;
vector.point.y = this->point.y - rhs.point.y;
return vector;
}
bool vec2d::operator==(const vec2d &rhs) {
if (point.x == rhs.point.x && point.y == rhs.point.y) return true;
else return false;
}
float vec2d::distance(const vec2d &other) {
float dist;
float xsqrd = pow(other.getX() - point.x, 2);
float ysqrd = pow(other.getY() - point.y, 2);
dist = sqrt(xsqrd + ysqrd);
return dist;
}
vec2d vec2d::normalize() const {
vec2d v;
float l = sqrt(pow(point.x, 2) + pow(point.y, 2));
v.setX(point.x / l);
v.setY(point.y / l);
return v;
}