Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. // graphred.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. enum Color
  10. {
  11.     transparent,
  12.     black,
  13.     white,
  14.     red,
  15.     unknown
  16. };
  17.  
  18. struct Coords
  19. {
  20.     double x, y;
  21.     Coords(double x, double y)
  22.     {
  23.         this->x = x;
  24.         this->y = y;
  25.     }
  26. };
  27.  
  28. class Shape
  29. {
  30. protected:
  31.     Color color;
  32.     Coords centre;
  33.  
  34. public:
  35.     Shape(Color color = transparent, double x = 0.0, double y = 0.0) : centre(x, y)
  36.     {
  37.         this->color = color;
  38.     }
  39.  
  40.     void SetColor(Color newColor)
  41.     {
  42.         this->color = newColor;
  43.     }
  44.  
  45.     virtual void Move(Coords newCentre)
  46.     {
  47.         centre = newCentre;
  48.     }
  49.  
  50.     void Resize(float ratio);
  51.     void Rotate(float angle);
  52.  
  53.     virtual void Draw()//чтобы можно было переопределять далее
  54.     {
  55.         cout << "(" << centre.x << ", " << centre.y << "): " <<
  56.             (color == transparent ? "transparent" :
  57.             (color == red ? "red" :
  58.             (color == black ? "black" :
  59.             (color == white ? "white" : "unknown")))) << endl;
  60.     }
  61. };
  62.  
  63. class Point : Shape
  64. {
  65. public:
  66.     Point(Color color = black, double x = 0.0, double y = 0.0)
  67.         : Shape(color, x, y)
  68.     {   }
  69. };
  70.  
  71. class Line : Shape
  72. {
  73.     Coords A, B;
  74.  
  75. public:
  76.     Line(Color color, double xA, double yA, double xB, double yB)
  77.         : Shape(color, (xB + xA) / 2, (yB + yA) / 2), A(xA, yA), B(xB, yB)
  78.     {   }
  79.  
  80.     void Draw() override
  81.     {
  82.         cout << "Line: (" << A.x << ", " << A.y << ") - (" << B.x << ", " << B.y << "); centre in " << "(" << centre.x << ", " << centre.y << "): " <<
  83.             (color == transparent ? "transparent" :
  84.             (color == red ? "red" :
  85.             (color == black ? "black" :
  86.             (color == white ? "white" : "unknown")))) << endl;
  87.     }
  88.  
  89.     void Move(Coords newCentre) override
  90.     {
  91.         A.x = A.x + (newCentre.x - centre.x);
  92.         A.y = A.y + (newCentre.y - centre.y);
  93.         B.x = B.x + (newCentre.x - centre.x);
  94.         B.y = B.y + (newCentre.y - centre.y);
  95.         centre = newCentre;
  96.     }
  97. };
  98.  
  99. class Triangle : Shape
  100. {
  101.     Coords A, B, C;
  102.  
  103. public:
  104.     Triangle(Color color, double xA, double yA, double xB, double yB, double xC, double yC)
  105.         : Shape(color, (xC + xB + xA) / 3, (yC + yB + yA) / 3), A(xA, yA), B(xB, yB), C(xC, yC)
  106.     {   }
  107.  
  108.     void Draw() override
  109.     {
  110.         cout << "Triangle: (" << A.x << ", " << A.y << ") - (" << B.x << ", " << B.y << ") - (" << C.x << ", " << C.y << "); centre in " << "(" << centre.x << ", " << centre.y << "): " <<
  111.             (color == transparent ? "transparent" :
  112.             (color == red ? "red" :
  113.             (color == black ? "black" :
  114.             (color == white ? "white" : "unknown")))) << endl;
  115.     }
  116.  
  117.     void Move(Coords newCentre) override
  118.     {
  119.         A.x = A.x + (newCentre.x - centre.x);
  120.         A.y = A.y + (newCentre.y - centre.y);
  121.         B.x = B.x + (newCentre.x - centre.x);
  122.         B.y = B.y + (newCentre.y - centre.y);
  123.         C.x = C.x + (newCentre.x - centre.x);
  124.         C.y = C.y + (newCentre.y - centre.y);
  125.         centre = newCentre;
  126.     }
  127. };
  128.  
  129. class Quadrilateral : Shape
  130. {
  131.     Coords A, B, C, D;
  132.  
  133. public:
  134.     Quadrilateral(Color color, double xA, double yA, double xB, double yB, double xC, double yC, double xD, double yD)
  135.         : Shape(color, (xD + xC + xB + xA) / 4, (yD + yC + yB + yA) / 4), A(xA, yA), B(xB, yB), C(xC, yC), D(xD, yD)
  136.     {   }
  137.  
  138.     void Draw() override
  139.     {
  140.         cout << "Quadrilateral: (" << A.x << ", " << A.y << ") - (" << B.x << ", " << B.y << ") - (" << C.x << ", " << C.y << ") - (" << D.x << ", " << D.y << "); centre in " << "(" << centre.x << ", " << centre.y << "): " <<
  141.             (color == transparent ? "transparent" :
  142.             (color == red ? "red" :
  143.             (color == black ? "black" :
  144.             (color == white ? "white" : "unknown")))) << endl;
  145.     }
  146.  
  147.     void Move(Coords newCentre) override
  148.     {
  149.         A.x = A.x + (newCentre.x - centre.x);
  150.         A.y = A.y + (newCentre.y - centre.y);
  151.         B.x = B.x + (newCentre.x - centre.x);
  152.         B.y = B.y + (newCentre.y - centre.y);
  153.         C.x = C.x + (newCentre.x - centre.x);
  154.         C.y = C.y + (newCentre.y - centre.y);
  155.         D.x = D.x + (newCentre.x - centre.x);
  156.         D.y = D.y + (newCentre.y - centre.y);
  157.         centre = newCentre;
  158.     }
  159. };
  160.  
  161. class Circle : Shape
  162. {
  163. public:
  164.     double Radius;
  165.  
  166.     Circle(Color color, double x, double y, double Radius)
  167.         : Shape(color, x, y)
  168.     {   }
  169.  
  170.     void Draw() override
  171.     {
  172.         cout << "Circle: radius=" << Radius << "; centre in " << "(" << centre.x << ", " << centre.y << "): " <<
  173.             (color == transparent ? "transparent" :
  174.             (color == red ? "red" :
  175.             (color == black ? "black" :
  176.             (color == white ? "white" : "unknown")))) << endl;
  177.     }
  178. };
  179.  
  180. int _tmain(int argc, _TCHAR* argv[])
  181. {
  182.     cout << "How many shapes do you want to create?\n";
  183.     int n;
  184.     cin >> n;
  185.     Shape** figures = new Shape*[n];
  186.     for (int i = 0; i < n; i++)
  187.     {
  188.         Color sColor;
  189.         cout << "Choose the color\n0-transparent\n1-black\n2-white\n3-red\n";
  190.         int c;
  191.         cin >> c;
  192.         switch (c)
  193.         {
  194.         case 0: sColor = transparent; break;
  195.         case 1: sColor = black; break;
  196.         case 2: sColor = white; break;
  197.         case 3: sColor = red; break;
  198.         default: sColor = unknown; break;
  199.         }
  200.         cout << "Choose the type of shape\n0-point\n1-line\n2-triangle\n3-quadrilateral\n4-circle\n";
  201.         int t;
  202.         cin >> t;
  203.         switch (t)
  204.         {
  205.         case 0:{ int x, y; cout << "Enter coords\n"; cin >> x >> y; figures[i] = (Shape*) new Point(sColor, x, y); break; }
  206.         case 1:{int x1, y1, x2, y2; cout << "Enter coords of (.)A\n"; cin >> x1 >> y1;
  207.             cout << "Enter coords of (.)B\n"; cin >> x2 >> y2;
  208.             figures[i] = (Shape*) new Line(sColor, x1, y1, x2, y2);
  209.             break; }
  210.         case 2: {int x1, y1, x2, y2, x3, y3; cout << "Enter coords of (.)A\n"; cin >> x1 >> y1;
  211.             cout << "Enter coords of (.)B\n"; cin >> x2 >> y2;
  212.             cout << "Enter coords of (.)C\n"; cin >> x3 >> y3;
  213.             figures[i] = (Shape*) new Triangle(sColor, x1, y1, x2, y2, x3, y3);
  214.             break; }
  215.         case 3: {int x1, y1, x2, y2, x3, y3, x4, y4; cout << "Enter coords of (.)A\n"; cin >> x1 >> y1;
  216.             cout << "Enter coords of (.)B\n"; cin >> x2 >> y2;
  217.             cout << "Enter coords of (.)C\n"; cin >> x3 >> y3;
  218.             cout << "Enter coords of (.)D\n"; cin >> x4 >> y4;
  219.             figures[i] = (Shape*) new Quadrilateral(sColor, x1, y1, x2, y2, x3, y3, x4, y4);
  220.             break; }
  221.         case 4: {int x, y; cout << "Enter coords of centre\n"; cin >> x >> y; cout << "Enter radius\n"; int r; cin >> r;
  222.             figures[i] = (Shape*) new Circle(sColor, x, y, r);
  223.             break; }
  224.         default: cout << "Wrong value!\n"; break;
  225.         }
  226.     }
  227.  
  228.     for (int i = 0; i < n; i++)
  229.     {
  230.         figures[i]->Draw();
  231.     }
  232.  
  233.  
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement