Search code examples
c++loopsinputaabb

How can i store the input without using array?


The question required to compute a minimum-sized AABB for a set of input geometries.

The types of geometries are listed below:

  1. Rectangle , center_x , center_y, width , height : R x y w h
  2. Circle, center_x, center_y, radius : C x y r
  3. Set of points, number of points, x_1, y_1 , ... , x_n , y_n : P n x_1 y_1 ... x_n y_n
  4. # : indicates end of input

The sample case are shown below:

input:

R 0 0 3 2
#

output:

0 0 3 2

input:

P 2 3 -2 -1 4

C -0.5 3.2 1.6

P 3 -1.5 3 3 3 5 3

R 0 0 3 2

#

output:

1.45 1.4 7.1 6.8


I am wondering about how the sample case two run, as there are multi-input, how can I construct my AABB? However, how to avoid the situation that the variable being replace?

For example,

R 0 0 3 2

R 1 2 4 5

I just don't know how to avoid the replacement of the center_x, center_y , w and h.


below are my code,

#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;
}

Thanks a lot !!


Solution

  • You don't need an array for this. As an analogy, imagine you are instead trying to find the minimum int from a list of ints (which is similar to what you're doing, but in one dimension instead of two). All you need to keep track of the minimum value is

    1. The current minimum of all previous values (current), and
    2. The next value (value).

    From those two values you can calculate the next minimum - min(current, value), then grab the next value, calculate the minimum again, and so on. At the end of the input print out current. At no point do you need to store all values at once.

    For your question, replace current with the current minimum-sized AABB, value with the next shape, and min with a function that returns the minimum-sized AABB that contains both shapes.

    enter image description here

    In this image, you have 2 previous shapes (the 2 black boxes), the current minimum (the gray box), the next shape (the blue circle), and the next minimum (the red box) that you need to calculate, which contains both the gray box and the blue circle.