14 #define SIZE MAX_ALLOWED_DIFF_LENGTH
17 #define M_PI_2 1.57079632679489661923
18 #define M_PI_4 0.78539816339744830962
27 gint pointSorter(gconstpointer a, gconstpointer b) {
28 unsigned int aTime = ((
const point *) a)->time;
29 unsigned int bTime = ((
const point *) b)->time;
37 void circleVisit(
unsigned int timeOfVisit[SIZE][SIZE]) {
38 unsigned int time = 0;
40 for (
double r = 0; r < SIZE; r += 0.5)
41 for (
double theta = 0; theta < M_PI_2; theta += M_PI_4 / (SIZE)) {
43 int x = floor(r * sin(theta));
44 int y = floor(r * cos(theta));
45 if ((x < SIZE) && (x >= 0) && (y < SIZE) && (y >= 0))
46 timeOfVisit[x][y] = time;
50 GArray* generateTimeOrderedVisitor(
unsigned int timeOfVisit[SIZE][SIZE]) {
51 GArray* visitor = g_array_new(TRUE, FALSE,
sizeof(
point));
52 for (
unsigned int i = 0; i < SIZE; i++)
53 for (
unsigned int j = 0; j < SIZE; j++) {
57 p.time = timeOfVisit[i][j];
58 if ((p.time > 0) && (i + j > 0))
59 g_array_append_val(visitor, p);
62 g_array_sort(visitor, pointSorter);
66 int writeVisitorToSourceFiles(GArray* visitor) {
67 FILE* fc = fopen(
"_squareVisitor.c",
"w");
73 fprintf(fc,
"unsigned int squareVisitorX[] = ");
75 fprintf(fc,
"{%u", p0.x);
76 for (
size_t i = 1; i < visitor->len; i++) {
78 fprintf(fc,
",\n\t%u", p.x);
82 fprintf(fc,
"unsigned int squareVisitorY[] = ");
83 fprintf(fc,
"{%u", p0.y);
84 for (
size_t i = 1; i < visitor->len; i++) {
86 fprintf(fc,
",\n\t%u", p.y);
92 FILE* fh = fopen(
"_squareVisitor.h.gen",
"w");
98 fprintf(fh,
"#define SQUARE_VISITOR_LENGTH %u\n", visitor->len);
105 unsigned int timeOfVisit[SIZE][SIZE];
107 for (
int i = 0; i < SIZE; i++)
108 for (
int j = 0; j < SIZE; j++)
109 timeOfVisit[i][j] = 0;
111 circleVisit(timeOfVisit);
113 #ifdef SQUARE_BUILDER_DEBUG
114 printf(
"time of visit:\n");
115 for (
int i = 0; i < SIZE; i++) {
116 for (
int j = 0; j < SIZE; j++)
117 printf(
"%u ", timeOfVisit[i][j]);
121 printf(
"visited:\n");
122 for (
int i = 0; i < SIZE; i++) {
123 for (
int j = 0; j < SIZE; j++)
124 if (timeOfVisit[i][j] > 0)
132 GArray* visitor = generateTimeOrderedVisitor(timeOfVisit);
134 if (visitor->len == 0)
137 #ifdef SQUARE_BUILDER_DEBUG
138 printf(
"sorted visitor is:\n");
139 for (
size_t i = 0; i < visitor->len; i++) {
141 printf(
"[%u]:{%u,%u}, ", p.time, p.x, p.y);
146 return writeVisitorToSourceFiles(visitor);