The code must be in c++ Create a class rectangle that represents a rectangle on
ID: 652119 • Letter: T
Question
The code must be in c++
Create a class rectangle that represents a rectangle on a cartesian plane. To simplify things, we will assume that the rectangle is always aligned with the x and y axis(one pair of sides is always perfectly vertical, the other pair is always horizontal). The job is to create a rectagle class that we could use to solve the problems with.
You should build Rectangle using composition. Create code that compiles against this point class file.
#include
#include
using namespace std;
#include "Point.h"
Point::Point() {
x = 0;
y = 0;
}
Point::Point(double startX, double startY) {
x = startX;
y = startY;
}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
void Point::setX(double newX) {
x = newX;
}
void Point::setY(double newY) {
y = newY;
}
void Point::translate(double xShift, double yShift) {
x += xShift;
y += yShift;
}
double Point::distanceTo(const Point& other) const {
double xDiff = x - other.x;
double yDiff = y - other.y;
return sqrt( pow(xDiff, 2) + pow(yDiff, 2) );
}
bool Point::isSameAs(const Point& other) const {
const double EPSILON = 0.0000001;
if( (abs(x - other.x) < EPSILON) &&
(abs(y - other.y) < EPSILON) )
return true;
else
return false;
}
void Point::print() const {
cout << "Point at (" << x << "," << y << ")" << endl;
}
here is a ulm diagram of what you're building
Explanation / Answer
Class Rectangle:public Point
{
double height, width;
Rectangle(Point& p1, Point& p2);
Rectangle(Point& p1,double height, double width)
{
Height = p1.getX();
Width = p1.getY();
}
Point getUpperLeftVertex();
double getWidth()
{
return Width;
}
double getHeight()
{
return Height;
}
double getArea()
{
return (Height*Width);
}
double getPerimeter()
{
return (2*(Height+Width));
}
Point getCenter()
{
point p3;
p3.getX();
p3.getY()
return p3;
}
Shift(double xShift, double yShift)
{
Height = Height +xShift;
Width = Width +yShift;
}
bool Contains(Point p)
{
int x = p.getX();
int y = p.getY();
}
}
public void main()
{
Point p1, p2, p3;
Rectangle r1;
int Area, Perimeter;
p3 = r1.getCenter();
Area = r1.getArea();
Perimeter = r1.getPerimeter();
r1.Shift(2.66, 7.54);
}
}