05_OO: in mehrere Dateien aufgeteilt
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
6abaf92ecf
commit
5b458386ac
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -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
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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;
|
|
@ -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
|
|
@ -1,139 +1,9 @@
|
|||
#include "../../helpers/AnsiConsole.h"
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
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<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;
|
||||
}
|
||||
}
|
||||
#include "Shape.h"
|
||||
#include "Point.h"
|
||||
#include "Circle.h"
|
||||
#include "Rectangle.h"
|
||||
#include "Scene.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::vector<Shape*> shapes;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#ifndef C_C_ANSICONSOLE_H
|
||||
#define C_C_ANSICONSOLE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
@ -32,3 +34,4 @@ public:
|
|||
extern AnsiConsole ansiConsole;
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue