找出两个矩形是否相交

时间:2022-12-21 11:25:36

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:

这是一项家庭作业,所以我主要是要求朝着正确的方向努力。我已经找到了大部分问题。我们有一个Rectangle类,其矩形由Point类中的x和y坐标定义。我坚持的一部分是弄清楚两个矩形是否相交。我做了一些研究,并且发现了这个:

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:

intersects函数只接受一个参数,即Rectangle类。在main中调用它是这样的:

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.

但我似乎无法弄清楚如何实现实际功能。我已经用setter定义了rec1和rec2的点数,但我似乎无法弄清楚如何使用只接受一个参数的函数来访问这些点。因为,在上面的示例中,我可以访问rec2的x和y点,但我不能使用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:

编辑:我认为我的问题的一部分就是如何实现。这是我们在Rectangle类中的内容:

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.

然后他让我们在Point类中设置get和set函数来设置lowerLeftCorner和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).

问题是当我在交叉函数中时,我只能(除非我遗漏了某些东西)使用参数(rectangle2)的get函数而不是第一个矩形(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.

如果我能够将两个矩形作为参数传递,我可以使用给定的示例来解决这个问题。它能够为第一个给我带来麻烦的矩形找到x和y的值。

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!

似乎已经为我测试的所有价值观工作了!

3 个解决方案

#1


2  

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

在C ++中,如果您在类的成员函数中,则可以隐式访问该类的成员变量。

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:

然后在intersects()函数中,因为它是类的成员,您可以访问该类的成员变量,如下所示:

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

然后你将调用rect_a的intersects()函数,所以在上面的例子中它将是“this”,其中rect_b将是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"

第二个GetLowerLeft()将在“this”上调用

#2


1  

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:

如果要使用get函数,请使用它们:

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

#3


1  

Pubby gave one possible case for this problem.

Pubby为这个问题提供了一个可能的案例。

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:

如果你真的想要通过它,尝试先在1维情况下看两行。你会发现3-5个案例:

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.

然后尝试将其扩展为二维对象。 Pubby的例子应该让你开始很好。

#1


2  

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

在C ++中,如果您在类的成员函数中,则可以隐式访问该类的成员变量。

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:

然后在intersects()函数中,因为它是类的成员,您可以访问该类的成员变量,如下所示:

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

然后你将调用rect_a的intersects()函数,所以在上面的例子中它将是“this”,其中rect_b将是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"

第二个GetLowerLeft()将在“this”上调用

#2


1  

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:

如果要使用get函数,请使用它们:

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

#3


1  

Pubby gave one possible case for this problem.

Pubby为这个问题提供了一个可能的案例。

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:

如果你真的想要通过它,尝试先在1维情况下看两行。你会发现3-5个案例:

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.

然后尝试将其扩展为二维对象。 Pubby的例子应该让你开始很好。