Skip to content

Instantly share code, notes, and snippets.

@Battleroid
Last active December 14, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Battleroid/5088002 to your computer and use it in GitHub Desktop.
Save Battleroid/5088002 to your computer and use it in GitHub Desktop.
Exercise surrounding object 'Rectangle2D'. Created several constructors, functions for finding overlapping parts, containing other items, as well as the area and perimeter. Of course, mutation and retrieval functions are included.
#include "11_09.h"
#include <iostream>
using namespace std;
int main () {
// objects
Rectangle2D r1(2, 2, 5.5, 4.9);
Rectangle2D r2(4, 5, 10.5, 3.2);
Rectangle2D r3(3, 5, 2.3, 5.4);
cout << "R1 Area: " << r1.getArea() << endl;
cout << "R1 Perimeter: " << r1.getPerimeter() << endl;
if (r1.contains(3, 3))
cout << "R1 contains the points (3, 3)!" << endl;
else
cout << "R1 does not contain the points (3, 3)!" << endl;
if (r1.contains(r2))
cout << "R1 contains R2!" << endl;
else
cout << "R1 does not contain R2!" << endl;
if (r1.overlaps(r3))
cout << "R1 overlaps R3!" << endl;
else
cout << "R1 does not overlap R3!" << endl;
// windows only
system("pause");
return 0;
}
#include "11_09.h"
#include <iostream>
#include <cmath>
using namespace std;
// constructors
Rectangle2D::Rectangle2D() {
setX(0);
setY(0);
setWidth(1);
setHeight(1);
}
Rectangle2D::Rectangle2D(double x, double y, double width, double height) {
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
// mutate
void Rectangle2D::setX(double x) {
this->x = x;
}
void Rectangle2D::setY(double y) {
this->y = y;
}
void Rectangle2D::setWidth(double width) {
this->width = width;
}
void Rectangle2D::setHeight(double height) {
this->height = height;
}
// get
double Rectangle2D::getX() const {
return x;
}
double Rectangle2D::getY() const {
return y;
}
double Rectangle2D::getWidth() const {
return width;
}
double Rectangle2D::getHeight() const {
return height;
}
// functions
double Rectangle2D::getArea() const {
return (width * height);
}
double Rectangle2D::getPerimeter() const {
return ((width * 2) + (height * 2));
}
bool Rectangle2D::contains(double x, double y) const {
// you get the boundaries by halving the width and adding or subtracting the associated coordinate
if ((x >= (this->x - (this->width / 2.0)) && x <= (this->x + (this->width / 2.0))) && (y >= (this->y - (this->height / 2.0)) && y <= (this->y + (this->height / 2.0)))) // could the () around each x/y be a problem?
return true;
else
return false;
}
bool Rectangle2D::contains(const Rectangle2D &r) const {
const double x = r.getX();
const double y = r.getY();
const double width = r.getWidth();
const double height = r.getHeight();
if ((x + (.5*width)) < (this->x + (.5*this->width)) && (x - (.5*width)) > (this->x - (.5*this->width)) &&
(y + (.5*height)) < (this->y + (.5*this->height)) && (y - (.5*height)) > (this->y - (.5*this->height)))
return true;
else
return false;
}
bool Rectangle2D::overlaps(const Rectangle2D &r) const {
// check if points (tl, tr, bl, br) are in parent rectangle
if (this->contains((r.getX() - (r.getWidth() / 2.0)), ((r.getX() - (r.getWidth() / 2.0)) + (r.getHeight() / 2.0)))) {
return true; // top left
} else if (this->contains((r.getX() + (r.getWidth() / 2.0)), (r.getX() + (r.getWidth() / 2.0)) + (r.getHeight() / 2.0))) {
return true; // top right
} else if (this->contains((r.getX() - (r.getWidth() / 2.0)), (r.getX() - (r.getWidth() / 2.0)) - (r.getHeight() / 2.0))) {
return true; // bottom left
} else if (this->contains((r.getX() + (r.getWidth() / 2.0)), (r.getX() + (r.getWidth() / 2.0)) - (r.getHeight() / 2.0))) {
return true; // bottom right
}
return false; // not inside
}
#ifndef RECTANGLE2D_H
#define RECTANGLE2D_H
using namespace std;
class Rectangle2D {
public:
// constructors
Rectangle2D(); // 0, 0, 1, 1
Rectangle2D(double x, double y, double width, double height); // custom
// functions
double getArea() const;
double getPerimeter() const;
bool contains(double x, double y) const;
bool contains(const Rectangle2D &r) const;
bool overlaps(const Rectangle2D &r) const;
// mutate
void setX(double x);
void setY(double y);
void setWidth(double width);
void setHeight(double height);
// get
double getX() const;
double getY() const;
double getWidth() const;
double getHeight() const;
private:
double x;
double y;
double width;
double height;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment