70.7k views
1 vote
Write a program to do the following:

a. First, your program will continuously read as input lines with 4 floats. Each set of 4 floats will define a 2D rectangle using your Rect class. Add each Rect to a container data structure of your choice, such as std::vector. Stop reading rectangles when your program reads 4 negative float values (do not create the rectangle with 4 negative float values).
b. Next, continuously read as input lines with 2 floats. Each pair of floats here will define a 2D point using your Vec class. For each point, print out its classification (whether it is inside or outside) with respect to all previously read rectangles. Stop reading points and end the program when you read (-99.0f,-99.0f).

1 Answer

7 votes

Final answer:

The task involves writing a C++ program to read rectangles and points, store rectangles in a vector, and then classify points as inside or outside these rectangles. The program stops reading rectangles after encountering 4 negative values, and stops reading points when it reads (-99.0f, -99.0f).

Step-by-step explanation:

To solve this problem, we will create a simple program in C++ that reads rectangles and points, and then determines if each point is inside or outside the rectangles. We will use a Rect class for rectangles and a Vec class for points. The std::vector will serve as our container for storing rectangles.

Let's define both classes first:

class Rect {
public:
float x1, y1, x2, y2;
// Constructor and other member functions here
};
class Vec {
public:
float x, y;
// Constructor and other member functions here
};

Now, here's a simplified version of the program:

#include
#include
int main() {
std::vector rectangles;
float a, b, c, d;
while (std::cin >> a >> b >> c >> d && (a >= 0 && b >= 0 && c >= 0 && d >= 0)) {
rectangles.push_back(Rect{a, b, c, d});
}
float x, y;
while (std::cin >> x >> y && !(x == -99.0f && y == -99.0f)) {
Vec point{x, y};
for (const auto& rect : rectangles) {
// Logic to classify point as inside or outside
}
// Print classification of the point
}
return 0;
}

The above code continuously reads groups of 4 floats, creating Rect objects, until it encounters 4 negative values, after which it begins to read points and classify them. The classification logic would involve checking whether the point coordinates fall within the rectangular bounds defined by the Rect objects in the vector.

User Gautam Sheth
by
8.9k points