-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymath.cpp
50 lines (39 loc) · 1.03 KB
/
mymath.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
#include "mymath.h"
bool isEqual(float first, float second, float eps)
{
return qAbs(first - second) <= eps;
}
bool isEqual(QPointF first, QPointF second, float eps)
{
auto dist = vectorLength(first - second);
return (dist < eps);
}
float angleBetweenVectors(QPointF first, QPointF second)
{
auto angleDelta = vectorAngle(first) - vectorAngle(second);
if (angleDelta >= PI) {
angleDelta -= 2 * PI;
}
if (angleDelta <= -PI) {
angleDelta += 2 * PI;
}
return angleDelta;
}
float vectorLength(QPointF vector)
{
return qSqrt(qPow(vector.x(), 2) + qPow(vector.y(), 2));
}
float vectorAngle(QPointF vector)
{
auto angle = qAtan2(vector.y(), vector.x());
return angle;
}
QPointF rotateVector(QPointF vector, float angle)
{
return {vector.x() * qCos(angle) - vector.y() * qSin(angle),
vector.x() * qSin(angle) + vector.y() * qCos(angle)};
}
float scalarMultiplicationVectors(QPointF first, QPointF second)
{
return first.x() * second.x() + first.y() * second.y();
}