-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorOperations.cpp
59 lines (53 loc) · 1.54 KB
/
VectorOperations.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
#include "VectorOperations.h"
Vector2f VectorOperations::ReductComponentsToOne(Vector2f vector)
{
Vector2f direction = vector;
if (direction.x == 0 && direction.y == 0)
return (Vector2f(0, 0));
else
{
float maxComp = std::max<float>(abs(direction.x), abs(direction.y));
direction.x = direction.x / maxComp;
direction.y = direction.y / maxComp;
return direction;
}
}
Vector2f VectorOperations::ReductToLength(Vector2f vector, float length)
{
Vector2f direction = vector;
float divSum = GetLength(direction);
divSum = length / divSum;
direction.x = direction.x * divSum;
direction.y = direction.y * divSum;
return direction;
}
float VectorOperations::VectorToRotation(Vector2f direction)
{
float scalar = direction.x*originDirection.x + direction.y*originDirection.y;
float lengths = GetLength(originDirection)*GetLength(direction);
float angle = acos(scalar / lengths)*180/(3.14);
if (direction.y < 0)
angle += (180 - angle) * 2;
return angle;
}
float VectorOperations::GetLength(Vector2f vector)
{
float length = sqrt(abs(vector.x)*abs(vector.x) + abs(vector.y)*abs(vector.y));
return length;
}
float VectorOperations::AngleBetweenVectors(Vector2f f, Vector2f s)
{
float x1 = f.x;
float y1 = f.y;
float x2 = s.x;
float y2 = s.y;
float t = (x1*x2 + y1*y2) / (sqrt((double)x1*x1 + y1*y1)*sqrt((double)x2*x2 + y2*y2));
if (t<-1) t = -1;
else if (t > 1) t = 1;
return acos(t) * 180 / (3.14);
}
float VectorOperations::Distance(Vector2f f, Vector2f s)
{
float dist = sqrt((f.x - s.x)*(f.x - s.x) + (f.y - s.y)*(f.y - s.y));
return dist;
}