#include #include #include #include using namespace std; // Du ska få huvudprogrammet att fungera som specificerat i uppgiften. // Du får skriva dina klasser och all kod direkt här (ingen filuppdelning krävs). // Du får naturligtvis även göra korrekt filuppdelning som du är van vid. 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; }; // Nedan finns given kod. // Du kommer att behöva justera den givna koden enligt de datatyper/klasser du väljer att använda. // Se TODO eller YOUR_TYPE_HERE för de rader vi förutser du kommer behöva justera // hämta symbolen som täcker (x,y) genom att fråga varje form char get_symbol_for(float x, float y, vector< Shape* > 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(vector< Shape* > 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< Shape* > 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}, }; draw( train ); for ( Shape* shape : train ) { delete shape; } return 0; }