-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswVector.cpp
81 lines (63 loc) · 1.61 KB
/
swVector.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
#include <cmath>
#include "swVector.h"
swVector::swVector(double a, double b) : x(a), y(b) {
type = SW_VECTOR;
}
swVector::swVector(const swVector &src) : x(src.x), y(src.y) {
type = SW_VECTOR;
}
swVector swVector::normalize() {
double len = length();
return *this / len;
}
swVector swVector::rotate(double angle) {
return swVector(x * cos(angle) - y * sin(angle), x * sin(angle) + y * cos(angle));
}
double swVector::length() {
return sqrt(x * x + y * y);
}
double swVector::angle() {
return atan2(y, x);
}
swVector swVector::scale(swVector a) {
return swVector(x * a.x, y * a.y);
}
swVector swVector::operator+(swVector a) {
return swVector(x + a.x, y + a.y);
}
swVector swVector::operator-(swVector a) {
return swVector(x - a.x, y - a.y);
}
double swVector::operator*(swVector a) {
return x * a.x + y * a.y;
}
swVector swVector::operator*(double a) {
return swVector(x * a, y * a);
}
swVector swVector::operator/(double a) {
return swVector(x / a, y / a);
}
swVector& swVector::operator+=(swVector a) {
x += a.x;
y += a.y;
return *this;
}
swVector& swVector::operator-=(swVector a) {
x -= a.x;
y -= a.y;
return *this;
}
bool swVector::operator==(swVector a) {
return (x == a.x && y == a.y);
}
bool swVector::operator!=(swVector a) {
return (x != a.x || y != a.y);
}
void swVector::read(swStream* stream) {
stream->readDouble(x);
stream->readDouble(y);
}
void swVector::write(swStream* stream) {
stream->writeDouble(x);
stream->writeDouble(y);
}