How to check if a point is inside a rectangle in C

1 Answer

0 votes
#include <stdio.h>

typedef struct {
    double x, y;
} Point;

typedef struct {
    Point topLeft;
    Point bottomRight;
} Rectangle;

int isPointInsideRectangle(Point p, Rectangle rect) {
    return (p.x >= rect.topLeft.x && p.x <= rect.bottomRight.x &&
            p.y >= rect.topLeft.y && p.y <= rect.bottomRight.y);
}

int main() {
    Rectangle rect = {{0.0, 0.0}, {7.0, 7.0}};
    Point p = {3.0, 2.0};

    if (isPointInsideRectangle(p, rect)) {
        printf("The point is inside the rectangle.\n");
    } else {
        printf("The point is outside the rectangle.\n");
    }

    return 0;
}


   
/*
run:
   
The point is inside the rectangle.
 
*/

 



answered Jun 21, 2025 by avibootz

Related questions

...