-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathVector.cc
More file actions
83 lines (68 loc) · 1.89 KB
/
MathVector.cc
File metadata and controls
83 lines (68 loc) · 1.89 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
#include "MathVector.h"
#include <cmath>
#include <cstring>
MathVector::MathVector() {
x = 0;
y = 0;
lenght = 0;
}
MathVector::MathVector(Point a, Point b) {
x = b.x - a.x;
y = b.y - a.y;
lenght = sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
}
MathVector MathVector::operator + (MathVector vector) {
vector.x += this->x;
vector.y += this->y;
return vector;
}
Point MathVector::operator + (Point point) {
point.x += this->x;
point.y += this->y;
return point;
}
double MathVector::operator * (MathVector v){
return this->x * v.x + this->y * v.y;
}
//TODO change to template
MathVector MathVector::operator * (double scale) {
MathVector result;
result.x = this->x * scale;
result.y = this->y * scale;
return result;
}
MathVector MathVector::operator / (double scale) {
MathVector result;
result.x = this->x / scale;
result.y = this->y / scale;
return result;
}
double MathVector::GetAngleToOrtoi() {
MathVector orti{ Point{0,0}, Point{1,0} };
return this->GetAngleBetweenVectors(orti);
}
double MathVector::GetAngleBetweenVectors(MathVector vec)
{
if (vec.lenght == 0 || this->lenght == 0)
return 0;
double t = (x * vec.x + y * vec.y) / (sqrt((double)pow(x, 2) + pow(y, 2)) * sqrt((double)pow(vec.x, 2) + pow(vec.y, 2)));
if (t < -1) t = -1;
else if (t > 1) t = 1;
double r = acos(t) * 180 / M_PI;
if (y < vec.y)
return r;
else
return 360 - r;
}
double MathVector::getAngle(MathVector vec){
return acos (*this * vec /(this->lenght* vec.lenght) );
}
MathVector MathVector::normilizeVec() {
double norm = this->lenght;
if (norm == 0) {
//cout << "The norm of the vector is 0" << endl;
return MathVector();
}
MathVector result = *this / norm;
return result;
}