25 #define SIZE MAX_ALLOWED_DIFF_LENGTH 28 #define M_PI_2 1.57079632679489661923 29 #define M_PI_4 0.78539816339744830962 38 gint pointSorter(gconstpointer a, gconstpointer b) {
39 unsigned int aTime = ((
const point *) a)->time;
40 unsigned int bTime = ((
const point *) b)->time;
48 void circleVisit(
unsigned int timeOfVisit[SIZE][SIZE]) {
49 unsigned int time = 0;
51 for (
double r = 0; r < SIZE; r += 0.5)
52 for (
double theta = 0; theta < M_PI_2; theta += M_PI_4 / (SIZE)) {
54 int x = floor(r * sin(theta));
55 int y = floor(r * cos(theta));
56 if ((x < SIZE) && (x >= 0) && (y < SIZE) && (y >= 0))
57 timeOfVisit[x][y] = time;
61 GArray* generateTimeOrderedVisitor(
unsigned int timeOfVisit[SIZE][SIZE]) {
62 GArray* visitor = g_array_new(TRUE, FALSE,
sizeof(
point));
63 for (
unsigned int i = 0; i < SIZE; i++)
64 for (
unsigned int j = 0; j < SIZE; j++) {
68 p.time = timeOfVisit[i][j];
69 if ((p.time > 0) && (i + j > 0))
70 g_array_append_val(visitor, p);
73 g_array_sort(visitor, pointSorter);
77 int writeVisitorToSourceFiles(GArray* visitor) {
78 FILE* fc = fopen(
"_squareVisitor.c",
"w");
84 fprintf(fc,
"unsigned int squareVisitorX[] = ");
86 fprintf(fc,
"{%u", p0.x);
87 for (
size_t i = 1; i < visitor->len; i++) {
89 fprintf(fc,
",\n\t%u", p.x);
93 fprintf(fc,
"unsigned int squareVisitorY[] = ");
94 fprintf(fc,
"{%u", p0.y);
95 for (
size_t i = 1; i < visitor->len; i++) {
97 fprintf(fc,
",\n\t%u", p.y);
103 FILE* fh = fopen(
"_squareVisitor.h.gen",
"w");
109 fprintf(fh,
"#define SQUARE_VISITOR_LENGTH %u\n", visitor->len);
116 unsigned int timeOfVisit[SIZE][SIZE];
118 for (
int i = 0; i < SIZE; i++)
119 for (
int j = 0; j < SIZE; j++)
120 timeOfVisit[i][j] = 0;
122 circleVisit(timeOfVisit);
124 #ifdef SQUARE_BUILDER_DEBUG 125 printf(
"time of visit:\n");
126 for (
int i = 0; i < SIZE; i++) {
127 for (
int j = 0; j < SIZE; j++)
128 printf(
"%u ", timeOfVisit[i][j]);
132 printf(
"visited:\n");
133 for (
int i = 0; i < SIZE; i++) {
134 for (
int j = 0; j < SIZE; j++)
135 if (timeOfVisit[i][j] > 0)
141 #endif //SQUARE_BUILDER_DEBUG 143 GArray* visitor = generateTimeOrderedVisitor(timeOfVisit);
145 if (visitor->len == 0)
148 #ifdef SQUARE_BUILDER_DEBUG 149 printf(
"sorted visitor is:\n");
150 for (
size_t i = 0; i < visitor->len; i++) {
152 printf(
"[%u]:{%u,%u}, ", p.time, p.x, p.y);
155 #endif //SQUARE_BUILDER_DEBUG 157 return writeVisitorToSourceFiles(visitor);