C_CPP/06_POLY/MP/shapes_main.cpp

161 lines
3.7 KiB
C++

#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector>
struct Position {
int x;
int y;
explicit Position(int x_ = 0, int y_ = 0) {
x = x_;
y = y_;
}
};
class Shape {
protected:
Position position;
Colors color;
public:
Shape(Position position, Colors color);
virtual ~Shape();
virtual void draw() = 0;
};
Shape::Shape(Position position, Colors color) {
this->position = position;
this->color = color;
}
Shape::~Shape() = default;
class Point : public Shape{
public:
explicit Point(int x, int y, Colors color);
void draw() override;
};
Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) {
}
void Point::draw() {
ansiConsole.printText(position.x, position.y, "*", color);
}
class Circle : public Shape{
protected:
int _radius;
public:
explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN);
void draw() override;
};
Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) {
_radius = radius;
}
void Circle::draw() {
/* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie
* Höhensatz des Euklid
* */
int x_start = position.x - _radius / 2;
int x_stop = position.x + _radius / 2;
for (int i = x_start; i <= x_stop; i++) {
double x_relative = double(i) - double(x_start);
double h = sqrt(x_relative * (x_stop - x_start - x_relative));
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(position.y + h), "#",
color);
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(position.y - h), "#",
color);
}
}
class Rectangle : public Shape{
protected:
int width;
int height;
public:
explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE);
void draw() override;
};
Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) {
this->width = width;
this->height = height;
}
void Rectangle::draw() {
int x_start = position.x - (width / 2);
int x_stop = position.x + (width / 2);
int y_start = position.y - (height / 2);
int y_stop = position.y + (height / 2);
for(int i = x_start; i <= x_stop; i++) {
ansiConsole.printText(i, position.y - (height / 2), "#", color);
ansiConsole.printText(i, position.y + (height / 2), "#", color);
}
for(int i = y_start; i< y_stop; i++) {
ansiConsole.printText(position.x + (width / 2), i, "#", color);
ansiConsole.printText(position.x - (width / 2), i, "#", color);
}
}
class Scene {
private:
std::vector<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
Scene::Scene(std::vector<Shape*> shapes) : shapes(std::move(shapes)) {
}
void Scene::draw() {
ansiConsole.clearScreen();
for(Shape* shape : shapes) {
shape->draw();
}
}
Scene::~Scene() {
for(Shape* shape : shapes) {
delete shape;
}
}
int main(int argc, char **argv) {
std::vector<Shape*> shapes;
shapes.push_back(new Point(30, 10, Colors::RED));
shapes.push_back(new Point(28, 8, Colors::BLUE));
shapes.push_back(new Point(32, 8, Colors::BLUE));
shapes.push_back(new Circle(30, 10, 10, Colors::WHITE));
shapes.push_back(new Circle(30, 20, 15, Colors::GREEN));
shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA));
auto *s = new Scene(shapes);
s->draw();
delete s;
return 0;
}