-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
117 lines (107 loc) · 2.68 KB
/
utils.cpp
File metadata and controls
117 lines (107 loc) · 2.68 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
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
#include "stdafx.h"
#include "utils.h"
namespace HDY_UTIL
{
float GetDistance(float x1, float y1, float x2, float y2)
{
float x = x2 - x1;
float y = y2 - y1;
return sqrtf(x * x + y * y);
}
template <typename T>
T GetDistance(T a, T b) {
return a - b;
}
float GetAngle(float x1, float y1, float x2, float y2)
{
float x = x2 - x1;
float y = y2 - y1;
float distance = sqrtf(x * x + y * y);
//코싸인의 역치역을 구하자 이부분이 코싸인
float angle = acosf(x / distance);
//대신에 Y축 보정을 해주자
if (y2 > y1)
{
angle = PI2 - angle;
if (angle >= PI2) angle -= PI2;
}
return angle;
}
int GetCenterX(RECT rc)
{
return GetCenter(rc.left, rc.right);
}
int GetCenterY(RECT rc)
{
return GetCenter(rc.top, rc.bottom);
}
POINT GetCenter(RECT rc)
{
return { GetCenter(rc.left, rc.right) , GetCenter(rc.top , rc.bottom) };
}
POINTF GetCenterF(RECT rc)
{
return { (float)GetCenter(rc.left, rc.right) , (float)GetCenter(rc.top, rc.bottom) };
}
int GetWidth(RECT rc)
{
return GetDistance(rc.right, rc.left);
}
int GetHeight(RECT rc)
{
return GetDistance(rc.bottom, rc.top);
}
int GetRadius(ELLIPSE ep)
{
return GetDistance(ep.right, ep.left) / 2;
}
POINTF GetCollisionPoint(RECT rc1, RECT rc2)
{
float angle;
POINTF center1 = GetCenterF(rc1);
POINTF center2 = GetCenterF(rc2);
angle = GetAngle(center1.x, center1.y, center2.x, center2.y);
return { center1.x + cosf(angle) * (float)GetRadius(rc1), center1.y - sinf(angle) * (float)GetRadius(rc1) };
}
POINTF MoveTowardTo(float x1, float y1, float x2, float y2, float speed, int limit)
{
float angle = GetAngle(x1, y1, x2, y2);
float nextX, nextY;
float distance;
distance = GetDistance(x1, y1, x2, y2);
float remain = distance - limit;
if (remain > speed) {
nextX = x1 + cosf(angle) * speed;
nextY = y1 - sinf(angle) * speed;
}
else {
nextX = x1 + cosf(angle) * remain;
nextY = y1 - sinf(angle) * remain;
}
return { nextX, nextY };
}
POINTF MoveTowardTo(RECT& rc1, RECT& rc2, float speed, int limit)
{
POINTF center1 = GetCenterF(rc1);
POINTF center2 = GetCenterF(rc2);
float angle = GetAngle(center1.x, center1.y, center2.x, center2.y);
float distance = GetDistance(center1.x, center1.y, center2.x, center2.y);
float nextX, nextY;
float remain = distance - limit;
if (remain > speed) {
nextX = center1.x + cosf(angle) * speed;
nextY = center1.y - sinf(angle) * speed;
}
else {
nextX = center1.x + cosf(angle) * remain;
nextY = center1.y - sinf(angle) * remain;
}
rc1 = RectMakeCenter(nextX, nextY, GetWidth(rc1), GetHeight(rc1));
return { nextX, nextY };
}
template<typename T>
T GetCenter(T a, T b)
{
return (a + b) / 2;
}
}