-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilbert_curve.c
227 lines (178 loc) · 3.82 KB
/
hilbert_curve.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include "hilbert_curve.h"
/******************************************************************************/
void d2xy ( int m, int d, int *x, int *y )
{
int n;
int rx;
int ry;
int s;
int t = d;
n = i4_power ( 2, m );
*x = 0;
*y = 0;
for ( s = 1; s < n; s = s * 2 )
{
rx = 1 & ( t / 2 );
ry = 1 & ( t ^ rx );
rot ( s, x, y, rx, ry );
*x = *x + s * rx;
*y = *y + s * ry;
t = t / 4;
}
return;
}
/******************************************************************************/
int i4_power ( int i, int j )
{
int k;
int value;
if ( j < 0 )
{
if ( i == 1 )
{
value = 1;
}
else if ( i == 0 )
{
fprintf ( stderr, "\n" );
fprintf ( stderr, "I4_POWER - Fatal error!\n" );
fprintf ( stderr, " I^J requested, with I = 0 and J negative.\n" );
exit ( 1 );
}
else
{
value = 0;
}
}
else if ( j == 0 )
{
if ( i == 0 )
{
fprintf ( stderr, "\n" );
fprintf ( stderr, "I4_POWER - Fatal error!\n" );
fprintf ( stderr, " I^J requested, with I = 0 and J = 0.\n" );
exit ( 1 );
}
else
{
value = 1;
}
}
else if ( j == 1 )
{
value = i;
}
else
{
value = 1;
for ( k = 1; k <= j; k++ )
{
value = value * i;
}
}
return value;
}
/******************************************************************************/
void rot ( int n, int *x, int *y, int rx, int ry )
/******************************************************************************/
/*
Purpose:
ROT rotates and flips a quadrant appropriately.
Modified:
05 December 2015
Parameters:
Input, int N, the length of a side of the square. N must be a power of 2.
Input/output, int *X, *Y, the old and the new coordinates.
Input, int RX, RY, ???
*/
{
int t;
if ( ry == 0 )
{
/*
Reflect.
*/
if ( rx == 1 )
{
*x = n - 1 - *x;
*y = n - 1 - *y;
}
/*
Flip.
*/
t = *x;
*x = *y;
*y = t;
}
return;
}
/******************************************************************************/
void timestamp ( )
/******************************************************************************/
/*
Purpose:
TIMESTAMP prints the current YMDHMS date as a time stamp.
Example:
17 June 2014 09:45:54 AM
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
17 June 2014
Author:
John Burkardt
Parameters:
None
*/
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm *tm;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
printf ( "%s\n", time_buffer );
return;
# undef TIME_SIZE
}
/******************************************************************************/
int xy2d ( int m, int x, int y )
/******************************************************************************/
/*
Purpose:
XY2D converts a 2D Cartesian coordinate to a 1D Hilbert coordinate.
Discussion:
It is assumed that a square has been divided into an NxN array of cells,
where N is a power of 2.
Cell (0,0) is in the lower left corner, and (N-1,N-1) in the upper
right corner.
Modified:
05 December 2015
Parameters:
Input, int M, the index of the Hilbert curve.
The number of cells is N=2^M.
0 < M.
Input, int X, Y, the Cartesian coordinates of a cell.
0 <= X, Y < N.
Output, int XY2D, the Hilbert coordinate of the cell.
0 <= D < N * N.
*/
{
int d = 0;
int n;
int rx;
int ry;
int s;
n = i4_power ( 2, m );
for ( s = n / 2; s > 0; s = s / 2 )
{
rx = ( x & s ) > 0;
ry = ( y & s ) > 0;
d = d + s * s * ( ( 3 * rx ) ^ ry );
rot ( s, &x, &y, rx, ry );
}
return d;
}