-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
126 lines (108 loc) · 2.78 KB
/
Utils.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
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
117
118
119
120
121
122
123
124
125
126
#include "Utils.h"
#include <iostream>
float DegToRad(float deg)
{
return deg / 180.0 * M_PI;
}
vecf vec_P_Sub(vecf *vec, size_t id)
{
vecf result;
for (size_t i = 0; i < vec->size(); i++)
result.push_back((*vec)[i] - (id == i ? 1.f : 0.f));
return result;
}
sf::Vector2f rotateVec(sf::Vector2f vec, int degree)
{
sf::Vector2f result(0,0);
float rad = degree / 180.0 * M_PI;
result.x = vec.x * cos(rad) - vec.y * sin(rad);
result.y = vec.x * sin(rad) + vec.y * cos(rad);
return result;
}
sf::Vector2f randVec(int len)
{
sf::Vector2f r = sf::Vector2f(rand() % 2 * len - len, 0);
r.y = sqrt(len * len - r.x * r.x);
r.y *= rand() % 2 == 0 ? -1 : 1;
return r;
}
float vecLen(sf::Vector2f vec)
{
return sqrt(vec.x * vec.x + vec.y * vec.y);
}
float vecLen(sf::Vector2i vec)
{
return sqrt(vec.x * vec.x + vec.y * vec.y);
}
float crossProd(sf::Vector2f vecA, sf::Vector2f vecB)
{
return (vecA.x * vecB.y) - (vecA.y * vecB.x);
}
float dotProd(sf::Vector2f vecA, sf::Vector2f vecB)
{
return vecA.x * vecB.x + vecA.y * vecB.y;
}
/*********************************************************************************************
** VECTOR UTILITY ROUTINES ******************************************************************
*********************************************************************************************/
size_t vec_EquDimens(vecf *vecA, vecf *vecB)
{
size_t len = vecA-> size();
return len == vecB-> size() ? len : 0;
}
vecf vec_Sub(vecf *vecA, vecf vecB)
{
return vec_Sub(vecA, &vecB);
}
vecf vec_Add(vecf *vecA, vecf vecB)
{
return vec_Add(vecA, &vecB);
}
vecf vec_Sub(vecf *vecA, vecf *vecB)
{
size_t len = vec_EquDimens(vecA, vecB);
if (!len) {
std::cerr << "SUB: Arguments don’t have the same dimensions" << std::endl;
return vecf();
}
vecf result = vecf();
for (size_t i = 0; i < len; ++i) {
result.push_back( (*vecA)[i] - (*vecB)[i] );
}
return result;
}
vecf vec_Add(vecf *vecA, vecf *vecB)
{
size_t len = vec_EquDimens(vecA, vecB);
if (!len) {
std::cerr << "ADD: Arguments don’t have the same dimensions" << std::endl;
return vecf();
}
vecf result = vecf();
for (size_t i = 0; i < len; ++i) {
result.push_back( (*vecA)[i] + (*vecB)[i] );
}
return result;
}
vecf vec_Mul(vecf vecA, float fac)
{
return vec_Mul(&vecA, fac);
}
vecf vec_Mul(vecf *vecA, float fac)
{
size_t len = vecA-> size();
vecf result = vecf();
for (size_t i = 0; i < len; ++i) {
result.push_back((*vecA)[i] * fac);
}
return result;
}
float vec_Len(vecf *vecA)
{
size_t len = vecA-> size();
float sum = 0.0f;
for (size_t i = 0; i < len; ++i) {
sum += (*vecA)[i] * (*vecA)[i];
}
return sqrt(sum);
}