-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
86 lines (68 loc) · 2.18 KB
/
utils.cpp
File metadata and controls
86 lines (68 loc) · 2.18 KB
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
#include "utils.hpp"
bool isInt(std::string s) {
for(int i = 0; i < int(s.size()); ++i)
if (!(s[i]>='0' && s[i]<='9'))
return false;
return true;
}
int myStoi(std::string s) {
int x=0;
for (int i = 0; i < int(s.size()); ++i)
x = 10*x+(s[i]-'0');
return x;
}
bool myStob(std::string s) {
return s != "0";
}
float difference(float a, float b){
float c = a-b;
if(c > 0) return c;
else return c*-1;
}
float getAngle(const sf::Vector2f &orig,const sf::Vector2f &des) {
return std::atan2(des.y - orig.y, des.x - orig.x)*180/(M_PI);
}
float getModule(const sf::Vector2f &orig, const sf::Vector2f &des) {
return std::sqrt(std::pow(std::abs(des.x-orig.x), 2) + std::pow(std::abs(des.y-orig.y), 2));
}
bool isCollisioning(sf::Vector2f point1, float radius1, sf::Vector2f point2, float radius2) {
return (getModule(point1,point2) < radius1+radius2);
}
/*
* bool RectangleColision(Rectangle r1, Rectangle r2) {
if (r1.lowerLeft.x < r2.lowerLeft.x + r2.width &&
r1.lowerLeft.x + r1.width > r2.lowerLeft.x &&
r1.lowerLeft.y < r2.lowerLeft.y + r2.height &&
r1.lowerLeft.y + r1.height > r2.lowerLeft.y)
return true ;
else
return false ;
}
bool colisionCircleRectangle(Circle c, Rectangle r) {
float closestX = c.center.x;
float closestY = c.center.y;
if (c.center.x < r.lowerLeft.x) {
closestX = r.lowerLeft.x;
}
else if (c.center.x > r.lowerLeft.x + r.width) {
closestX = r.lowerLeft.x + r.width;
}
if (c.center.y < r.lowerLeft.y) {
closestY = r.lowerLeft.y;
}
else if (c.center.y > r.lowerLeft.y + r.height) {
closestY = r.lowerLeft.y + r.height;
}
return c.center.distSquared(closestX, closestY) < c.radius * c.radius;
}
*/
sf::Vector2f getRelativeCenter(sf::Vector2f origen, sf::IntRect boundOr, sf::IntRect boundDest) {
sf::Vector2f final;
final.x = boundOr.left + boundOr.width/2 - boundDest.width/2 - boundDest.left + origen.x;
final.y = boundOr.top + boundOr.height/2 - boundDest.height/2 - boundDest.top + origen.y;
return final;
}
float normalize(float f) {
if (f == 0) return 0;
return f/std::abs(f);
}