-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaths.cpp
70 lines (57 loc) · 1.69 KB
/
maths.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
#include <cmath>
#include <math.h>
#include <vector>
#include "maths.hpp"
double getAngle(double cosinus, double signSinus) {
// Returns angle in radians from its cosinus and sign of its sinus.
double angle = acos(cosinus);
angle *= signSinus > 0 ? 1 : -1;
return angle;
}
double algDistPeriod(double const& x1, double const& x2, double const& length) {
// Returns algebraic distance from `x1' to `x2' on a line of length `L' taking
// into account periodic boundary condition.
double diff = x2 - x1;
if ( fabs(diff) > length/2 ) {
double diff1 = fabs(x1) + fabs(length - x2);
double diff2 = fabs(length - x1) + fabs(x2);
if ( diff1 < diff2 ) { diff = diff1; }
else { diff = diff2; }
diff *= (x2 > x1 ? -1 : 1);
}
return diff;
}
double dist2DPeriod(double* pos0, double* pos1, double const& length) {
// Returns distance between points on a plane, with positions `pos0' and
// `pos1' taking into account period boundary condition in a square system
// of size `length'.
return sqrt(
pow(
algDistPeriod( // separation in x position
pos0[0],
pos1[0],
length),
2)
+ pow(
algDistPeriod( // separation in y position
pos0[1],
pos1[1],
length),
2));
}
//wp: transpose of 2D vectors
void transpose(std::vector<std::vector<double> > &b)
{
if (b.size() == 0)
return;
std::vector<std::vector<double> > trans_vec(b[0].size(), std::vector<double>());
for (int i = 0; i < b.size(); i++)
{
for (int j = 0; j < b[i].size(); j++)
{
trans_vec[j].push_back(b[i][j]);
}
}
b = trans_vec; //wp: reassigns
return;
}