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 7675441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:57:48+00:00 2026-05-31T16:57:48+00:00

Except from my Rect-class: public class Rect { public int x; public int y;

  • 0

Except from my Rect-class:

public class Rect {
  public int x;
  public int y;
  public int w;
  public int h;

  public Rect(int x, int y, int w, int h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }

  ...
}

I have a method to check if two Rects intersects (no pun intended):

public boolean intersect(Rect r) {
  return (((r.x >= this.x) && (r.x < (this.x + this.w))) || ((this.x >= r.x) && (this.x < (r.x + r.w)))) &&
  (((r.y >= this.y) && (r.y < (this.y + this.h))) || ((this.y >= r.y) && (this.y < (r.y + r.h))));
}

Test case:

r1 = (x, y, w, h) = (0, 0, 15, 20)  center: (x, y) = (7, 10)
r2 = (x, y, w, h) = (10, 11, 42, 15)  center: (x, y) = (31, 18)
r1 Intersect r2: true

The class works fine.

What I’m wondering is if there is another – perhaps faster – way to check if the rectangles are intersecting. Can I optimize it in some way?

  • 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-31T16:57:49+00:00Added an answer on May 31, 2026 at 4:57 pm

    I tend to store rectangles as min x, min y, max x and max y. Then overlap occurs when

    r1.maxX > r2.minX &&
    r1.minX < r2.maxX &&
    r1.maxY > r2.minY &&
    r1.minY < r2.maxY
    

    And if they overlap, the intersection is defined by

    r3.minX = max(r1.minX, r2.minX);
    r3.minY = max(r1.minY, r2.minY);
    r3.maxX = min(r1.maxX, r2.maxX);
    r3.maxY = min(r1.maxY, r2.maxY);
    

    Some care should be taken depending on whether or not you consider them to be overlapping if they have the same boundary. I’ve used strict inequalities meaning that overlapping boundaries do not count as an overlap. Given that you are using integers (and thus the boundaries have a width of 1) I will assume that you do want to consider overlapping boundaries as an overlap. I would do something like:

    public class Rect {
        public int minX;
        public int minY;
        public int maxX;
        public int maxY;
    
        public Rect() {}
    
        public Rect(int x, int y, int w, int h) {
            this.minX = x;
            this.minY = y;
            this.maxX = x + w -1;
            this.maxY = y + h -1;
        }
    
        public boolean Intersect(Rect r) {
            return this.maxX >= r.minX &&
                   this.minX <= r.maxX &&
                   this.maxY >= r.minY &&
                   this.minY <= r.maxY;              
        }
    
        public Rect GetIntersection(Rect r) {
            Rect i = new Rect();
            if (this.Intersect(r)) {
                i.minX = Math.max(this.minX, r.minX);
                i.minY = Math.max(this.minY, r.minY);
                i.maxX = Math.min(this.maxX, r.maxX);
                i.maxY = Math.min(this.maxY, r.maxY);
            }
            return i;       
       }
    
       public int GetWidth() {
           return this.maxX - this.minX + 1;   
       }
    
        public int GetHeight() {
            return this.maxY - this.minY + 1;   
        }
    
        public void SetPosition(int x, int y) {
            int w = this.GetWidth();
            int h= this.GetHeight();
            this.minX = x;
            this.minY = y;
            this.maxX = x + w -1;
            this.maxY = y + h -1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class shape { public int x, y; public class triangle { int sides
Provide an example for the pseudo-regex: Match every url except those from example.com and
are there any additional WPF themes from microsoft except that default ones provided in
I am running a query that returns records from the LEFT of the EXCEPT
I just switched my code from Objective-C to Objective-C++. Everything goes swimmingly except for
I'm porting a web-app from iPhone to Android. Unexpectedly, buttons work as usual, except
OK, so I know that from-import is exactly the same as import , except
I know nothing about Xcode, except it's a Developer interface from Apple that actually
I am little confused... Is it true that reading\writing from several threads all except
Here is an except from a manual for a piece of hardware I wish

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.