Create a Stash class specifically for storing Rect objects and call it RectStash
ID: 3676546 • Letter: C
Question
Create a Stash class specifically for storing Rect objects and call it RectStash. Add a default constructor and a destructor to correctly initialize your RectStash class. Then write a program that will read several lines as input. Each line will contain 4 floats defining a 2D rectangle in the Rect format described above. Read the rectangles adding them to a RectStash object. Stop reading rectangles when your program loads 4 negative float values. After this point you will start reading a series of 2D points, and for each 2D point you will print the classification of each point in respect to all previously read rectangles. The classification should print "in" or "out" according to its result. Stop your program when you read vector (-99,-99). Everything should be contained in one file. You may not assume the existance of any header files in your working directory.
Explanation / Answer
/********************************* RectStash.cpp ********************************************/
#include <iostream>
#include <cassert>
using namespace std;
class Vec {
public:
int x, y;
static Vec const null;
Vec() {
this->x = 0;
this->y = 0;
}
Vec(int x, int y) {
this->x = x;
this->y = y;
}
void add(Vec othervec) {
x += othervec.x;
y += othervec.y;
}
void print() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
Vec const Vec::null = Vec(0, 0);
class Rect {
float upper_x, upper_y, width, height;
public:
Rect(float x, float y, float width, float height) {
this->upper_x = x;
this->upper_y = y;
this->width = width;
this->height = height;
}
bool contains(Vec othervec) {
//check x intersection
if (othervec.x > upper_x + width || othervec.x < upper_x) {
return false;
}
//check y intersection
if (othervec.y < upper_y - height || othervec.y > upper_y) {
return false;
}
return true;
}
};
class RectStash {
int size; // Size of each space
int quantity; // Number of storage spaces
int next; // Next empty space
// Dynamically allocated array of bytes:
unsigned char* storage;
public:
// Functions!
void initialize(int sz){
size = sz;
quantity = 0;
storage = 0;
next = 0;
}
void cleanup(){
if (storage != 0) {
std::cout << "freeing storage" << std::endl;
delete[]storage;
}
}
int add(const void* element){
if (next >= quantity) // Enough space left?
inflate(100);
// Copy element into storage,
// starting at next empty space:
int startBytes = next * size;
unsigned char* e = (unsigned char*)element;
for (int i = 0; i < size; i++)
storage[startBytes + i] = e[i];
next++;
return(next - 1); // Index number
}
void* fetch(int index){
// Check index boundaries:
assert(0 <= index);
if (index >= next)
return 0; // To indicate the end
// Produce pointer to desired element:
return &(storage[index * size]);
}
int count() {
return next; // Number of elements in CStash
}
void inflate(int increase){
assert(increase > 0);
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char[newBytes];
for (int i = 0; i < oldBytes; i++)
b[i] = storage[i]; // Copy old to new
delete[]storage; // Old storage
storage = b; // Point to new memory
quantity = newQuantity;
}
RectStash() {
initialize(sizeof(Rect*));
}
};
int main(int argc, const char * argv[])
{
float a, b, c, d;
RectStash storage;
Rect * tmp;
Vec vector;
do {
cin >> a;
cin >> b;
cin >> c;
cin >> d;
tmp = new Rect(a, b, c, d);
if (a >= 0 || b >= 0 || c >= 0 || d >= 0) {
storage.add(tmp);
}
} while (a >= 0 || b >= 0 || c >= 0 || d >= 0);
do {
cin >> a;
cin >> b;
vector = Vec(a, b);
if (a != -99 || b != -99) {
int i = 0;
tmp = static_cast<Rect *> (storage.fetch(i));
while (tmp != 0) {
if (tmp->contains(vector)) cout << "in ";
else cout << "out ";
i++;
tmp = static_cast<Rect *> (storage.fetch(i));
}
cout << endl;
}
} while (a != -99 || b != -99);
}