This is similar to the exercise that was discussed in the lectures. However, you
ID: 3885228 • Letter: T
Question
This is similar to the exercise that was discussed in the lectures. However, you need to define two methods, namely, contains and touches, that have slightly different meanings. Please see the diagrams. Define the Rectangle class that contains: Double data fields named xpos, ypos (that specify the top left corner of the rectangle), width and height, (assume that the rectangle sides are parallel to the x and y axes. See example below). A constructor that creates a rectangle with the specified xpos, ypos, width, and height. Get and set methods for all the instance variables. A method contains (Rectangle r) that returns true if the specified rectangle is inside this rectangle. A method touches (Rectangle r) that returns true if the specified rectangle touches this rectangle. You may add other methods if necessary. Study the figures to understand when the contains method should return true, and when the touches method should return true. If the rectangles overlap, both methods should return false.Explanation / Answer
// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
using namespace std;
class Rectangle2D
{
private:
double xpos,ypos,width,height;
public:
Rectangle2D() //default constructor
{
xpos = 0.0;
ypos = 0.0;
width = 0.0;
height =0.0;
}
Rectangle2D(double xpos,double ypos,double width,double height) //parameterized constructor
{
this->xpos = xpos;
this->ypos = ypos;;
this->width =width;
this->height = height;
}
double getWidth()
{
return width;
}
void setWidth(double width)
{
this->width = width;
}
double getHeight()
{
return height;
}
void setHeight(double height)
{
this->height = height;
}
void setXpos(double xpos)
{
this->xpos = xpos;
}
double getXpos()
{
return xpos;
}
void setYpos(double ypos)
{
this->ypos = ypos;
}
double getYpos()
{
return ypos;
}
double getArea()
{
double area;
area = getWidth() * getHeight();
return area;
}
double getPerimeter() //compute perimeter of rectangle
{
double perimeter;
perimeter = 2*(getWidth()+getHeight());
return perimeter;
}
bool contains(double x,double y) //check if a point is inside a rectangle
{
if(x > xpos && x <= width && y > ypos && y <= height)
return true;
else
return false;
}
bool contains(Rectangle2D r) //check if a rectangle is inside another rectangle
{
if(this->xpos >= r.xpos && r.width <= this->width && this->ypos >= r.ypos && r.height <= this->height)
return true;
else
return false;
}
};
int main()
{
double xpos,ypos,width,height;
cout<<" Enter the xpos,ypos,width and height of the rectangle: ";
cin>>xpos>>ypos>>width>>height;
Rectangle2D r1(xpos,ypos,width,height);
cout<<" Perimeter of Rectangle : "<<r1.getPerimeter();
cout<<" Area of Rectangle : "<<r1.getArea();
xpos = 3;
ypos = 3;
if(r1.contains(xpos,ypos))
cout<<" Rectangle [["<<r1.getXpos()<<","<<r1.getYpos()<<"],width="<<r1.getWidth()<<",height="<<r1.getHeight()<<"] contains point ("<<xpos<<","<<ypos<<")";
else
cout<<" Rectangle [["<<r1.getXpos()<<","<<r1.getYpos()<<"],width="<<r1.getWidth()<<",height="<<r1.getHeight()<<"] does not contain point ("<<xpos<<","<<ypos<<")";
Rectangle2D r2(4,5,10.5,3.2);
if (r1.contains(r2))
cout<<" Rectangle [["<<r1.getXpos()<<","<<r1.getYpos()<<"],width="<<r1.getWidth()<<",height="<<r1.getHeight()<<"] contains Rectangle [["<<r2.getXpos()<<","<<r2.getYpos()<<"],width="<<r2.getWidth()<<",height="<<r2.getHeight()<<"]";
else
cout<<" Rectangle [["<<r1.getXpos()<<","<<r1.getYpos()<<"],width="<<r1.getWidth()<<",height="<<r1.getHeight()<<"] does not contain Rectangle [["<<r2.getXpos()<<","<<r2.getYpos()<<"],width="<<r2.getWidth()<<",height="<<r2.getHeight()<<"]";
return 0;
}
/*
output:
Enter the xpos,ypos,width and height of the rectangle: 2 2 5.5 4.9
Perimeter of Rectangle : 20.8
Area of Rectangle : 26.95
Rectangle [[2,2],width=5.5,height=4.9] contains point (3,3)
Rectangle [[2,2],width=5.5,height=4.9] does not contain Rectangle [[4,5],width=10.5,height=3.2]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.