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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

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

1 Answer

0 votes
/*
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.
*/

case class Rect(
    x: Double,    // top-left X
    y: Double,    // top-left Y
    w: Double,    // width
    h: Double     // height
)

/*
    Returns true if rectangles A and B overlap.
*/
def rectanglesOverlap(A: Rect, B: Rect): Boolean = {
    // Compute edges of A
    val A_left: Double   = A.x
    val A_right: Double  = A.x + A.w
    val A_top: Double    = A.y
    val A_bottom: Double = A.y + A.h

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

    // Separating conditions:
    if (A_right <= B_left)   return false   // A is left of B
    if (B_right <= A_left)   return false   // B is left of A
    if (A_bottom <= B_top)   return false   // A is above B
    if (B_bottom <= A_top)   return false   // B is above A

    true // Otherwise, they overlap
}

@main def main(): Unit = {
    val A = Rect(10, 10, 30, 20)   // Example rectangle A
    val B = Rect(25, 15, 40, 25)   // Overlaps A
    val C = Rect(100, 100, 10, 10) // Does not overlap A

    println("A vs B overlap? " + (if rectanglesOverlap(A, B) then "YES" else "NO"))
    println("A vs C overlap? " + (if rectanglesOverlap(A, C) then "YES" else "NO"))
}



/*
run:

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

*/

 



answered Jul 18 by avibootz

Related questions

...