我正在尝试编写一个C ++程序,该程序需要用户输入以下内容以构造矩形(2到5之间):高度,宽度,x-pos,y-pos。所有这些矩形将平行于x和y轴存在,也就是说,它们的所有边缘的斜率均为0或无穷大。
我已经尝试实现此问题中提到的内容,但运气并不好。
我当前的实现执行以下操作:
// 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;
但是,我不确定(a)是否已正确实现链接到的算法,或者我是否确切地解释了这一点?
有什么建议?
if (RectA.Left < RectB.Right && RectA.Right > RectB.Left && RectA.Top > RectB.Bottom && RectA.Bottom < RectB.Top )
或者,使用笛卡尔坐标
(其中X1为左坐标,X2为右坐标,从左向右递增,Y1为下坐标,Y2为下坐标,从下往上递增 -如果这不是您的坐标系的方式(例如,大多数计算机具有Y方向颠倒],在下面交换比较)…
if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 && RectA.Y1 > RectB.Y2 && RectA.Y2 < RectB.Y1)
假设您有Rect A和RectB。证明是矛盾的。四个 条件中的任何一个都保证不会存在重叠:
因此,非重叠的条件是
NON-Overlap => Cond1 Or Cond2 Or Cond3 Or Cond4
Therefore, a sufficient condition for Overlap is the opposite.
Overlap => NOT (Cond1 Or Cond2 Or Cond3 Or Cond4)
De Morgan’s law says Not (A or B or C or D) is the same as Not A And Not B And Not C And Not D so using De Morgan, we have
Not (A or B or C or D)
Not A And Not B And Not C And Not D
Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4
这等效于:
2020-07-28