Question In this lab assignment, you will design a class that holds an axis-alig
ID: 3889077 • Letter: Q
Question
Question
In this lab assignment, you will design a class that holds an axis-aligned 2D rectangle and is able to determine if two rectangles overlap and if they overlap can calculate their intersection. We will use integers for all coordinates (e.g. as used in raster images or displays). For this assignment, you may assume that the x/y coordinates are all greater or equals 0. If you like to visualize your results assume that the origin of the coordinate system is in the lower left corner and that x is to the right and y goes up (in the usual way).
Design a function split in your class with no arguments that returns a standard array of four rectangles. Calculate the position and size of the four rectangles to correspond to the rectangle which the function was called on. Note that for uneven side length, the rectangles should differ in size by 1. Again, the corner points are part of a rectangle. An example is shown in the attachment.
An test run of your program will look as follows:
Intersection of Rectangle: (3,4) to (12,8) and Rectangle: (8,8) to (10,11)?
Rectangle: (8,8) to (10,8)
Intersection of Rectangle: (3,4) to (12,8) and Rectangle: (4,1) to (13,2)?
No intersection
Intersection of Rectangle: (3,4) to (12,8) and Rectangle: (14,2) to (16,6)?
No intersection
Intersection of Rectangle: (3,4) to (12,8) and Rectangle: (1,2) to (15,10)?
Rectangle: (3,4) to (12,8)
Testing split of Rectangle: (3,4) to (12,8)
Rectangle: (3,4) to (7,6)
Rectangle: (8,4) to (12,6)
Rectangle: (8,7) to (12,8)
Rectangle: (3,7) to (7,8)
The program
#include <iostream>
#include "rectangle.h"
#include <vector>
#include <string>
using namespace std;
int main() {
//std::cout << "Hello" << std::endl;
std::vector<Rectangle> rVec;
rVec.push_back(Rectangle(3, 4, 10, 5));
rVec.push_back(Rectangle(8, 8, 3, 4));
rVec.push_back(Rectangle(4, 1, 10, 2));
rVec.push_back(Rectangle(14, 2, 3, 5));
rVec.push_back(Rectangle(1, 2, 15, 9));
/*for(int i = 0; i<rVec.size(); i++){
rVec[i].print();
}*/
// Test intersect
for (int oI = 1; oI<rVec.size(); ++oI) {
std::cout << "Intersection of ";
rVec[0].print();
std::cout << " and ";
rVec[oI].print();
std::cout << "?" << std::endl;
if (rVec[0].intersect(rVec[oI])) {
Rectangle r = rVec[0].intersection(rVec[oI]);
r.print();
std::cout << std::endl;
}
else {
std::cout << "No intersection" << std::endl;
}
}
std::cout << std::endl << "Testing split of ";
rVec[0].print();
std::cout << std::endl;
// Test split
std::array<Rectangle, 4> children = rVec[0].split();
for (auto child : children) {
child.print();
std::cout << std::endl;
} */
return 0;
}
Explanation / Answer
#include <iostream>
#include "rectangle.h"
#include <vector>
#include <string>
using namespace std;
int main() {
//std::cout << "Hello" << std::endl;
std::vector<Rectangle> rVec;
rVec.push_back(Rectangle(3, 4, 10, 5));
rVec.push_back(Rectangle(8, 8, 3, 4));
rVec.push_back(Rectangle(4, 1, 10, 2));
rVec.push_back(Rectangle(14, 2, 3, 5));
rVec.push_back(Rectangle(1, 2, 15, 9));
/*for(int i = 0; i<rVec.size(); i++){
rVec[i].print();
}*/
// Test intersect
for (int oI = 1; oI<rVec.size(); ++oI) {
std::cout << "Intersection of ";
rVec[0].print();
std::cout << " and ";
rVec[oI].print();
std::cout << "?" << std::endl;
if (rVec[0].intersect(rVec[oI])) {
Rectangle r = rVec[0].intersection(rVec[oI]);
r.print();
std::cout << std::endl;
}
else {
std::cout << "No intersection" << std::endl;
}
}
std::cout << std::endl << "Testing split of ";
rVec[0].print();
std::cout << std::endl;
// Test split
std::array<Rectangle, 4> children = rVec[0].split();
for (auto child : children) {
child.print();
std::cout << std::endl;
} */
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.