Question: Question: 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
Question:
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). 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).
Create a source and a header file for your class and name them rectangle.cpp and rectangle.h.
Your class rectangle should have four private integer class variables d_x_start, d_x_end, d_y_start and d_y_end. Define a constructor that takes as arguments the lower left corner of a rectangle and its width and height. Design the constructor in such a way that it can be called with all or less aguments including no arguments. Be sure to use initializer lists. If the lower left corner is missing, it should default to -1 and if width and/or height are missing, they should default to 1. As we have assumed that the x and y coordinates are always greater or equals $0$, a default constructed rectangle will be {\em invalid}, i.e., we asume that it is not used further. In this assignment, you are not asked to perform error checking as we still have to discuss how this is done in C++.
Give your class a function print that simply outputs the four integers to console in the format stop and does not return anything
Design a function intersect in your class which accept an rectangle object as argument and returns true if two triangles overlap. Note that we understand the corners as belonging to a rectangle, i.e., the ranges in x and y are inclusive. Note that axis aligned rectangles, e.g., rectangles A and B, do not overlap if in one dimension the following order of coordinates applies:
A_start A_end B_start B_end
B_start B_end A_start A_end
Design a function intersection in your class which accept an rectangle object as argument and returns another rectangle as result. This is easier if you use your overlap test from above. If there is no overlap return a default constructed rectangle. Note that for intersecting axis aligned rectangles, e.g., rectangles A and B, there are four cases for each dimension (and is actually quite simple):
A_start B_start A_end B_end
A_start B_start B_end A_end
B_start A_start B_end A_end
B_start A_start A_end B_end
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.
----------------------------------------------------------------------------------------------------------------------
programs:
main.cpp:
#include #include
#include "rectangle.h"
int main() {
std::vector rVec;
rVec.emplace_back(Rectangle(3,4,10,5));
rVec.emplace_back(Rectangle(8,8,3,4));
rVec.emplace_back(Rectangle(4,1,10,2));
rVec.emplace_back(Rectangle(14,2,3,5));
rVec.emplace_back(Rectangle(1,2,15,9));
for (int oI=1; 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 children = rVec[0].split();
for (auto child:children) {
child.print();
std::cout << std::endl;
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------
rectangle.h:
#ifndef RECTANGLE_H
#define RECTANGLE_H
using namespace std;
class Rectangle {
private:
int d_x_start;
int d_x_end;
int d_y_start;
int d_y_end;
public:
int width;
int height;
public:
Rectangle();
Rectangle(int d_x_start, int d_y_start, int width, int height);
int setX2();
int getX() const;
void print();
};
#endif
----------------------------------------------------------------------------------------------------------------------
rectangle.cpp:
#include #include
#include "rectangle.h"
Rectangle::Rectangle(){
d_x_start = -1;
d_y_start = -1;
width = -1;
height = -1;
}
Rectangle::Rectangle(int _x_start, int _y_start, int _width, int _height) {
d_x_start = _x_start;
d_y_start = _y_start;
width = _width;
height = _height;
}
int Rectangle::setX2(){
d_x_start = d_x_start + width;
return d_x_end;
}
int Rectangle::getX() const{
return d_x_start;
}
void Rectangle::print(){
cout << "(" << d_x_start << ", " << d_y_start << ") to " << flush;
cout << "(" << setXEnd() << ", " << setYEnd() << ")" << flush;
}
----------------------------------------------------------------------------------------------------------------
i would like to get the second rectangle point, e.g. the point is (3,4,10,5) and the first point is (3,4), second point is (12,8).
however, now i'm getting the wrong answer for the second point.
the answer is the following:
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)
Note: this program is written in C++.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
