#include #include #include #include using namespace std; class Shape { public: Shape(float x_pos, float y_pos, char symbol) : x_pos {x_pos}, y_pos{y_pos}, symbol{symbol} { } virtual ~Shape() = default; virtual bool covers(float x, float y) const = 0; char get_symbol() const { return symbol; } protected: float x_pos; float y_pos; char symbol; }; class Rectangle : public Shape { public: Rectangle(float x_pos, float y_pos, char symbol, float width, float height) : Shape{x_pos, y_pos, symbol}, width{width}, height{height} { } bool covers(float x, float y) const override { return (x < x_pos + width && x >= x_pos && y < y_pos + height && y >= y_pos); } private: float width; float height; }; class Circle : public Shape { public: Circle(float x_pos, float y_pos, char symbol, float radius) : Shape{x_pos, y_pos, symbol}, radius{radius} { } bool covers(float x, float y) const override { double dist {pow(x - x_pos, 2) + pow(y - y_pos, 2)}; return dist < pow(radius, 2); } private: float radius; }; //// Extra class Romb : public Shape // egentligen ett parallogram { public: Romb(float x_pos, float y_pos, char symbol, float width, float height, float slope) : Shape{x_pos, y_pos, symbol}, width{width}, height{height}, slope{slope} { } bool covers(float x, float y) const override { if ( y < y_pos + height && y >= y_pos ) { float dy{y - y_pos}; float dx{dy*slope}; return (x < (x_pos + width + dx) && x >= (x_pos + dx)); } return false; } private: float width; float height; float slope; }; using Figure = vector; // döp om en vektor med former som Figure // hämta symbolen som täcker (x,y) genom att fråga varje form char get_symbol_for(float x, float y, Figure const& shape_list) { char to_print {' '}; for ( Shape* s : shape_list ) // för varje form i listan { if ( s->covers(x, y) ) // om formen täcker koordinat (x,y) { to_print = s->get_symbol(); // hämta formens symbol } } return to_print; } // utritning av alla former i en vektor void draw(Figure const& shapes) { const int h {70}, w{80}; // Skriv ut övre linjal cout << " |"; for (int i{}; i < w/10; ++i) cout << " . . . . |"; cout << endl; for(int i {}; i < h; ++i) { // vänster radnumrering cout << setfill('0') << setw(2) << i; // skriv tecken för varje kolumn på raden for(int j{}; j < w; ++j) { cout << get_symbol_for(j/2.0, i, shapes); } // höger radnumrering cout << ' ' << setw(2) << i << endl; } // Skriv ut undre linjal cout << " |"; for (int i{}; i < w/10; ++i) cout << " ' ' ' ' |"; cout << endl; } int main() { vector
figure_list{}; Figure train { new Circle{10,60, 'x', 5}, new Circle{20,60, 'x', 5}, new Circle{30,60, 'x', 5}, new Rectangle{5,49, '#', 31,6}, new Rectangle{25,40, '#', 10,10}, new Rectangle{23,34, '#', 14,6}, new Rectangle{10,43, '#', 4,6}, new Circle{12,38, '%', 3}, new Circle{16,26, ';', 6}, new Circle{28,10, '.', 9}, }; Figure ylogo { new Circle{20,35, 'y', 18}, new Circle{20,35, ' ', 16}, new Romb{24.5,25, '/', 6, 21, -.75}, new Circle{13,28, 'o', 4}, new Rectangle{22,39, 'T', 9,7}, new Rectangle{22,40, ' ', 4,6}, new Rectangle{22,41, 'L', 3,5}, new Rectangle{23,41, ' ', 2,4}, new Rectangle{24.0,41, '<', .5,1}, new Rectangle{24.5,41, '>', .5,1}, new Rectangle{24,42, '.', 1,1}, new Rectangle{24,43, '|', 1,1}, new Rectangle{24,44, '\'', 1,1}, new Rectangle{27,40, ' ', 4,6}, new Rectangle{28,41, 'H', 3,5}, new Rectangle{29,41, ' ', 1,2}, new Rectangle{29,44, ' ', 1,2}, }; figure_list.push_back(train); figure_list.push_back(ylogo); for ( Figure const& figure : figure_list ) { draw(figure); } for ( Figure const& figure : figure_list ) { for ( Shape* shape : figure ) { delete shape; } } }