150 lines
3.4 KiB
C++
150 lines
3.4 KiB
C++
#include <iostream>
|
|
#include <list>
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <iomanip>
|
|
#include <vector>
|
|
#include <cmath>
|
|
|
|
using string = std::string;
|
|
using slist = std::list<string>;
|
|
|
|
//Place in header!
|
|
template <typename T>
|
|
void printm(T m){
|
|
std::cout << m;
|
|
}
|
|
|
|
template <typename T>
|
|
void printlm(T m){
|
|
printm(m);
|
|
printm("\n");
|
|
}
|
|
|
|
class Point {
|
|
public:
|
|
const double x;
|
|
const double y;
|
|
Point(double a, double b) : x(a), y(b) {}
|
|
};
|
|
|
|
double dist(Point a, Point b) {
|
|
const auto dx = a.x - b.x;
|
|
const auto dy = a.y - b.y;
|
|
return sqrt(dx*dx + dy*dy);
|
|
}
|
|
|
|
std::string printp(const Point &p) {
|
|
return "( " + std::to_string(p.x) + " , " + std::to_string(p.y) + " )";
|
|
}
|
|
|
|
class Shape {
|
|
public:
|
|
virtual bool contains(Point p) const = 0;
|
|
virtual std::string description() const = 0;
|
|
virtual void scale(double s) = 0;
|
|
virtual ~Shape() {}
|
|
};
|
|
class Circle : public Shape {
|
|
public:
|
|
Point center;
|
|
double radius;
|
|
Circle(Point c, double r) : center(c), radius(r) {}
|
|
virtual bool contains(Point p) const override {
|
|
return dist(center, p) <= radius;
|
|
}
|
|
virtual std::string description() const override {
|
|
return "Circle ; " + printp(center) + " ; " + std::to_string(radius);
|
|
}
|
|
virtual void scale(double s) override {
|
|
radius *= s;
|
|
}
|
|
};
|
|
class Rectangle : public Shape {
|
|
public:
|
|
Point topleft;
|
|
double width, height;
|
|
Rectangle(Point tl, double w, double h) : topleft(tl), width(w), height(h) {}
|
|
virtual bool contains(Point p) const override {
|
|
return topleft.x <= p.x && topleft.x + width >= p.x && topleft.y <= p.y && topleft.y + height >= p.y;
|
|
}
|
|
virtual std::string description() const override {
|
|
return "Rectangle ; " + printp(topleft) + " ; [ " + std::to_string(width) + " , " + std::to_string(height) + " ]";
|
|
}
|
|
virtual void scale(double s) override {
|
|
width *= s;
|
|
height *= s;
|
|
}
|
|
};
|
|
class CompositeShape : public Shape {
|
|
public:
|
|
Shape* shape1;
|
|
Shape* shape2;
|
|
CompositeShape(Shape* s1, Shape* s2) : shape1(s1), shape2(s2) {}
|
|
virtual bool contains(Point p) const override {
|
|
return shape1->contains(p) || shape2->contains(p);
|
|
}
|
|
virtual std::string description() const override {
|
|
return "Composite ; " + shape1->description() + " ; " + shape2->description();
|
|
}
|
|
virtual void scale(double s) override {
|
|
shape1->scale(s);
|
|
shape2->scale(s);
|
|
}
|
|
};
|
|
|
|
int prompti(string,bool*);
|
|
double promptd(string,bool*);
|
|
string prompts(string,bool*);
|
|
|
|
int main() {
|
|
std::vector<Shape*> s {};
|
|
printlm("Creating Cirlces...");
|
|
for (int i = 0; i <= 10; ++i) s.push_back(new Circle(Point(0, i), 10));
|
|
printlm("Circles with the point (5, 12) within them:");
|
|
for (int i = 0; i < s.size(); i++) if (s[i]->contains(Point(5, 12))) printlm(i);
|
|
printlm("Destroying Circles...");
|
|
while (s.size() > 0) {delete s.back(); s.pop_back();}
|
|
Circle c {Poi}
|
|
return 0;
|
|
}
|
|
|
|
int prompti(string message, bool *status) {
|
|
int tr {};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return 0;
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|
|
|
|
double promptd(string message, bool *status) {
|
|
double tr {};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return 0.0;
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|
|
|
|
string prompts(string message, bool *status) {
|
|
string tr {""};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return "";
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|