Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:45:41+00:00 2026-05-23T16:45:41+00:00

I’m looking for an algorithm that takes two rectangles defined by (xa1,ya1,xa2,ya2) and (xb1,yb1,xb2,yb2),

  • 0

I’m looking for an algorithm that takes two rectangles defined by (xa1,ya1,xa2,ya2) and (xb1,yb1,xb2,yb2), checks if they can be combined into a single rectangle and if they can, returns the new rectangle. An example:

xa1=0,ya1=0,xa2=320,ya2=119
xb1=0,yb1=120,xb2=320,yb2=239

These two rectangles can be combined into the following rectangle:

xc1=0,yc1=0,xc2=320,yc2=240

How would you implement such an algorithm? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:45:41+00:00Added an answer on May 23, 2026 at 4:45 pm

    After much fiddling I kind of worked out what you want. Note that there is still some contention to what you mean by ‘strict bounding box’: the sample in you original question does not satisfy the description you gave:

    But the rectangles should only be combined if the bounding box is exactly the size of the two rectangles combined, i.e. the area of the bounding rectangle must be exactly the same as the size of the areas of the two source rectangles. If the area of rect 1 is a1, and the area of rect2 is a2, and the area of the bounding rect is a3, then a1+a2=a3.

    This implementation should give you plenty of ideas, and I’m sure you know how to write

    r.area() == a.area() + b.area()
    

    if you really wanted that.


    Codepad code:

    // Final proposal: combine adjacent rectangles, 
    // if they are 'flush': almost touching
    
    #include <iostream>
    
    struct R
    {
        int x1,y1,x2,y2;
        int height() const { return y2-y1; }
        int width() const  { return y2-y1; }
    
        void normalize() 
        { 
            if (x1>x2) std::swap(x1,x2);
            if (y1>y2) std::swap(y1,y2);
        }
    
        /*
         * adjacent: return whether two rectangles
         * are adjacent; the tolerance in pixels
         * allow you to specify the gap:
         *    tolerance = 0: require at least one pixel overlap
         *    tolerance = 1: accepts 'flush' adjacent neighbours
         * Negative tolerance require more overlap;
         * tolerance > 1 allows gaps between rects;
         */
        bool adjacent(R const& other, int tolerance=1) const
        {
            return !( (other.x1 - x2) > tolerance
                   || (x1 - other.x2) > tolerance
                   || (other.y1 - y2) > tolerance
                   || (y1 - other.y2) > tolerance);
        }
    };
    
    /* 
     * tolerance: see R::adjacent()
     * 
     * strict: only allow strict ('pure') combinations of rects
     */
    R combined(R const& a, R const& b, int tolerance=1, bool strict=false)
    {
        if (!a.adjacent(b, tolerance))
            throw "combined(a,b): a and b don't satisfy adjacency requirements (are the coords normalized?)";
    
        R r = { min(a.x1, b.x1), 1,1,1};
        r.x1 = min(a.x1, b.x1);
        r.y1 = min(a.y1, b.y1);
        r.x2 = max(a.x2, b.x2);
        r.y2 = max(a.y2, b.y2);
    
        if (!strict)
            return r;
    
        if ( (r.height() <= max(a.height(), b.height()))
         &&  (r.width () <= max(a.width (), b.width ())) )
            return r;
        else
            throw "combined(a,b): strict combination not available";
    }
    
    std::ostream& operator<<(std::ostream &os, R const& r)
    {
        return os << '(' << r.x1 << "," << r.y1 << ")-(" << r.x2 << ',' << r.y2 << ')';
    }
    
    int main()
    {
        const int tolerance = 1;
        {
            std::cout << "sample from original question" << std::endl;
            R a = { 0, 0,   320, 119 }; /* a.normalize(); */
            R b = { 0, 120, 320, 239 }; /* b.normalize(); */
    
            std::cout << "a: " << a << "\t b: " << b << std::endl;
            std::cout << "r: " << combined(a,b, tolerance) << std::endl;
        }
        {
            std::cout << "sample from the comment" << std::endl;
            R a = { 0, 0, 1, 320 }; /* a.normalize(); */
            R b = { 0, 0, 2, 320 }; /* b.normalize(); */
    
            std::cout << "a: " << a << "\t b: " << b << std::endl;
    
            // NOTE: strict mode
            std::cout << "r: " << combined(a,b, tolerance, true) << std::endl;
        }
    }
    

    Output:

    sample from original question
    a: (0,0)-(320,119)   b: (0,120)-(320,239)
    r: (0,0)-(320,239)
    sample from the comment
    a: (0,0)-(1,320)     b: (0,0)-(2,320)
    r: (0,0)-(2,320)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.