【发布时间】:2010-09-23 07:23:39
【问题描述】:
我正在尝试编写一个 C++ 程序,它接受用户的以下输入来构造矩形(2 到 5 之间):高度、宽度、x-pos、y-pos。所有这些矩形都将平行于 x 轴和 y 轴存在,也就是说,它们的所有边的斜率为 0 或无穷大。
我已尝试实现this 问题中提到的内容,但运气不佳。
我当前的实现如下:
// 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) 我是否已经正确地实现了我链接到的算法,或者我是否做了正确的解释?
有什么建议吗?
【问题讨论】:
-
我认为您的问题的解决方案不涉及任何乘法。
-
如果您需要旋转矩形的答案,我会创建一个包含所有步骤的答案:stackoverflow.com/questions/62028169/…(它在 Javascript 中,但可以在 C++ 中轻松复制)
标签: c++ algorithm geometry overlap rectangles