From 5b458386acd17a39f346d8f550343f793b06ddb9 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 26 Oct 2018 13:31:39 +0200 Subject: [PATCH] 05_OO: in mehrere Dateien aufgeteilt Signed-off-by: Johannes Theiner --- src/05_OO/MP/Circle.cpp | 26 +++++++ src/05_OO/MP/Circle.h | 15 ++++ src/05_OO/MP/Point.cpp | 8 ++ src/05_OO/MP/Point.h | 14 ++++ src/05_OO/MP/Position.h | 14 ++++ src/05_OO/MP/Rectangle.cpp | 23 ++++++ src/05_OO/MP/Rectangle.h | 18 +++++ src/05_OO/MP/Scene.cpp | 19 +++++ src/05_OO/MP/Scene.h | 17 +++++ src/05_OO/MP/Shape.cpp | 9 +++ src/05_OO/MP/Shape.h | 17 +++++ src/05_OO/MP/shapes_main.cpp | 140 ++--------------------------------- src/CMakeLists.txt | 2 +- src/helpers/AnsiConsole.h | 3 + 14 files changed, 189 insertions(+), 136 deletions(-) create mode 100644 src/05_OO/MP/Circle.cpp create mode 100644 src/05_OO/MP/Circle.h create mode 100644 src/05_OO/MP/Point.cpp create mode 100644 src/05_OO/MP/Point.h create mode 100644 src/05_OO/MP/Position.h create mode 100644 src/05_OO/MP/Rectangle.cpp create mode 100644 src/05_OO/MP/Rectangle.h create mode 100644 src/05_OO/MP/Scene.cpp create mode 100644 src/05_OO/MP/Scene.h create mode 100644 src/05_OO/MP/Shape.cpp create mode 100644 src/05_OO/MP/Shape.h diff --git a/src/05_OO/MP/Circle.cpp b/src/05_OO/MP/Circle.cpp new file mode 100644 index 0000000..5ef58f7 --- /dev/null +++ b/src/05_OO/MP/Circle.cpp @@ -0,0 +1,26 @@ +#include "Circle.h" +#include + +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(position.y + h), "#", + color); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y - h), "#", + color); + + } +} diff --git a/src/05_OO/MP/Circle.h b/src/05_OO/MP/Circle.h new file mode 100644 index 0000000..4a359ff --- /dev/null +++ b/src/05_OO/MP/Circle.h @@ -0,0 +1,15 @@ +#ifndef C_C_CIRCLE_H +#define C_C_CIRCLE_H + +#include "Shape.h" + +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; +}; + +#endif //C_C_CIRCLE_H diff --git a/src/05_OO/MP/Point.cpp b/src/05_OO/MP/Point.cpp new file mode 100644 index 0000000..77efe5c --- /dev/null +++ b/src/05_OO/MP/Point.cpp @@ -0,0 +1,8 @@ +#include "Point.h" + +Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) { +} + +void Point::draw() { + ansiConsole.printText(position.x, position.y, "*", color); +} diff --git a/src/05_OO/MP/Point.h b/src/05_OO/MP/Point.h new file mode 100644 index 0000000..bc693bf --- /dev/null +++ b/src/05_OO/MP/Point.h @@ -0,0 +1,14 @@ +#ifndef C_C_POINT_H +#define C_C_POINT_H + +#include "Shape.h" + +class Point : public Shape{ + +public: + explicit Point(int x, int y, Colors color); + void draw() override; +}; + + +#endif diff --git a/src/05_OO/MP/Position.h b/src/05_OO/MP/Position.h new file mode 100644 index 0000000..f977d98 --- /dev/null +++ b/src/05_OO/MP/Position.h @@ -0,0 +1,14 @@ +#ifndef C_C_POSITION_H +#define C_C_POSITION_H + +struct Position { + int x; + int y; + + explicit Position(int x_ = 0, int y_ = 0) { + x = x_; + y = y_; + } +}; + +#endif \ No newline at end of file diff --git a/src/05_OO/MP/Rectangle.cpp b/src/05_OO/MP/Rectangle.cpp new file mode 100644 index 0000000..7aaa71a --- /dev/null +++ b/src/05_OO/MP/Rectangle.cpp @@ -0,0 +1,23 @@ +#include "Rectangle.h" + +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); + } +} \ No newline at end of file diff --git a/src/05_OO/MP/Rectangle.h b/src/05_OO/MP/Rectangle.h new file mode 100644 index 0000000..f8775b9 --- /dev/null +++ b/src/05_OO/MP/Rectangle.h @@ -0,0 +1,18 @@ +#include "Shape.h" + +#ifndef C_C_RECTANGLE_H +#define C_C_RECTANGLE_H + + +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; +}; + + +#endif //C_C_RECTANGLE_H diff --git a/src/05_OO/MP/Scene.cpp b/src/05_OO/MP/Scene.cpp new file mode 100644 index 0000000..9d25e7e --- /dev/null +++ b/src/05_OO/MP/Scene.cpp @@ -0,0 +1,19 @@ +#include "Scene.h" +#include + +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; + } +} diff --git a/src/05_OO/MP/Scene.h b/src/05_OO/MP/Scene.h new file mode 100644 index 0000000..90e3352 --- /dev/null +++ b/src/05_OO/MP/Scene.h @@ -0,0 +1,17 @@ +#include +#include "Shape.h" + +#ifndef C_C_SCENE_H +#define C_C_SCENE_H + +class Scene { +private: + std::vector shapes; +public: + explicit Scene(std::vector shapes); + void draw(); + ~Scene(); +}; + + +#endif //C_C_SCENE_H diff --git a/src/05_OO/MP/Shape.cpp b/src/05_OO/MP/Shape.cpp new file mode 100644 index 0000000..6fa576c --- /dev/null +++ b/src/05_OO/MP/Shape.cpp @@ -0,0 +1,9 @@ +#include "Position.h" +#include "Shape.h" + +Shape::Shape(Position position, Colors color) { + this->position = position; + this->color = color; +} + +Shape::~Shape() = default; \ No newline at end of file diff --git a/src/05_OO/MP/Shape.h b/src/05_OO/MP/Shape.h new file mode 100644 index 0000000..2c803d3 --- /dev/null +++ b/src/05_OO/MP/Shape.h @@ -0,0 +1,17 @@ +#ifndef C_C_SHAPE_H +#define C_C_SHAPE_H + +#include "../../helpers/AnsiConsole.h" +#include "Position.h" + +class Shape { +protected: + Position position; + Colors color; +public: + Shape(Position position, Colors color); + virtual ~Shape(); + virtual void draw() = 0; +}; + +#endif \ No newline at end of file diff --git a/src/05_OO/MP/shapes_main.cpp b/src/05_OO/MP/shapes_main.cpp index f09dcb8..f3eaaf1 100644 --- a/src/05_OO/MP/shapes_main.cpp +++ b/src/05_OO/MP/shapes_main.cpp @@ -1,139 +1,9 @@ -#include "../../helpers/AnsiConsole.h" -#include #include - -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(position.y + h), "#", - color); - ansiConsole.printText(position.x + int(x_relative) - _radius / 2, - static_cast(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 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; - } -} +#include "Shape.h" +#include "Point.h" +#include "Circle.h" +#include "Rectangle.h" +#include "Scene.h" int main(int argc, char **argv) { std::vector shapes; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index eaf329a..6397d9a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,7 @@ target_link_libraries(04_UDEF_MP m) add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp) target_link_libraries(04_UDEF_Testat m) -add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp) +add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp 05_OO/MP/Shape.cpp 05_OO/MP/Position.h 05_OO/MP/Shape.h 05_OO/MP/Point.cpp 05_OO/MP/Point.h 05_OO/MP/Circle.cpp 05_OO/MP/Circle.h 05_OO/MP/Rectangle.cpp 05_OO/MP/Rectangle.h 05_OO/MP/Scene.cpp 05_OO/MP/Scene.h) add_executable(05_OO_Testat 05_OO/Testat/shapes_main.cpp helpers/AnsiConsole.cpp) add_executable(06_POLY_MP 06_POLY/MP/shapes_main.cpp helpers/AnsiConsole.cpp) diff --git a/src/helpers/AnsiConsole.h b/src/helpers/AnsiConsole.h index 1d3ad39..45a6b68 100644 --- a/src/helpers/AnsiConsole.h +++ b/src/helpers/AnsiConsole.h @@ -1,3 +1,5 @@ +#ifndef C_C_ANSICONSOLE_H +#define C_C_ANSICONSOLE_H #include @@ -32,3 +34,4 @@ public: extern AnsiConsole ansiConsole; +#endif \ No newline at end of file