-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathUtil.cpp
75 lines (59 loc) · 1.76 KB
/
MathUtil.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
#include "MathUtil.h"
#include <math.h>
namespace dxco {
float MathUtil::distance(cocos2d::CCPoint a, cocos2d::CCPoint b) {
//FIXME use CCPoint distance
float x = a.x - b.x;
float y = a.y - b.y;
x = x * x;
y = y * y;
return sqrt(x + y);
}
float MathUtil::angle(cocos2d::CCPoint center,
cocos2d::CCPoint pointA) {
float result = 0;
if (pointA.y > center.y) {
// cuadrante 1
if (pointA.x > center.x) {
double adyacente = pointA.x - center.x;
double hipotenusa = distance(pointA, center);
result = acos(adyacente / hipotenusa);
} else { // cuadrante 2
double adyacente = pointA.y - center.y;
double hipotenusa = distance(pointA, center);
result = acos(adyacente / hipotenusa) + (PI * 0.5);
}
} else {
// cuadrante 3
if (pointA.x < center.x) {
double adyacente = center.x - pointA.x;
double hipotenusa = distance(pointA, center);
result = acos(adyacente / hipotenusa) + PI;
} else { // cuadrante 4
double adyacente = center.y - pointA.y;
double hipotenusa = distance(pointA, center);
result = acos(adyacente / hipotenusa) + (PI * 1.5);
}
}
return result;
}
float MathUtil::angle(cocos2d::CCPoint vector) {
float angle = atan(vector.y / vector.x);
if(vector.x > 0 && vector.y > 0) { //quad 1
return angle;
} else if (vector.x < 0) { //quad 2 and 3
return angle + PI;
} else if (vector.y < 0) { //quad 4
return angle + 2*PI;
}
}
float MathUtil::max(float value1, float value2) {
return (value1 > value2) ? value1 : value2;
}
cocos2d::CCPoint MathUtil::scalarProd(cocos2d::CCPoint vector, float scale) {
return cocos2d::CCPoint(scale * vector.x, scale * vector.y);
}
cocos2d::CCPoint MathUtil::scaleVector(cocos2d::CCPoint vector, float scale) {
return scalarProd(vector.normalize(), scale);
}
} /* namespace dxco */