Nate
Nate

Reputation: 925

Finding out if two rectangles intersect

This is a homework assignment, so I'm mostly asking for a nudge in the right direction. I've figured out the majority of the problem. We have a Rectangle class, whose rectangles are defined by the x and y coordinates in a Point class. The one part I'm stuck on is figuring out if the two rectangles intersect. I've done some research, and came upon this:

http://silentmatt.com/rectangle-intersection/

That helps me with how it would need to be done. But I can't seem to implement it in the way that our teacher had it setup in the problem overview:

//This method returns true if this rectangle intersects with another rectangle (which is provided as a parameter)
bool intersects(Rectangle);

The intersects function only takes one parameter, that is of the Rectangle class. The call to it in main is something like this:

rec1.intersects(rec2);

I can't seem to figure out how to implement the actual function, though. I've already defined the points for both rec1 and rec2 with setters, but I can't seem to figure out how to access those points with the function that only accepts the one parameter. Because, in the example above, I can access the x and y points for rec2, but I can't with rec1.

Any tips on how to proceed?

EDIT: I think part of my problem is just how this was implemented. Here's what we have in the Rectangle class:

private:
Point lowerLeftCorner;
Point upperRightCorner;

And then he had us setup get and set functions in the Point class to set the points for lowerLeftCorner and upperRightCorner.

The problem is when I'm in the intersect function, I can ONLY (unless I'm missing something) use the get functions for the parameter (rectangle2) and not for the first rectangle (rectangle1).

If I was able to pass both rectangles as parameters, I could use the given examples to solve this. It's being able to find the values of x and y for the first rectangle that's giving me trouble.

EDIT: Robert's info helped me figure out how to do it using "this". Here's what I ended up with which seems to work well. It's probably not the best, I am still pretty new to programming.

bool Rectangle::intersects(Rectangle rec2) {
    return ( getupperX() > rec2.getlowerX() &&
    getlowerX() < rec2.getupperX() &&
    getupperY() > rec2.getlowerY() &&
    getlowerY() < rec2.getupperY());
    }
}

Seems to have worked for all the values I've tested!

Upvotes: 2

Views: 2986

Answers (3)

Robert Kelly
Robert Kelly

Reputation: 1006

In C++, if you're inside a member function of class, you have access to the member variables of that class implicitly.

So if you had

struct Rectangle 
{
  Point UpperLeft;
  Point LowerRight;
  bool intersects(Rectangle);
}

Then within the intersects() function, because it is a member of the class, you can access member variables of that class, like this:

Rectangle::intersects(Rectangle rhs)
{
  // So the second UpperLeft here is a member of "this" instance of the class
  if(rhs.UpperLeft < UpperLeft)
    return true;
}

That function won't actually detect intersection, but that's how you would access all the points.

So if you had:

Rectangle rect_a;
Rectangle rect_b;
rect_a.intersects(rect_b);

Then you would be calling rect_a's intersects() function, so it would be "this" in the example above, where rect_b would be rhs

EDIT: Alternatively, in your example above, you could write something like

Rectangle::intersects(Rectangle rhs)
{
  if(rhs.GetLowerLeft() < GetLowerLeft())
    return true;
}

And the second GetLowerLeft() would be called on "this"

Upvotes: 2

MurderDeathCode
MurderDeathCode

Reputation: 41

Pubby gave one possible case for this problem.

If you really want to work through it, try looking at it in the 1-dimensional case first, 2 lines. You'll find 3-5ish cases:

Overlap on left side. Overlap on right side. Both left endpoint coordinates are the same Both right endpoint coordinates are the same Both left and both right coordinates are the same (Lines are same length and same coordinates)

Then try to extend that out to a two-dimensional object. Pubby's example should get you started well.

Upvotes: 1

Pubby
Pubby

Reputation: 53017

This?

bool Rectangle::intersects(Rectangle r) {
  return
    (x1 < r.x2
  && x2 > r.x1
  && y1 < r.y2
  && y2 > r.y1);
}

If you want to use get functions then use them:

bool Rectangle::intersects(Rectangle r) {
  return
    (this->getX1() < r.getX2();
  // ....
}

Upvotes: 1

Related Questions