Grundgerüst für 05_OO_a fertig

This commit is contained in:
Johannes Theiner 2018-08-28 15:18:47 +02:00
parent cfeaa39e8a
commit b074da8be3
3 changed files with 95 additions and 54 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ cmake-build-debug
CMakeCache.txt CMakeCache.txt
cmake_install.cmake cmake_install.cmake
Makefile Makefile
/C

View File

@ -1,5 +1,10 @@
#include <utility>
#include <utility>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <vector>
#include "../../helpers/AnsiConsole.h" #include "../../helpers/AnsiConsole.h"
struct Position { struct Position {
@ -12,35 +17,50 @@ struct Position {
} }
}; };
class Point { class Shape {
protected: protected:
Position _position; Position position;
Colors color;
public: public:
explicit Point(int x = 0, int y = 0); Shape(Position position, Colors color);
virtual ~Shape();
void draw(); virtual void draw() = 0;
}; };
Point::Point(int x, int y) { Shape::Shape(Position position, Colors color) {
_position = Position(x, y); 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() { 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: protected:
Position _position;
int _radius; int _radius;
public: 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) { Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) {
_position = Position(x, y);
_radius = radius; _radius = radius;
} }
@ -48,35 +68,33 @@ void Circle::draw() {
/* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie /* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie
* Höhensatz des Euklid * Höhensatz des Euklid
* */ * */
int x_start = _position.x - _radius / 2; int x_start = position.x - _radius / 2;
int x_stop = _position.x + _radius / 2; int x_stop = position.x + _radius / 2;
for (int i = x_start; i <= x_stop; i++) { for (int i = x_start; i <= x_stop; i++) {
double x_relative = double(i) - double(x_start); double x_relative = double(i) - double(x_start);
double h = sqrt(x_relative * (x_stop - x_start - x_relative)); double h = sqrt(x_relative * (x_stop - x_start - x_relative));
ansiConsole.printText(_position.x + int(x_relative) - _radius / 2, ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(_position.y + h), "#", static_cast<int>(position.y + h), "#",
Colors::GREEN); color);
ansiConsole.printText(_position.x + int(x_relative) - _radius / 2, ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(_position.y - h), "#", static_cast<int>(position.y - h), "#",
Colors::GREEN); color);
} }
} }
class Rectangle { class Rectangle : public Shape{
protected: protected:
Position position;
int width; int width;
int height; int height;
public: public:
explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0); explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE);
void draw(); void draw() override;
}; };
Rectangle::Rectangle(int x, int y, int width, int height) { Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) {
position = Position(x, y);
this->width = width; this->width = width;
this->height = height; this->height = height;
} }
@ -89,42 +107,65 @@ void Rectangle::draw() {
int y_stop = position.y + (height / 2); int y_stop = position.y + (height / 2);
for(int i = x_start; i <= x_stop; i++) { for(int i = x_start; i <= x_stop; i++) {
ansiConsole.printText(position.x, position.y - (height / 2), "#"); ansiConsole.printText(i, position.y - (height / 2), "#", color);
ansiConsole.printText(position.x, position.y + (height / 2), "#"); ansiConsole.printText(i, position.y + (height / 2), "#", color);
} }
for(int i = y_start; i< y_stop; i++) { for(int i = y_start; i< y_stop; i++) {
ansiConsole.printText(position.x - (width / 2), position.y, "#"); ansiConsole.printText(position.x + (width / 2), i, "#", color);
ansiConsole.printText(position.x - (width / 2), position.y, "#"); 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) { int main(int argc, char **argv) {
// x=1 and y=1 is the upper left corner std::vector<Shape*> shapes;
// x and y are more like column and row
ansiConsole.printText(5, 5, "Hello, World!");
auto *p = new Point(10, 10); shapes.push_back(new Point(30, 10, Colors::RED));
p->draw(); shapes.push_back(new Point(28, 8, Colors::BLUE));
shapes.push_back(new Point(32, 8, Colors::BLUE));
auto *p2 = new Point(2, 10);
p2->draw();
auto *c = new Circle(30, 15, 10); shapes.push_back(new Circle(30, 10, 10, Colors::WHITE));
c->draw(); shapes.push_back(new Circle(30, 20, 15, Colors::GREEN));
auto *p3 = new Point(30, 15);
p3->draw();
auto *r = new Rectangle(20, 20, 20, 20);
r->draw();
delete p; shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA));
delete p2;
delete p3;
delete c; auto *s = new Scene(shapes);
delete r; s->draw();
delete s;
return 0; return 0;
} }

View File

@ -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_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_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_MP 06_POLY/MP/main_mp5_POLY.cpp)
#add_executable(06_POLY_Testat 06_POLY/Testat/main_mp5_POLY.cpp) #add_executable(06_POLY_Testat 06_POLY/Testat/main_mp5_POLY.cpp)