问题描述
我试图写一个C ++程序,它从用户以下投入兴建的矩形(2至5之间):高度,宽度,X POS机,Y-POS。所有这些矩形的将存在平行于x和y轴,即所有它们的边缘将具有0或无穷大斜坡
我试图执行什么是提到在这个的问题,但我没有很多的运气。
我目前的实现执行以下操作:
//获取所有顶点的矩形1,并将其存储在一个数组 - > arrRect1
//点1个:arrRect1 [0],点1 Y:arrRect1 [1]等等...
//获取数组中的所有顶点的矩形2,并将其存储 - > arrRect2
//旋转的点边缘,矩形1
INT rot_x,rot_y;
rot_x = -arrRect1 [3];
rot_y = arrRect1 [2];
//在旋转的边缘点
INT pnt_x,pnt_y;
pnt_x = arrRect1 [2];
pnt_y = arrRect1 [3];
//测试点,距离矩形2
INT tst_x,tst_y;
tst_x = arrRect2 [0];
tst_y = arrRect2 [1];
int值;
值=(rot_x *(tst_x - pnt_x))+(rot_y *(tst_y - pnt_y));
COUT<< 值:<<值;
不过,我不太清楚,如果(一)我已经实现了我的算法联系到正确的,或者如果我这样做究竟是如何相互preT呢?
有什么建议?
如果(RectA.Left< RectB.Right和放大器;&安培; RectA.Right> RectB.Left&安培; &放大器;
RectA.Bottom< RectB.Top&功放;&安培; RectA.Top> RectB.Bottom)
或者使用笛卡尔坐标...
如果(RectA.X1< RectB.X2和放大器;&安培; RectA.X2> RectB.X1和放大器;&安培;
RectA.Y1< RectB.Y2&功放;&安培; RectA.Y2> RectB.Y1)
假设你有矩形A和B.矩形证据是矛盾的。那四个条件保证任何一个没有重叠可以存在
- COND1。如果A的左边缘到B的右边缘的右侧, - 那么A是完全到右乙
- COND2。如果A的右边缘是到B的左边缘的左侧, - 那么A是完全向左中乙
- Cond3。如果A的顶部边缘低于B的底部边缘, - 那么A是完全下文B
- Cond4。如果A的底部边缘高于B的顶部边缘, - 那么A是完全以上乙
所以条件的非重叠
COND1或COND2或者Cond3或者Cond4
因此,一个充分条件重叠是相反的(德摩根)
不COND1而不是COND2和不Cond3而不是Cond4
这是等价于:
- 系统的左边缘到左乙的右边缘,和
- A的右边缘到B的左边缘的权利,
- 系统的顶级高于B的底部,和
- 在下文B评出A的底部
注1 :这是相当明显的同样的原理可以扩展到任何数量的尺寸
注意2 :这也应该是相当明显的计算只是一个像素的重叠,改<
和/或>
上边界为< =
或> =
。
I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectangles will exist parallel to the x and the y axis, that is all of their edges will have slopes of 0 or infinity.
I've tried to implement what is mentioned in this question but I am not having very much luck.
My current implementation does the following:
// Gets all the vertices for Rectangle 1 and stores them in an array -> arrRect1
// point 1 x: arrRect1[0], point 1 y: arrRect1[1] and so on...
// Gets all the vertices for Rectangle 2 and stores them in an array -> arrRect2
// rotated edge of point a, rect 1
int rot_x, rot_y;
rot_x = -arrRect1[3];
rot_y = arrRect1[2];
// point on rotated edge
int pnt_x, pnt_y;
pnt_x = arrRect1[2];
pnt_y = arrRect1[3];
// test point, a from rect 2
int tst_x, tst_y;
tst_x = arrRect2[0];
tst_y = arrRect2[1];
int value;
value = (rot_x * (tst_x - pnt_x)) + (rot_y * (tst_y - pnt_y));
cout << "Value: " << value;
However I'm not quite sure if (a) I've implemented the algorithm I linked to correctly, or if I did exactly how to interpret this?
Any suggestions?
if (RectA.Left < RectB.Right && RectA.Right > RectB.Left &&
RectA.Bottom < RectB.Top && RectA.Top > RectB.Bottom)
or, using Cartesion coordinates...
if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1)
Say you have Rect A, and Rect B.Proof is by contradiction. Any one of four conditions guarantees that no overlap can exist:
- Cond1. If A's left edge is to the right of the B's right edge, - then A is Totally to right Of B
- Cond2. If A's right edge is to the left of the B's left edge, - then A is Totally to left Of B
- Cond3. If A's top edge is below B's bottom edge, - then A is Totally below B
- Cond4. If A's bottom edge is above B's top edge, - then A is Totally above B
So condition for Non-Overlap is
Cond1 Or Cond2 Or Cond3 Or Cond4
Therefore, a sufficient condition for Overlap is the opposite (De Morgan)
Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4
This is equivalent to:
- A's Left Edge to left of B's right edge, and
- A's right edge to right of B's left edge, and
- A's top above B's bottom, and
- A's bottom below B's Top
Note 1: It is fairly obvious this same principle can be extended to any number of dimensions.
Note 2: It should also be fairly obvious to count overlaps of just one pixel, change the <
and/or the >
on that boundary to a <=
or a >=
.
这篇关于确定两个矩形彼此重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!