Broad Network


9.2 Give an algorithm to check if two rectangles intersect, where rectangles are defined by their width, height, and (x, y) coordinates of their top-left corners, in C.

9. Basic Matrix Algorithm Problems in C

Full Course on Data Structures and Algorithms in C

By: Chrysanthus Date Published: 3 Feb 2026

The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.

The rows of a matrix can be used to represent the horizontal lines of Cartesian Coordinates (Graph). The columns of a matrix can be used to represent the vertical lines of the Cartesian Coordinates. Since a matrix is represented in a program by a two-dimensional array, this means that the rows of a 2D array can be used to represent the horizontal lines of the Cartesian Coordinates; and the columns of a 2D array can be used to represent the vertical lines of the Cartesian Coordinates.

In the program below, there are no negative x-lines and no negative y-lines. Also y-lines become more positive downwards (and not upwards).

Rows represent y-lines while columns represent x-lines.

The rest of this tutorial answers the question, beginning with some explanation.

Note:

There are two rectangles concerned. So there are two sets of values for {x, y, width, height}.
Do not forget that (x,y) are the coordinates for the left-top corners of either rectangle.

To solve the problem, use the logical principle of contradiction. In this case, the principle is: Two rectangles do not intersect if one is completely to the left or completely to the right, or completely to the top, or completely to the bottom of the other. If none of these four conditions are true, then they must intersect.

Completely to the left, does not only mean that the two rectangles are at the same horizontal level. The left one may be higher up or lower down, but still to the left.
Completely to the right, does not only mean that the two rectangles are at the same horizontal level. The right one may be higher up or lower down, but still to the right.
Completely to the top, does not only mean that the two rectangles are in the same column space. The top one may be leftwards or rightwards, but still high up.
Completely to the bottom, does not only mean that the two rectangles are in the same column space. The bottom one may be leftwards or rightwards, but still low down.

The Conditions

Let rect1 and rect2 be the two rectangles. Let rect2 be fixed (stationary) and rect1 can be to the left or right or top or bottom.

rect1 completely to the left

The condition for no intersection here, true, is:

    rect1.x + rect1.width < rect2.x

rect1 completely to the right

The condition for no intersection here, true, is:

    rect1.x > rect2.x + rect2.width

rect1 completely to the top

The condition for no intersection here, true, is:

    rect1.y + rect1.height < rect2.y    //y grows positively downwards

rect1 completely to the bottom

The condition for no intersection here, true, is:

    rect1.y > rect2.y + rect2.height    //y grows positively downwards

Do not forget that y values on the screen as row lines, become more positive from top to bottom.

The above four conditions are combined with OR operators and just NOTted (complemented) and put in the return statement of the called function, as the principal solution; see below.

The Program

The combined conditions in the return statement is NOTted (complemented), for the contradiction principle. The program is (read through the code and comments):

#include <stdio.h>
#include <stdbool.h>    //for true and false

typedef struct {
    int x, y, width, height;
} Rectangle;

bool checkIntersection(Rectangle rect1, Rectangle rect2) {

    return !(rect1.x + rect1.width < rect2.x   ||    // condition 1
             rect1.x > rect2.x + rect2.width   ||    // condition 2
             rect1.y + rect1.height < rect2.y  ||    // condition 3
             rect1.y > rect2.y + rect2.height);     // condition 3
}

int main() {
    // Example 1: Intersecting Rectangles
    Rectangle r1 = {0, 0, 10, 10}; // top-left(0,0), width 10, height 10
    Rectangle r2 = {5, 5, 10, 10}; // top-left(5,5), width 10, height 10, Overlaps at (5,5) to (10,10)
    
    printf("Example 1 (Intersection): %s\n", checkIntersection(r1, r2) ? "Yes" : "No");

    // Example 2: Non-Intersecting Rectangles
    Rectangle r3 = {0, 0, 5, 5};
    Rectangle r4 = {10, 10, 5, 5}; // far to the bottom-right
    
    printf("Example 2 (Non-intersection): %s\n", checkIntersection(r3, r4) ? "Yes" : "No");

    return 0;
}

The output is:

    Example 1 (Intersection): Yes
    Example 2 (Non-intersection): No

Edge Cases Comments?

There are two edge cases, thus:

a) If both rectangles share a common border from outside, is that intersection?
b) If one of the rectangles is completely included inside the other one, is that intersection?

Solution a) The choice made by the author to question a) is No. That is handled by using ">" instead of ">=" or "<" instead of "<=" .
Solution b) The choice made by the author to question b) is Yes. The logic of the algorithm automatically handles that.





Related Links

More Related Links

Cousins

BACK NEXT

Comments


Note: You can use the Search Box above to find articles and discussions of interest.