06_POLY_MP: Modifikationen durchgefügt
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
cd1fc54c84
commit
65ea034511
|
@ -1,2 +1,31 @@
|
|||
add_executable(06_POLY_MP MP/shapes_main.cpp ../helpers/AnsiConsole.cpp)
|
||||
add_executable(06_POLY_Testat Testat/shapes_main.cpp ../helpers/AnsiConsole.cpp)
|
||||
add_executable(06_POLY_MP
|
||||
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
|
||||
)
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -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,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;
|
||||
}
|
|
@ -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
|
|
@ -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,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();
|
||||
}
|
|
@ -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
|
|
@ -1,139 +1,11 @@
|
|||
#include "../../helpers/AnsiConsole.h"
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include "Shape.h"
|
||||
#include "Point.h"
|
||||
#include "Circle.h"
|
||||
#include "Rectangle.h"
|
||||
#include "Scene.h"
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
void invokeVirtually(Shape* shape);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
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 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);
|
||||
s->draw();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
|
@ -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,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
|
|
@ -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;
|
||||
|
@ -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 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);
|
||||
s->draw();
|
||||
|
@ -157,4 +31,4 @@ int main(int argc, char **argv) {
|
|||
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue