9.1 Give an algorithm, to check if two rectangles intersect, where rectangles are defined by their width, height, and (x, y) coordinates of their centers, 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:
If both rectangles share a common border from outside (i.e. they are touching after being brought nearer), then that is not intersection, for this tutorial.
Imagine x values on the screen as column lines that become more positive from left to right. Imagine y values on the screen as row lines that become more positive from top to bottom. The intersection of line x=0 and line y=0 has the coordinates, (0,0).
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 x/y center point, of either rectangle.
To determine if two rectangles intersect using their centers, widths, and heights, the distance between their centers must be less than the sum of their half-dimensions along both axes (see below).
To solve the problem, calculate the absolute distance in x and y between both centers and compare that to half of the sum of their widths and heights, respectively. To appreciate this solution, imagine that the two rectangles are almost touching at their borders.
Before presenting the program below, know that in mathematics:
width1 / 2 + width2 / 2 = (width1 + width2) / 2
also
height1 / 2 + height2 / 2 = (height1 + height2) / 2
As the principal solution, the condition (logical AND of two sub-conditions) of
(difX < summedHalfWidths) && (difY < summedHalfHeights);
is put in the return statement; where
difX = absoluteValueOf(rect1.x - rect2.x) and difY = absoluteValueOf(rect1.y - rect2.y)
The Program
The ANDed sub-conditions is in the return statement as the principal solution. True is returned if they intersect and false is returned, if they do not. The program is (read through the code and comments):
#include <stdio.h>
#include <stdlib.h> //for abs() function
#include <stdbool.h> //for true and false
typedef struct {
float x, y, width, height;
} Rectangle;
bool checkIntersection(Rectangle rect1, Rectangle rect2) {
float difX = abs(rect1.x - rect2.x); //horizontal distance between centers
float difY = abs(rect1.y - rect2.y); //vertical distance between centers
float summedHalfWidths = (rect1.width + rect2.width) / 2.0;
float summedHalfHeights = (rect1.height + rect2.height) / 2.0;
return (difX < summedHalfWidths) && (difY < summedHalfHeights);
}
int main() {
// Example 1: Intersecting Rectangles
Rectangle r1 = {0, 0, 10, 10}; // top-left(0,0), width 10, height 10
Rectangle r2 = {8, 0, 10, 10}; // top-left(8,0), width 10, height 10
printf("Example 1 (Intersection): %s\n", checkIntersection(r1, r2) ? "Yes" : "No");
// Example 2: Non-Intersecting Rectangles
Rectangle r3 = {0, 0, 4, 4}; // top-left(0,0), width 4, height 4
Rectangle r4 = {10, 10, 4, 4}; // top-left(10,10), width 4, height 4
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 "<= .
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 LinksCousins
BACK NEXTComments
Note: You can use the Search Box above to find articles and discussions of interest.