Why doesn't my code print from this if condition correctly?

76 views Asked by At

I am writing a program in C which currently does the following:

  • takes 4 coordinate pairs in the form (x,y) as input
  • calculates the sides formed by two pairs of points, compares the corresponding sides (and diagonals)
  • prints out that the points form a rectangle if the corresponding sides are equal

What's not working is, when the order of the coordinates is shuffled, the program is supposed to print out that the points do not form a rectangle.

So for example if the points are (0,0) (20,0) (0,50) (20,50), it's supposed to print that the points do not form a rectangle, since it jumps from (20,0) to (0,50). However, the condition I have written to check this doesn't work apparently (see the commented line). What can I replace this condition with to successfully operate?

#include <stdio.h>
#include <math.h>

int main() {
    int x[4];
    int y[4];

    for (int i = 0; i < 4; i++) {
        printf("Point #%d:\n", i + 1);

        if (scanf(" (%d,%d)", &x[i], &y[i]) != 2) {
            printf("Invalid input.\n");
            return 1;
        }

      
        if (!(-100000 <= x[i] && x[i] <= 100000 && -100000 <= y[i] && y[i] <= 100000))
        {
            printf("Invalid input.\n");
            return 1;
        }
    }

    int x1 = x[0];
    int x2 = x[1];
    int x3 = x[2];
    int x4 = x[3];

    int y1 = y[0];
    int y2 = y[1];
    int y3 = y[2];
    int y4 = y[3];

    int isRectangle = 0;

    //the condition
    if ((x1 == y4 && y1 == x4) && (x2 == y3 && y2 == x3)) {
        isRectangle = 1;
    } else if ((x1 == x2 && y1 == y4) && (y2 == x3 && y3 == x4)) {
        isRectangle = 1;
    } 

    if (isRectangle) {
        printf("The points form a rectangle.\n");
    } else {
        printf("The points do not form a rectangle.\n");
    }

    return 0;
}
0

There are 0 answers