/
SixEdgeMath.cpp
executable file
·153 lines (122 loc) · 2.92 KB
/
SixEdgeMath.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "SixEdgeMath.h"
#include <memory.h>
resSixEdge::resSixEdge()
{
memset(m_fPoint, 0, sizeof(m_fPoint));
}
CLPoint2D evenAdjacentOffset[6] =
{
{ -1, 0, true },
{ -1, -1, false },
{ 0, -1, false },
{ 1, 0, true },
{ -1, 1, false },
{ 0, 1, false }
};
CLPoint2D oddAdjacentOffset[6] =
{
{ -1, 0, true },
{ 0, -1, false },
{ 1, -1, false },
{ 1, 0, true },
{ 1, 1, false },
{ 0, 1, false }
};
resSixEdge CSixEdgeMath::GetPointByLogicPos(int x, int y, bool isOdd)
{
//ÇřˇÖÁůąßĐÎľŘÍźľÄĆćĹź
float off_y = y * 3 * m_fEdgeLength;
float off_x = x * SQRT3 * m_fEdgeLength;
if (!isOdd)
{
off_y += m_fEdgeLength * 1.5;
off_x -= 0.5 * SQRT3 * m_fEdgeLength;
}
resSixEdge res;
CPoint2D off_point(off_x, off_y);
CPoint2D cur_point;
for (int i = 0; i < 6; i++)
{
cur_point = m_MetaSixEdge.m_point[i];
cur_point *= m_fEdgeLength;
cur_point += off_point;
res.m_fPoint[i * 2] = cur_point.x;
res.m_fPoint[i * 2 + 1] = cur_point.y;
}
return res;
}
CPoint2D CSixEdgeMath::GetPoint(CLPoint2D& p, bool isEven)
{
float off_y = p.y * 3 * m_fEdgeLength;
float off_x = p.x * SQRT3 * m_fEdgeLength;
if (!isEven)
{
off_y += m_fEdgeLength * 1.5;
off_x += 0.5 * SQRT3 * m_fEdgeLength;
}
CPoint2D rp(off_x, off_y);
return rp;
}
void CSixEdgeMath::GetAdjacentCell(CLPoint2D& p, CLPoint2D res_point[])
{
if (p.e)
{
for (int i = 0; i < 6; i++)
res_point[i] = p + evenAdjacentOffset[i];
}
else
{
for (int i = 0; i < 6; i++)
res_point[i] = p + oddAdjacentOffset[i];
}
}
CSixEdgeMath::CSixEdgeMath()
{
m_fEdgeLength = 20.0f;
}
CSixEdgeMath::~CSixEdgeMath()
{
}
CMetaSixEdge::CMetaSixEdge()
{
m_point[0] = CPoint2D(-SQRT3*0.5f, -0.5f);
m_point[1] = CPoint2D(-SQRT3*0.5f, 0.5f);
m_point[2] = CPoint2D(0.0f, 1.0f);
m_point[3] = CPoint2D(SQRT3*0.5f, 0.5f);
m_point[4] = CPoint2D(SQRT3*0.5f, -0.5f);
m_point[5] = CPoint2D(0.0f, -1.0f);
}
CMetaSixEdge::~CMetaSixEdge()
{
}
CSixEdgeMath* GetSixEdgeMath()
{
static CSixEdgeMath* g_pSixEdgeMath = new CSixEdgeMath;
return g_pSixEdgeMath;
}
void CSixEdgeMath::GetCell(int sx, int sy, CLPoint2D& resLogicPos, CPoint2D& resPos)
{
float f_xtimes = sx / (SQRT3 * m_fEdgeLength);
int ox = (int)f_xtimes;
int ex = (int)(f_xtimes + 0.5f);
float f_ytimes = sy / (3 * m_fEdgeLength);
int oy = (int)f_ytimes;
int ey = (int)(f_ytimes + 0.5);
CLPoint2D o_lpoint(ox, oy, false);
CLPoint2D e_lpoint(ex, ey, true);
CPoint2D o_point = GetPoint(o_lpoint, false);
CPoint2D e_point = GetPoint(e_lpoint, true);
CPoint2D scenePoint(sx, sy);
float d_o = o_point.Distance(scenePoint);
float d_e = e_point.Distance(scenePoint);
if (d_o < d_e)
{
resLogicPos = o_lpoint;
resPos = o_point;
}
else
{
resLogicPos = e_lpoint;
resPos = e_point;
}
}