Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <windows.h>
  4. #include <process.h>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. //A structure of a point, containing x,y
  10. struct Point
  11. {
  12.     int xCoordinate = 0;
  13.     int yCoordinate = 0;
  14. };
  15. //Abstract class
  16. class Shape
  17. {
  18. public:
  19.     virtual void set() = 0;
  20.     virtual void draw() const = 0;
  21. private:
  22. };
  23. class Triangle : public Shape
  24. {
  25. public:
  26.     //For reading the input from the user and storing them into the private members
  27.     void set();
  28.     //For plotting Triangle shape
  29.     void draw() const;
  30.     Triangle();
  31. private:
  32.     Point first;
  33.     Point second;
  34.     Point third;
  35. };
  36.  
  37. class Square : public Shape
  38. {
  39. public:
  40.     //For reading the input from the user and storing them into the private members
  41.     void set();
  42.     //For plotting Triangle shape
  43.     void draw() const;
  44.     Square();
  45. private:
  46.     Point first;
  47.     double length;
  48. };
  49.  
  50. class Rectangles : public Shape
  51. {
  52. public:
  53.     //For reading the input from the user and storing them into the private members
  54.     void set();
  55.     //For plotting Triangle shape
  56.     void draw() const;
  57.     Rectangles();
  58. private:
  59.     Point first;
  60.     Point second;
  61. };
  62. class Circle : public Shape
  63. {
  64. public:
  65.     //For reading the input from the user and storing them into the private members
  66.     void set();
  67.     //For plotting Triangle shape
  68.     void draw() const;
  69.     Circle();
  70. private:
  71.     Point first;
  72.     double radius;
  73. };
  74.  
  75. const double pi = 4 * atan(1.0);
  76. void gotoxy(double, double);
  77. void line(Point, Point);
  78. void circleShape(Point, double);
  79.  
  80. int main()
  81. {
  82.     int i;
  83.     Shape* p[4];
  84.  
  85.    
  86.     p[0] = new Triangle;
  87.     p[1] = new Square;
  88.     p[2] = new Rectangles;
  89.     p[3] = new Circle;
  90.  
  91.     for (i = 0; i < 4; i++)
  92.         p[i]->set();
  93.  
  94.     for (i = 0; i < 4; i++)
  95.     {
  96.         system("cls");
  97.         p[i]->draw();
  98.         Sleep(5000);
  99.     }
  100.  
  101.     cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl;
  102.     cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl;
  103.  
  104.     return 0;
  105. }
  106.  
  107.  
  108.  
  109. void line(Point first, Point second)
  110. {
  111.     int i, n = 10;
  112.     double hx, hy, xPos, yPos, cXc, cYc, slope = 1;
  113.  
  114.     xPos = cXc = first.xCoordinate;
  115.     yPos = cYc = first.yCoordinate;
  116.  
  117.     gotoxy(cXc, cYc);
  118.     cout << '.';
  119.  
  120.     hx = second.xCoordinate - cXc;
  121.     hy = second.yCoordinate - cYc;
  122.  
  123.     if (hx == 0)
  124.         hy = hy / n;
  125.     else
  126.     {
  127.         slope = hy / hx;
  128.         hx /= n;
  129.     }
  130.  
  131.     for (i = 0; i < n - 1; i++)
  132.     {
  133.         xPos += hx;
  134.         if (hx == 0)
  135.             yPos += hy;
  136.         else
  137.             yPos += slope * hx;
  138.         gotoxy(xPos, yPos);
  139.         cout << '.';
  140.     }
  141. }
  142.  
  143. void circleShape(Point center, double radius)
  144. {
  145.     int i, n = 10;
  146.     double hx, hy, xPos, yPos, cXc, cYc, diff;
  147.  
  148.     hy = 2 * radius / n;
  149.  
  150.     cXc = center.xCoordinate;
  151.     cYc = center.yCoordinate;
  152.     xPos = cXc;
  153.     yPos = cYc - radius;
  154.  
  155.     gotoxy(cXc, yPos);
  156.     cout << '.';
  157.  
  158.     for (i = 0; i < n - 1; i++)
  159.     {
  160.         yPos += hy;
  161.         diff = yPos - cYc;
  162.         hx = sqrt(radius * radius - diff * diff);
  163.         xPos = cXc - hx;
  164.         gotoxy(xPos, yPos);
  165.         cout << '.';
  166.         xPos = cXc + hx;
  167.         gotoxy(xPos, yPos);
  168.         cout << '.';
  169.     }
  170.  
  171.     yPos += hy;
  172.     gotoxy(cXc, yPos);
  173.     cout << '.';
  174. }
  175.  
  176. void gotoxy(double x, double y)
  177. {
  178.     HANDLE hConsoleOutput;
  179.     COORD dwCursorPosition;
  180.     cout.flush();
  181.     dwCursorPosition.X = x;
  182.     dwCursorPosition.Y = y;
  183.     hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  184.     SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition);
  185. }
  186.  
  187.  
  188. void Triangle::set()
  189. {
  190.     int x, y;
  191.  
  192.     cout << "Enter the coordinates of the triangle first point: ";
  193.     cin >> x >> y;
  194.     first.xCoordinate = x;
  195.     first.yCoordinate = y;
  196.  
  197.     cout << "Enter the coordinates of the triangle second point: ";
  198.     cin >> x >> y;
  199.     second.xCoordinate = x;
  200.     second.yCoordinate = y;
  201.  
  202.     cout << "Enter the coordinates of the triangle third point: ";
  203.     cin >> x >> y;
  204.     third.xCoordinate = x;
  205.     third.yCoordinate = y;
  206. }
  207.  
  208. void Triangle::draw() const
  209. {
  210.     //line(first, second);
  211.     //line(first, third);
  212.     //line(second, third);
  213.  
  214.     line(first, second);
  215.     line(second, third);
  216.     line(third, first);
  217. }
  218.  
  219. Triangle::Triangle()
  220. {
  221.     first.xCoordinate = 0; first.yCoordinate = 0;
  222.     second.xCoordinate = 0; second.yCoordinate = 0;
  223.     third.xCoordinate = 0; third.yCoordinate = 0;
  224. }
  225.  
  226.  
  227. void Square::set()
  228. {
  229.     int x, y, l;
  230.  
  231.     cout << "Enter the coordinates of the square first point: ";
  232.     cin >> x >> y;
  233.     first.xCoordinate = x;
  234.     first.yCoordinate = y;
  235.  
  236.     cout << "Enter the side length: ";
  237.     cin >> l;
  238.     length = l;
  239. }
  240.  
  241. void Square::draw() const
  242. {
  243.     //First is given, we need to create second, third, forth
  244.     Point second;
  245.     second.xCoordinate = (length + first.xCoordinate);
  246.     second.yCoordinate = first.yCoordinate;
  247.  
  248.     Point third;
  249.     third.xCoordinate = first.xCoordinate + length;
  250.     third.yCoordinate = first.yCoordinate + length;
  251.  
  252.     Point forth;
  253.     forth.xCoordinate = first.xCoordinate;
  254.     forth.yCoordinate = first.yCoordinate + length;
  255.  
  256.     line(first, second);
  257.     line(second, third);
  258.     line(third, forth);
  259.     line(forth, first);
  260. }
  261.  
  262. Square::Square()
  263. {
  264.     first.xCoordinate = 0;
  265.     first.yCoordinate = 0;
  266.     length = 0;
  267. }
  268.  
  269. void Rectangles::set()
  270. {
  271.     int x, y;
  272.  
  273.     cout << "Enter the coordinates of the rectangle first point: ";
  274.     cin >> x >> y;
  275.     first.xCoordinate = x;
  276.     first.yCoordinate = y;
  277.  
  278.     cout << "Enter the coordinates of the rectangle second point: ";
  279.     cin >> x >> y;
  280.     second.xCoordinate = x;
  281.     second.yCoordinate = y;
  282. }
  283.  
  284. void Rectangles::draw() const
  285. {
  286.     Point third;
  287.     third.xCoordinate = second.xCoordinate;
  288.     third.yCoordinate = first.yCoordinate;
  289.    
  290.     Point forth;
  291.     forth.xCoordinate = first.xCoordinate;
  292.     forth.yCoordinate = second.yCoordinate;
  293.  
  294.     line(second, third);
  295.     line(third, first);
  296.     line(second, forth);
  297.     line(forth, first);
  298. }
  299.  
  300. Rectangles::Rectangles()
  301. {
  302.     first.xCoordinate = 0; first.yCoordinate = 0;
  303.     second.xCoordinate = 0; second.yCoordinate = 0;
  304. }
  305.  
  306. void Circle::set()
  307. {
  308.     int x, y, r;
  309.  
  310.     cout << "Enter the coordinates of the center: ";
  311.     cin >> x >> y;
  312.     first.xCoordinate = x;
  313.     first.yCoordinate = y;
  314.  
  315.     cout << "Enter the radius: ";
  316.     cin >> r;
  317.     radius = r;
  318. }
  319.  
  320. void Circle::draw() const
  321. {
  322.     circleShape(first, radius);
  323. }
  324.  
  325. Circle::Circle()
  326. {
  327.     first.xCoordinate = 0;
  328.     first.yCoordinate = 0;
  329.     radius = 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement