06_POLY_MP: Modifikationen durchgefügt

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2018-11-16 14:58:33 +01:00
parent cd1fc54c84
commit 65ea034511
25 changed files with 464 additions and 272 deletions

View File

@ -1,2 +1,31 @@
add_executable(06_POLY_MP MP/shapes_main.cpp ../helpers/AnsiConsole.cpp) add_executable(06_POLY_MP
add_executable(06_POLY_Testat Testat/shapes_main.cpp ../helpers/AnsiConsole.cpp) MP/shapes_main.cpp
MP/Shape.cpp
MP/Position.h
MP/Shape.h
MP/Point.cpp
MP/Point.h
MP/Circle.cpp
MP/Circle.h
MP/Rectangle.cpp
MP/Rectangle.h
MP/Scene.cpp
MP/Scene.h
../helpers/AnsiConsole.cpp
)
add_executable(06_POLY_Testat
Testat/shapes_main.cpp
Testat/Shape.cpp
Testat/Position.h
Testat/Shape.h
Testat/Point.cpp
Testat/Point.h
Testat/Circle.cpp
Testat/Circle.h
Testat/Rectangle.cpp
Testat/Rectangle.h
Testat/Scene.cpp
Testat/Scene.h
../helpers/AnsiConsole.cpp
)

34
src/06_POLY/MP/Circle.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "Circle.h"
#include <cmath>
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);
}
}
void Circle::nonVirtual() {
std::cout << "Circle" << std::endl;
}
void Circle::virtualVoid() {
std::cout << "Circle" << std::endl;
}

16
src/06_POLY/MP/Circle.h Normal file
View File

@ -0,0 +1,16 @@
#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;
void nonVirtual();
void virtualVoid() override;
};
#endif //C_C_CIRCLE_H

8
src/06_POLY/MP/Point.cpp Normal file
View File

@ -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);
}

14
src/06_POLY/MP/Point.h Normal file
View File

@ -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

14
src/06_POLY/MP/Position.h Normal file
View File

@ -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

View File

@ -0,0 +1,31 @@
#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);
}
}
void Rectangle::nonVirtual() {
std::cout << "Rectangle" << std::endl;
}
void Rectangle::virtualVoid() {
std::cout << "Rectangle" << std::endl;
}

View File

@ -0,0 +1,20 @@
#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;
void nonVirtual();
void virtualVoid() override;
};
#endif //C_C_RECTANGLE_H

19
src/06_POLY/MP/Scene.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Scene.h"
#include <vector>
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;
}
}

17
src/06_POLY/MP/Scene.h Normal file
View File

@ -0,0 +1,17 @@
#include <vector>
#include "Shape.h"
#ifndef C_C_SCENE_H
#define C_C_SCENE_H
class Scene {
private:
std::vector<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
#endif //C_C_SCENE_H

21
src/06_POLY/MP/Shape.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "Position.h"
#include "Shape.h"
Shape::Shape(Position position, Colors color) {
this->position = position;
this->color = color;
}
Shape::~Shape() = default;
void Shape::nonVirtual() {
std::cout << "Shape" << std::endl;
}
void Shape::virtualVoid() {
std::cout << "Shape" << std::endl;
}
void Shape::callVirtual() {
virtualVoid();
}

20
src/06_POLY/MP/Shape.h Normal file
View File

@ -0,0 +1,20 @@
#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;
void nonVirtual();
virtual void virtualVoid();
void callVirtual();
};
#endif

View File

@ -1,139 +1,11 @@
#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector> #include <vector>
#include "Shape.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Scene.h"
struct Position { void invokeVirtually(Shape* shape);
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) { int main(int argc, char **argv) {
std::vector<Shape*> shapes; std::vector<Shape*> shapes;
@ -149,12 +21,36 @@ int main(int argc, char **argv) {
shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA));
shapes.push_back(new Point(5, 10, Colors::WHITE));
shapes.push_back(new Point(15, 7, Colors::WHITE));
shapes.push_back(new Point(7, 5, Colors::WHITE));
auto *s = new Scene(shapes); auto *s = new Scene(shapes);
s->draw(); s->draw();
delete s; delete s;
invokeVirtually(new Circle(1, 1, 1, Colors::RED));
invokeVirtually(new Rectangle(1, 1, 1, 1, Colors::BLACK));
invokeVirtually(new Point(1, 1, Colors::GREEN));
std::cout << "-------------------------------" << std::endl;
auto * c = new Circle(1, 1, 1, Colors::RED);
c->callVirtual();
auto * r = new Rectangle(1, 1, 1, 1, Colors::BLACK);
r->callVirtual();
auto * p = new Point(1, 1, Colors::GREEN);
p->callVirtual();
return 0; return 0;
} }
void invokeVirtually(Shape* shape) {
if(auto * c = dynamic_cast<Circle*>(shape))
c->nonVirtual();
else if(auto * r = dynamic_cast<Rectangle*>(shape))
r->nonVirtual();
else shape->nonVirtual();
}

View File

@ -0,0 +1,26 @@
#include "Circle.h"
#include <cmath>
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);
}
}

View File

@ -0,0 +1,14 @@
#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

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -0,0 +1,19 @@
#include "Scene.h"
#include <vector>
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;
}
}

View File

@ -0,0 +1,17 @@
#include <vector>
#include "Shape.h"
#ifndef C_C_SCENE_H
#define C_C_SCENE_H
class Scene {
private:
std::vector<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
#endif //C_C_SCENE_H

View File

@ -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;

View File

@ -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

View File

@ -1,139 +1,9 @@
#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector> #include <vector>
#include "Shape.h"
struct Position { #include "Point.h"
int x; #include "Circle.h"
int y; #include "Rectangle.h"
#include "Scene.h"
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) { int main(int argc, char **argv) {
std::vector<Shape*> shapes; std::vector<Shape*> shapes;
@ -149,6 +19,10 @@ int main(int argc, char **argv) {
shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA));
shapes.push_back(new Point(5, 10, Colors::WHITE));
shapes.push_back(new Point(15, 7, Colors::WHITE));
shapes.push_back(new Point(7, 5, Colors::WHITE));
auto *s = new Scene(shapes); auto *s = new Scene(shapes);
s->draw(); s->draw();