-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
116 lines (91 loc) · 2.07 KB
/
Vector2D.cpp
File metadata and controls
116 lines (91 loc) · 2.07 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
112
113
114
115
116
#include "Vector2D.h"
Vector2D::Vector2D(double nX, double nY) {
SetXY(nX, nY);
}
void Vector2D::SetXY(double nX = 0.0, double nY = 0.0) {
x = nX;
y = nY;
}
void Vector2D::SetDM(double dir, double mag) {
x = cos(dir) * mag;
y = sin(dir) * mag;
}
double Vector2D::GetDir() {
return atan2(x, y);
}
double Vector2D::GetMag() {
return hypot(x, y);
}
double Vector2D::X() const {
return x;
}
double Vector2D::Y() const {
return y;
}
Vector2D& Vector2D::operator+=(const Vector2D& v) {
x += v.x;
y += v.y;
return (*this);
}
Vector2D& Vector2D::operator-=(const Vector2D& v) {
x -= v.x;
y -= v.y;
return (*this);
}
Vector2D& Vector2D::operator*=(double t) {
x *= t;
y *= t;
return (*this);
}
Vector2D& Vector2D::operator/=(double t) {
double f = 1.0 / t;
x *= f;
y *= f;
return (*this);
}
Vector2D& Vector2D::operator&=(const Vector2D& v) {
x *= v.x;
y *= v.y;
return (*this);
}
Vector2D Vector2D::operator-(void) const {
return (Vector2D(-x, -y));
}
Vector2D Vector2D::operator+(const Vector2D& v) const {
return (Vector2D(x + v.x, y + v.y));
}
Vector2D Vector2D::operator-(const Vector2D& v) const {
return (Vector2D(x - v.x, y - v.y));
}
Vector2D Vector2D::operator*(double t) const {
return (Vector2D(x * t, y * t));
}
Vector2D Vector2D::operator/(double t) const {
double f = 1.0 / t;
return (Vector2D(x * f, y * f));
}
bool Vector2D::operator==(const Vector2D& v) const {
return ((x == v.x) && (y == v.y));
}
bool Vector2D::operator!=(const Vector2D& v) const {
return ((x != v.x) || (y != v.y));
}
Vector2D& Vector2D::Rotate(double angle) {
double s = sin(angle);
double c = cos(angle);
double nx = c * x - s * y;
double ny = s * x + c * y;
x = nx;
y = ny;
return (*this);
}
std::ostream& operator<<(std::ostream& os, const Vector2D& v) {
os << "(" << v.x << ", " << v.y << ")";
return os;
}
std::istream& operator>>(std::istream& is, Vector2D& v) {
double tX, tY;
is >> tX >> tY;
v.SetXY(tX, tY);
return is;
}