Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,082 questions

55,955 answers

573 users

How to determine whether two axis‑aligned rectangles overlap in Pascal

1 Answer

0 votes
program RectangleOverlap;

{
Axis‑aligned rectangles are simply rectangles whose edges are parallel to the 
coordinate axes — meaning their sides are horizontal and vertical. 
That single assumption makes the overlap test dramatically simpler and faster.
}

{
    Rectangle overlap detection (axis-aligned)

    Each rectangle is defined by:
        - x, y : coordinates of its top-left corner
        - w, h : width and height

    Two rectangles DO NOT overlap if any separating condition is true:
        - One is completely to the left of the other
        - One is completely to the right of the other
        - One is completely above the other
        - One is completely below the other

    Otherwise, they overlap.

    This is the standard O(1) test for axis-aligned rectangles.
}

type
    Rect = record
        x: Double;   // top-left X
        y: Double;   // top-left Y
        w: Double;   // width
        h: Double;   // height
    end;

{
    Returns true if rectangles A and B overlap.
}
function rectanglesOverlap(const A, B: Rect): Boolean;
var
    A_left, A_right, A_top, A_bottom: Double;
    B_left, B_right, B_top, B_bottom: Double;
begin
    // Compute edges of A
    A_left   := A.x;
    A_right  := A.x + A.w;
    A_top    := A.y;
    A_bottom := A.y + A.h;

    // Compute edges of B
    B_left   := B.x;
    B_right  := B.x + B.w;
    B_top    := B.y;
    B_bottom := B.y + B.h;

    // Separating conditions:
    if (A_right <= B_left) then begin
        rectanglesOverlap := False;   // A is left of B
        Exit;
    end;

    if (B_right <= A_left) then begin
        rectanglesOverlap := False;   // B is left of A
        Exit;
    end;

    if (A_bottom <= B_top) then begin
        rectanglesOverlap := False;   // A is above B
        Exit;
    end;

    if (B_bottom <= A_top) then begin
        rectanglesOverlap := False;   // B is above A
        Exit;
    end;

    rectanglesOverlap := True; // Otherwise, they overlap
end;

var
    A, B, C: Rect;

begin
    A.x := 10;  A.y := 10;  A.w := 30;  A.h := 20;   // Example rectangle A
    B.x := 25;  B.y := 15;  B.w := 40;  B.h := 25;   // Overlaps A
    C.x := 100; C.y := 100; C.w := 10;  C.h := 10;   // Does not overlap A

    if rectanglesOverlap(A, B) then
        WriteLn('A vs B overlap? YES')
    else
        WriteLn('A vs B overlap? NO');

    if rectanglesOverlap(A, C) then
        WriteLn('A vs C overlap? YES')
    else
        WriteLn('A vs C overlap? NO');
end.


{
run:

A vs B overlap? YES
A vs C overlap? NO

}

 



answered Jul 18 by avibootz

Related questions

...