Well you've managed to ctrl-c/ctrl-v your way into a hole, without studying the problem.
Careful observation would note the following.
- a cycle of right, up, left, down
- right and up are of length N, and left and down are of length N+1. The pattern repeats again at N+2.
Code:
#include <stdio.h>
typedef struct coord {
int x, y;
} coord;
coord createSide(coord start, coord direction, int steps, int *num) {
for ( int i = 0 ; i < steps ; i++ ) {
start.x += direction.x;
start.y += direction.y;
printf("%d coord=%d %d\n", *num, start.x, start.y);
(*num)++;
}
return start;
}
int main() {
coord directions[] = {
{ 1, 0 }, // right
{ 0, 1 }, // up
{ -1, 0 }, // left
{ 0, -1 }, // down
};
coord start = { 0, 0 };
int num = 1;
for ( int side = 1 ; side <= 9 ; side += 2 ) {
start = createSide(start,directions[0],side,&num);
start = createSide(start,directions[1],side,&num);
start = createSide(start,directions[2],side+1,&num);
start = createSide(start,directions[3],side+1,&num);
}
}