-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilbertcurve.cpp
140 lines (132 loc) · 2.48 KB
/
hilbertcurve.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
#include "hilbertcurve.h"
hilbertcurve::hilbertcurve(int N)
{
this->N = N;
n = 1;
int i = N;
while (i) {
n *= 2;
i--;
}
total = n * n;
temp_table.x = temp_table.y = 0;
}
/**
* @brief 创建表格
*/
void hilbertcurve::createtable()
{
POINT begin;
begin.x = 0;
begin.y = 0;
table.push_back(begin);
}
/**
* @brief 移动一步,并记录至vector<POINT> table
* @param direction 方向
*/
void hilbertcurve::move(int direction)
{
switch (direction)
{
case UP: // 向上移动
temp_table.x = temp_table.x - 1;
break;
case DOWN: // 向下移动
temp_table.x = temp_table.x + 1;
break;
case LEFT: // 向左移动
temp_table.y = temp_table.y - 1;
break;
case RIGHT: // 向右移动
temp_table.y = temp_table.y + 1;
break;
}
table.push_back(temp_table);
tmp++;
}
/**
* @brief 递归实现
* @param level 维度
* @param direction 方向
* @note 四个格子,按上一级方向确定
*/
void hilbertcurve::make_hilbertcurve(int level, int direction)
{
if (level == 1)
{
switch (direction)
{
case LEFT:
move(RIGHT);
move(DOWN);
move(LEFT);
break;
case RIGHT:
move(LEFT);
move(UP);
move(RIGHT);
break;
case UP:
move(DOWN);
move(RIGHT);
move(UP);
break;
case DOWN:
move(UP);
move(LEFT);
move(DOWN);
break;
}
}
else
{
switch (direction) {
case LEFT:
make_hilbertcurve(level - 1, UP);
move(RIGHT);
make_hilbertcurve(level - 1, LEFT);
move(DOWN);
make_hilbertcurve(level - 1, LEFT);
move(LEFT);
make_hilbertcurve(level - 1, DOWN);
break;
case RIGHT:
make_hilbertcurve(level - 1, DOWN);
move(LEFT);
make_hilbertcurve(level - 1, RIGHT);
move(UP);
make_hilbertcurve(level - 1, RIGHT);
move(RIGHT);
make_hilbertcurve(level - 1, UP);
break;
case UP:
make_hilbertcurve(level - 1, LEFT);
move(DOWN);
make_hilbertcurve(level - 1, UP);
move(RIGHT);
make_hilbertcurve(level - 1, UP);
move(UP);
make_hilbertcurve(level - 1, RIGHT);
break;
case DOWN:
make_hilbertcurve(level - 1, RIGHT);
move(UP);
make_hilbertcurve(level - 1, DOWN);
move(LEFT);
make_hilbertcurve(level - 1, DOWN);
move(DOWN);
make_hilbertcurve(level - 1, LEFT);
break;
}
}
}
//执行生成
void hilbertcurve::run()
{
createtable();
make_hilbertcurve(n, UP);
for (int i = 0; i < total; ++i){
cout << '(' << table[i].x << ", " << table[i].y << ")\n";
}
}