Grundgerüst für 05_OO_a fertig
This commit is contained in:
parent
cfeaa39e8a
commit
b074da8be3
|
@ -9,3 +9,4 @@ cmake-build-debug
|
|||
CMakeCache.txt
|
||||
cmake_install.cmake
|
||||
Makefile
|
||||
/C
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
#include <utility>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#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<int>(_position.y + h), "#",
|
||||
Colors::GREEN);
|
||||
ansiConsole.printText(_position.x + int(x_relative) - _radius / 2,
|
||||
static_cast<int>(_position.y - h), "#",
|
||||
Colors::GREEN);
|
||||
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 {
|
||||
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<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) {
|
||||
// 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<Shape*> 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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue