-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.cpp
executable file
·81 lines (64 loc) · 1.29 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
#include "vector.h"
#include <sstream>
vec2::vec2(double x_init, double y_init) {
x = x_init;
y = y_init;
}
vec2::vec2() {};
vec2 vec2::scale(double s) {
vec2 temp(x * s, y * s);
return temp;
}
vec2 vec2::normalize() {
return scale(1/magnitude());
}
vec2 vec2::project(vec2 b) {
return b.scale(dot(b)/b.magsquared());
}
vec2 vec2::rightnorm() {
vec2 temp(-y,x); //should be -x in a normal coord system, but y is pointed down
return temp;
}
vec2 vec2::leftnorm() {
vec2 temp(y,-x);
return temp;
}
vec2 vec2::rotate(double radians) {
//double radians = degrees * (M_PI/180.0f);
vec2 temp(x*cos(radians) - y*sin(radians), x*sin(radians) + y*cos(radians));
return temp;
}
double vec2::magnitude() {
return sqrt(magsquared());
}
double vec2::magsquared() {
return dot(*this);
}
double vec2::dot(vec2 b) {
return x*b.x + y*b.y;
}
vec2 vec2::operator+(vec2 b) {
vec2 temp(x + b.x, y+b.y);
return temp;
}
vec2 vec2::operator-(vec2 b) {
vec2 temp(x - b.x, y - b.y);
return temp;
}
vec2 vec2::operator-() {
vec2 temp(-x,-y);
return temp;
}
vec2 vec2::operator=(vec2 b) {
x = b.x;
y = b.y;
return *this;
}
bool vec2::operator==(vec2 b) {
return x == b.x && y == b.y;
}
std::string vec2::text() {
std::stringstream s;
s << "(" << x << "," << y << ")";
return s.str();
}