Advertisement
Proff_Ust

figures_ready

Jun 4th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. using namespace std;
  4.  
  5. #define FIGURE_NUM 2
  6. enum tFigure
  7. {
  8.     circle,
  9.     rectangle,
  10.     triangle
  11. };
  12.  
  13. enum tColor
  14. {
  15.     red,
  16.     green,
  17.     blue
  18. };
  19.  
  20. struct tPoint
  21. {
  22.     float x;
  23.     float y;
  24. };
  25.  
  26. struct tCircle
  27. {
  28.     tPoint* center;
  29.     float radius;
  30. };
  31.  
  32. struct tRectangle
  33. {
  34.     tPoint* s1;
  35.     tPoint* s2;
  36. };
  37.  
  38. struct tTriangle
  39. {
  40.     tPoint* t1;
  41.     tPoint* t2;
  42.     tPoint* t3;
  43. };
  44.  
  45. union uFigures
  46. {
  47.     tCircle* Circle;
  48.     tRectangle* Rectangle;
  49.     tTriangle* Triangle;
  50. };
  51.  
  52. struct tFigures
  53. {
  54.     tColor color;
  55.     tFigure figureType;
  56.     uFigures figures;
  57. };
  58.  
  59. tPoint* inputPoint()
  60. {
  61.     tPoint* point1 = new tPoint;
  62.     cout<<"x=";
  63.     cin>>point1->x;
  64.     cout<<"y=";
  65.     cin>>point1->y;
  66.     return point1;
  67. }
  68.  
  69. tFigures* inputFigure()
  70. {
  71.     tFigures* fig1 = new tFigures;
  72.     int num;
  73.     cout<<"Введите цвет фигуры (0-красный, 1-зеленый, 2-синий) ";
  74.     cin>>num;
  75.     fig1->color=tColor(num);
  76.     cout<<"Введите тип фигуры (0-круг, 1-прямоугольник, 2-треугольник) ";
  77.     cin>>num;
  78.     fig1->figureType=tFigure(num);
  79.     switch(fig1->figureType)
  80.     {
  81.     case circle:
  82.         {
  83.             cout<<"Введите координату центра и радиус круга"<<endl;
  84.             fig1->figures.Circle->center=inputPoint();
  85.             cout<<"r=";
  86.             cin>>fig1->figures.Circle->radius;
  87.             break;
  88.         }
  89.     case rectangle:
  90.         {
  91.             cout<<"Ведите координаты верхней левой и нижнего правой вершины"<<endl;
  92.             fig1->figures.Rectangle->s1=inputPoint();
  93.             fig1->figures.Rectangle->s2=inputPoint();
  94.             break;
  95.         }
  96.     case triangle:
  97.         {
  98.             cout<<"Введите координаты вершин треугольника"<<endl;
  99.             fig1->figures.Triangle->t1=inputPoint();
  100.             fig1->figures.Triangle->t2=inputPoint();
  101.             fig1->figures.Triangle->t3=inputPoint();
  102.             break;
  103.         }
  104.     }
  105.     return fig1;
  106. }
  107.  
  108. int outputFigure(tFigures fig)
  109. {
  110.     switch (fig.color)
  111.     {
  112.     case blue:
  113.         cout<<"Blue ";
  114.         break;
  115.     case red:
  116.         cout<<"Red ";
  117.         break;
  118.     case green:
  119.         cout<<"Green ";
  120.         break;
  121.     }
  122.  
  123.     switch (fig.figureType)
  124.     {
  125.     case circle:
  126.         cout<<"Circle"<<endl;
  127.         cout<<fig.figures.Circle->center->x<<" ";
  128.         cout<<fig.figures.Circle->center->y<<endl;
  129.         cout<<fig.figures.Circle->radius<<endl<<endl;
  130.         break;
  131.     case rectangle:
  132.         cout<<"Rectangle"<<endl;
  133.         cout<<fig.figures.Rectangle->s1->x<<" ";
  134.         cout<<fig.figures.Rectangle->s1->y<<endl;
  135.         cout<<fig.figures.Rectangle->s2->x<<" ";
  136.         cout<<fig.figures.Rectangle->s2->y<<endl;
  137.         break;
  138.     case triangle:
  139.         cout<<"Triangle"<<endl;
  140.         cout<<fig.figures.Triangle->t1->x<<" ";
  141.         cout<<fig.figures.Triangle->t1->y<<endl;
  142.         cout<<fig.figures.Triangle->t2->x<<" ";
  143.         cout<<fig.figures.Triangle->t2->y<<endl;
  144.         cout<<fig.figures.Triangle->t3->x<<" ";
  145.         cout<<fig.figures.Triangle->t3->y<<endl;
  146.         break;
  147.     }
  148.     return 0;
  149. }
  150.  
  151. bool checkFigure(tFigures* fig1, tFigures* fig2)
  152. {
  153.     if (fig1->figureType==circle)
  154.     {
  155.         return (fig1->figures.Circle->center->x + fig1->figures.Circle->radius < fig2->figures.Rectangle->s2->x &&
  156.                fig1->figures.Circle->center->x - fig1->figures.Circle->radius > fig2->figures.Rectangle->s1->x &&
  157.                fig1->figures.Circle->center->y + fig1->figures.Circle->radius < fig2->figures.Rectangle->s1->y &&
  158.                fig1->figures.Circle->center->y - fig1->figures.Circle->radius > fig2->figures.Rectangle->s2->y);
  159.     }
  160.     else
  161.     {
  162.         return (fig2->figures.Circle->center->x + fig2->figures.Circle->radius < fig1->figures.Rectangle->s2->x &&
  163.                fig2->figures.Circle->center->x - fig2->figures.Circle->radius > fig1->figures.Rectangle->s1->x &&
  164.                fig2->figures.Circle->center->y + fig2->figures.Circle->radius < fig1->figures.Rectangle->s1->y &&
  165.                fig2->figures.Circle->center->y - fig2->figures.Circle->radius > fig1->figures.Rectangle->s2->y);
  166.     }
  167.  
  168. }
  169. int main()
  170. {
  171.     setlocale(LC_ALL,"rus");
  172.     tFigures* List = new tFigures[FIGURE_NUM];
  173.     for(int i=0;i<FIGURE_NUM;i++)
  174.     {
  175.         cout<<"Введите данные об "<<i+1<<"-ой фигуре"<<endl;
  176.         List[i] = *inputFigure();
  177.     }
  178.  
  179.     for(int i=0;i<FIGURE_NUM;i++)
  180.         outputFigure(List[i]);
  181.  
  182.     bool checkResult = false;
  183.     for(int i=0;i<FIGURE_NUM;i++)
  184.     {
  185.         for(int j=i;j<FIGURE_NUM;j++)
  186.             if(((List[i].figureType==circle) && (List[j].figureType==rectangle))||((List[i].figureType==rectangle) && (List[j].figureType==circle)))
  187.             {
  188.                 checkResult = checkFigure(&List[i], &List[j]);
  189.                 if(checkResult)
  190.                     break;
  191.             }
  192.             else
  193.                 continue;
  194.         if (checkResult)
  195.             break;
  196.     }
  197.  
  198.     if(checkResult)
  199.         cout<<"есть круг вписанный в прямоугольник";
  200.     else
  201.         cout<<"нет круга вписанного в прямоугольник";
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement