C_CPP/05_OO/MP/shapes_main.cpp

161 lines
3.7 KiB
C++
Raw Normal View History

#include "../../helpers/AnsiConsole.h"
2018-03-09 09:47:53 +01:00
#include <cmath>
2018-08-28 15:18:47 +02:00
#include <vector>
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
struct Position {
int x;
int y;
explicit Position(int x_ = 0, int y_ = 0) {
2018-06-14 16:22:51 +02:00
x = x_;
y = y_;
}
2018-03-09 09:47:53 +01:00
};
2018-08-28 15:18:47 +02:00
class Shape {
2018-03-09 09:47:53 +01:00
protected:
2018-08-28 15:18:47 +02:00
Position position;
Colors color;
2018-03-09 09:47:53 +01:00
public:
2018-08-28 15:18:47 +02:00
Shape(Position position, Colors color);
virtual ~Shape();
virtual void draw() = 0;
};
2018-06-14 16:22:51 +02:00
2018-08-28 15:18:47 +02:00
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;
2018-03-09 09:47:53 +01:00
};
2018-08-28 15:18:47 +02:00
Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) {
2018-03-09 09:47:53 +01:00
}
2018-06-14 16:22:51 +02:00
void Point::draw() {
2018-08-28 15:18:47 +02:00
ansiConsole.printText(position.x, position.y, "*", color);
2018-03-09 09:47:53 +01:00
}
2018-08-28 15:18:47 +02:00
class Circle : public Shape{
2018-03-09 09:47:53 +01:00
protected:
2018-06-14 16:22:51 +02:00
int _radius;
2018-03-09 09:47:53 +01:00
public:
2018-08-28 15:18:47 +02:00
explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN);
2018-06-14 16:22:51 +02:00
2018-08-28 15:18:47 +02:00
void draw() override;
2018-03-09 09:47:53 +01:00
};
2018-08-28 15:18:47 +02:00
Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) {
2018-06-14 16:22:51 +02:00
_radius = radius;
2018-03-09 09:47:53 +01:00
}
2018-06-14 16:22:51 +02:00
void Circle::draw() {
/* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie
* Höhensatz des Euklid
* */
2018-08-28 15:18:47 +02:00
int x_start = position.x - _radius / 2;
int x_stop = position.x + _radius / 2;
2018-06-14 16:22:51 +02:00
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));
2018-08-28 15:18:47 +02:00
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);
2018-06-14 16:22:51 +02:00
}
}
2018-08-28 15:18:47 +02:00
class Rectangle : public Shape{
2018-06-14 16:22:51 +02:00
protected:
int width;
int height;
public:
2018-08-28 15:18:47 +02:00
explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE);
void draw() override;
2018-06-14 16:22:51 +02:00
};
2018-08-28 15:18:47 +02:00
Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) {
2018-06-14 16:22:51 +02:00
this->width = width;
this->height = height;
}
void Rectangle::draw() {
int x_start = position.x - (width / 2);
int x_stop = position.x + (width / 2);
2018-06-14 16:22:51 +02:00
int y_start = position.y - (height / 2);
int y_stop = position.y + (height / 2);
for(int i = x_start; i <= x_stop; i++) {
2018-08-28 15:18:47 +02:00
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++) {
2018-08-28 15:18:47 +02:00
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();
}
2018-03-09 09:47:53 +01:00
}
2018-08-28 15:18:47 +02:00
Scene::~Scene() {
for(Shape* shape : shapes) {
delete shape;
}
}
2018-06-14 16:22:51 +02:00
int main(int argc, char **argv) {
2018-08-28 15:18:47 +02:00
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));
2018-03-09 09:47:53 +01:00
2018-08-28 15:18:47 +02:00
shapes.push_back(new Circle(30, 10, 10, Colors::WHITE));
shapes.push_back(new Circle(30, 20, 15, Colors::GREEN));
2018-03-09 09:47:53 +01:00
2018-08-28 15:18:47 +02:00
shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA));
2018-03-09 09:47:53 +01:00
2018-08-28 15:18:47 +02:00
auto *s = new Scene(shapes);
s->draw();
2018-08-28 15:18:47 +02:00
delete s;
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
return 0;
2018-03-09 09:47:53 +01:00
}