C_CPP/05_OO/shapes_main.cpp

114 lines
2.2 KiB
C++
Raw Normal View History

2018-03-09 09:47:53 +01:00
#include <iostream>
#include <cmath>
2018-06-14 16:22:51 +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() {
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
2018-06-14 16:22:51 +02:00
delete p;
delete p2;
delete p3;
delete c;
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
}