为一组输入几何计算最小尺寸 AABB 所需的问题。
几何类型如下:
- 矩形,center_x,center_y,宽度,高度:R xywh
- 圆,center_x,center_y,半径:C xyr
- 点集、点数、x_1、y_1、...、x_n、y_n:P n x_1 y_1 ... x_n y_n
- # : 表示输入结束
示例案例如下所示:
输入:
R 0 0 3 2
#
输出:
0 0 3 2
输入:
P 2 3 -2 -1 4
C -0.5 3.2 1.6
P 3 -1.5 3 3 3 5 3
0 0 3 2
#
输出:
1.45 1.4 7.1 6.8
我想知道示例案例二如何运行,因为有多个输入,我该如何构建我的 AABB?但是,如何避免变量被替换的情况呢?
例如,
0 0 3 2
1 2 4 5
我只是不知道如何避免更换 center_x、center_y、w 和 h。
以下是我的代码,
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int times, i;
char geo;
double center_x, center_y, w, h, r;
double box_LX = 0, box_RX = 0, box_DY = 0, box_UY = 0;
bool end;
end = false;
double LX, RX, UY, DY;
while (!end) {
cin >> geo;
if (geo == 'R') {
cin >> center_x >> center_y >> w >> h;
LX = (center_x - w / 2);
RX = (center_x + w / 2);
DY = (center_y - h / 2);
UY = (center_y + h / 2);
if (RX > box_RX)
box_RX = RX;
if (LX < box_LX)
box_LX = LX;
if (UY > box_UY)
box_UY = UY;
if (DY < box_DY)
box_DY = DY;
}
if (geo == 'C') {
cin >> center_x >> center_y >> r;
if (box_RX < (center_x + r))
box_RX = center_x + r;
if (box_LX > (center_x - r))
box_LX = center_x - r;
if (box_UY < (center_y + r))
box_UY = center_y + r;
if (box_DY < (center_y - r))
box_DY = center_y - r;
}
if (geo == 'P') {
cin >> times;
i = 0;
while (i != times) {
cin >> center_x >> center_y;
if (box_RX < center_x)
box_RX = center_x;
if (box_LX > center_x)
box_LX = center_x;
if (box_UY < center_y)
box_UY = center_y;
if (box_DY > center_y)
box_DY = center_y;
i++;
}
}
if (geo == '#') {
cout << (box_LX+box_RX)/2 << " " << (box_DY+box_UY)/2 << " " << abs(box_LX - box_RX) << " " << abs(box_UY - box_DY) << endl;
end = true;
}
}
return 0;
}
非常感谢 !!