Advertisement
NelloRizzo

Canvas

Mar 20th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. // Canvas.h
  2. class Canvas
  3. {
  4. private:
  5.     // Matrice di punti.
  6.     bool points[24][80];
  7.  
  8. public:
  9.     // Costruttore.
  10.     Canvas();
  11.     // Distruttore.
  12.     virtual ~Canvas();
  13.     // Accende un punto ad una determinata coordinata specificata dai parametri.
  14.     void set(int, int);
  15.     // Spegne un punto ad una determinata coordinata specificata dai parametri.
  16.     void reset(int, int);
  17.     // Pulisce il canovaccio;
  18.     void clear();
  19.     // Controlla se un punto รจ acceso.
  20.     bool isSet(int, int) const;
  21. };
  22.  
  23. // Canvas.cpp
  24.  
  25. Canvas::Canvas() : points()
  26. {
  27.     clear();
  28. }
  29.  
  30.  
  31. Canvas::~Canvas() {}
  32.  
  33. void Canvas::set(int x, int y)
  34. {
  35.     if (x < 0 || x > 79 || y < 0 || y > 23) return;
  36.     points[y][x] = true;
  37. }
  38.  
  39. void Canvas::reset(int x, int y)
  40. {
  41.     if (x < 0 || x > 79 || y < 0 || y > 23) return;
  42.     points[y][x] = false;
  43. }
  44.  
  45. void Canvas::clear()
  46. {
  47.     for (int y = 0; y < 24; ++y)
  48.         for (int x = 0; x < 80; ++x)
  49.             reset(x, y);
  50. }
  51.  
  52. bool Canvas::isSet(int x, int y) const {
  53.     if (x < 0 || x > 79 || y < 0 || y > 23) return false;
  54.     return points[y][x];
  55. }
  56.  
  57. // main
  58. #include "Canvas.h"
  59. #include "Shape.h"
  60. #include "Rectangle.h"
  61. #include "Square.h"
  62. #include "Ellipse.h"
  63. #include "Circle.h"
  64.  
  65. void drawShapeOnCanvas(Shape& s, Canvas& c) {
  66.     s.draw(c);
  67. }
  68.  
  69. void printCanvas(Canvas& c) {
  70.     for (int y = 0; y < 24; ++y) {
  71.         for (int x = 0; x < 80; ++x)
  72.             cout << (c.isSet(x, y) ? '*' : ' ');
  73.         cout << endl;
  74.     }
  75. }
  76. int main()
  77. {
  78.     Canvas c;
  79.     c.set(10, 10);
  80.     c.set(30, 15);
  81.     printCanvas(c);
  82.     Rectangle r(4, 4, 20, 15);
  83.     drawShapeOnCanvas(r, c);
  84.     Square s(4, 4, 15);
  85.     drawShapeOnCanvas(s, c);
  86.     Ellipse e(30, 12, 25, 10);
  87.     Circle ci(30, 12, 10);
  88.     drawShapeOnCanvas(e, c);
  89.     drawShapeOnCanvas(ci, c);
  90.     printCanvas(c);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement