C_CPP/05_OO/MP/shapes_main.cpp

131 lines
2.9 KiB
C++
Raw Normal View History

2018-03-09 09:47:53 +01:00
#include <iostream>
#include <cmath>
2018-08-19 18:30:13 +02:00
#include "../../helpers/AnsiConsole.h"
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
struct Position {
int x;
int y;
Position(int x_ = 0, int y_ = 0) {
x = x_;
y = y_;
}
2018-03-09 09:47:53 +01:00
};
2018-06-14 16:22:51 +02:00
class Point {
2018-03-09 09:47:53 +01:00
protected:
2018-06-14 16:22:51 +02:00
Position _position;
2018-03-09 09:47:53 +01:00
public:
2018-06-14 16:22:51 +02:00
Point(int x = 0, int y = 0);
void draw();
2018-03-09 09:47:53 +01:00
};
2018-06-14 16:22:51 +02:00
Point::Point(int x, int y) {
_position = Position(x, y);
2018-03-09 09:47:53 +01:00
}
2018-06-14 16:22:51 +02:00
void Point::draw() {
ansiConsole.printText(_position.x, _position.y, "*", Colors::RED);
2018-03-09 09:47:53 +01:00
}
2018-06-14 16:22:51 +02:00
class Circle {
2018-03-09 09:47:53 +01:00
protected:
2018-06-14 16:22:51 +02:00
Position _position;
int _radius;
2018-03-09 09:47:53 +01:00
public:
2018-06-14 16:22:51 +02:00
Circle(int x = 0, int y = 0, int radius = 0);
void draw();
2018-03-09 09:47:53 +01:00
};
2018-06-14 16:22:51 +02:00
Circle::Circle(int x, int y, int radius) {
_position = Position(x, y);
_radius = radius;
2018-03-09 09:47:53 +01:00
}
2018-06-14 16:22:51 +02:00
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,
_position.y + h, "#",
Colors::GREEN);
ansiConsole.printText(_position.x + int(x_relative) - _radius / 2,
_position.y - h, "#",
Colors::GREEN);
}
}
class Rectangle {
protected:
Position position;
int width;
int height;
public:
Rectangle(int x = 0, int y = 0, int width = 0, int height = 0);
void draw();
};
Rectangle::Rectangle(int x, int y, int width, int height) {
position = Position(x, y);
this->width = width;
this->height = height;
}
void Rectangle::draw() {
int x_start = position.x - (width / 2);
int x_stop = position.x + (width / 2);
2018-06-14 16:22:51 +02:00
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(position.x, position.y - (height / 2), "#");
ansiConsole.printText(position.x, position.y + (height / 2), "#");
}
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, "#");
}
2018-03-09 09:47:53 +01:00
}
2018-06-14 16:22:51 +02:00
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!");
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
Point *p = new Point(10, 10);
p->draw();
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
Point *p2 = new Point(2, 10);
p2->draw();
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
Circle *c = new Circle(30, 15, 10);
c->draw();
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
Point *p3 = new Point(30, 15);
p3->draw();
2018-03-09 09:47:53 +01:00
Rectangle *r = new Rectangle(20, 20, 20, 20);
r->draw();
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
delete p;
delete p2;
delete p3;
delete c;
delete r;
2018-03-09 09:47:53 +01:00
2018-06-14 16:22:51 +02:00
return 0;
2018-03-09 09:47:53 +01:00
}