From b074da8be3879d53593ac434136d73240a06d557 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 28 Aug 2018 15:18:47 +0200 Subject: [PATCH] =?UTF-8?q?Grundger=C3=BCst=20f=C3=BCr=2005=5FOO=5Fa=20fer?= =?UTF-8?q?tig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 05_OO/MP/shapes_main.cpp | 147 +++++++++++++++++++++++++-------------- CMakeLists.txt | 1 - 3 files changed, 95 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 14d94a4..8e95ce4 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ cmake-build-debug CMakeCache.txt cmake_install.cmake Makefile +/C diff --git a/05_OO/MP/shapes_main.cpp b/05_OO/MP/shapes_main.cpp index b514f01..3e74716 100644 --- a/05_OO/MP/shapes_main.cpp +++ b/05_OO/MP/shapes_main.cpp @@ -1,5 +1,10 @@ +#include + +#include + #include #include +#include #include "../../helpers/AnsiConsole.h" struct Position { @@ -12,35 +17,50 @@ struct Position { } }; -class Point { +class Shape { protected: - Position _position; + Position position; + Colors color; public: - explicit Point(int x = 0, int y = 0); - - void draw(); + Shape(Position position, Colors color); + virtual ~Shape(); + virtual void draw() = 0; }; -Point::Point(int x, int y) { - _position = Position(x, y); +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, "*", Colors::RED); + ansiConsole.printText(position.x, position.y, "*", color); } -class Circle { +class Circle : public Shape{ protected: - Position _position; int _radius; public: - explicit Circle(int x = 0, int y = 0, int radius = 0); + explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN); - void draw(); + void draw() override; }; -Circle::Circle(int x, int y, int radius) { - _position = Position(x, y); +Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) { _radius = radius; } @@ -48,35 +68,33 @@ 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; + 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(_position.y + h), "#", - Colors::GREEN); - ansiConsole.printText(_position.x + int(x_relative) - _radius / 2, - static_cast(_position.y - h), "#", - Colors::GREEN); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y + h), "#", + color); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y - h), "#", + color); } } -class Rectangle { +class Rectangle : public Shape{ protected: - Position position; int width; int height; public: - explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0); - void draw(); + 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) { - position = Position(x, y); +Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) { this->width = width; this->height = height; } @@ -89,42 +107,65 @@ void Rectangle::draw() { int y_stop = position.y + (height / 2); for(int i = x_start; i <= x_stop; i++) { - ansiConsole.printText(position.x, position.y - (height / 2), "#"); - ansiConsole.printText(position.x, position.y + (height / 2), "#"); + 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), position.y, "#"); - ansiConsole.printText(position.x - (width / 2), position.y, "#"); + ansiConsole.printText(position.x + (width / 2), i, "#", color); + ansiConsole.printText(position.x - (width / 2), i, "#", color); } } +class Scene { +private: + std::vector shapes; +public: + explicit Scene(std::vector shapes); + void draw(); + ~Scene(); +}; + +Scene::Scene(std::vector 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) { - // x=1 and y=1 is the upper left corner - // x and y are more like column and row - ansiConsole.printText(5, 5, "Hello, World!"); + std::vector shapes; - auto *p = new Point(10, 10); - p->draw(); - - auto *p2 = new Point(2, 10); - p2->draw(); + 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)); - auto *c = new Circle(30, 15, 10); - c->draw(); - - auto *p3 = new Point(30, 15); - p3->draw(); - - auto *r = new Rectangle(20, 20, 20, 20); - r->draw(); + shapes.push_back(new Circle(30, 10, 10, Colors::WHITE)); + shapes.push_back(new Circle(30, 20, 15, Colors::GREEN)); - delete p; - delete p2; - delete p3; - delete c; - delete r; + shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); + + + auto *s = new Scene(shapes); + s->draw(); + + delete s; + return 0; } diff --git a/CMakeLists.txt b/CMakeLists.txt index 87b88f8..08239cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,6 @@ add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp) add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp) add_executable(05_OO_Testat 05_OO/Testat/shapes_main.cpp helpers/AnsiConsole.cpp) -add_executable(05_OO_Demo 05_OO/MP/AnsiConsoleDemo.cpp helpers/AnsiConsole.cpp) add_executable(06_POLY_MP 06_POLY/MP/main_mp5_POLY.cpp) #add_executable(06_POLY_Testat 06_POLY/Testat/main_mp5_POLY.cpp)