I am trying to create a program in C to check if the lines of rectilinear polygon intersect each other at any point.
I only need simple rectilinear polygons that do not intersect at any point. It can be counter-clockwise or clockwise.
The direction values will be less than 10. NS direction must alternate with WE direction and vice versa.
The inputs being passed are in the form of direction from an input file such as; and also shown in the pictures:
S 2 E 4 S 2 E 4 N 2 W 4 N 2 W 4

I tried to store points in a two-dimensional array with every point being checked as true, but I am unable to figure out how to move in case of counter and clockwise direction as the points could be N4 E6 or S4 W6. In such a case if I add when the value is N-4 (x,y) = (0,4) and subtract when S-4 (x,y) = (0,-4) that would fail when using it as index in array.
int arr[10][10];
int xPrime = 0, yPrime = 0;
bool checkContinuity(int y, const char * dir ){
if(strcmp(dir, "S")==0){
y = -y;
cols = y;
int j;
for(j = cols; j >= 0; j--){
if(arr[xPrime][j] == 1 && j != yPrime){
return false;
}
arr[xPrime][j] = 1;
printf(" %d ", j);
}
yPrime -= y;
if(yPrime < 0)
yPrime = -yPrime;
}
else if(strcmp(dir, "W")==0){
y = -y;
cols = y;
int j;
for(j = cols; j >= 0; j--){
if(arr[j][yPrime] == 1 && j != xPrime && (j != 0 && yPrime != 0)){
return false;
}
arr[j][yPrime] = 1;
printf(" %d ", j);
}
xPrime -= y;
if(xPrime < 0)
xPrime = -xPrime;
}
else if(strcmp(dir, "N")==0){
cols = y;
int j;
for(j = 0; j <= cols; j++){
if(arr[xPrime][j] == 1)
return false;
arr[xPrime][j] = 1;
printf(" %d ", j);
}
yPrime += y;
}
else if(strcmp(dir, "E")==0){
cols = y;
int j;
for(j = 0; j <= cols; j++){
if(arr[j][yPrime] == 1 && j != xPrime)
return false;
arr[j][yPrime] = 1;
printf(" %d ", j);
}
xPrime += y;
}
else
return false;
return true;
}