Mike_King

Untitled

Feb 13th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.46 KB | None | 0 0
  1. struct point
  2. {
  3.     int x, y;    
  4. };
  5.  
  6. int inRectangle(struct point p1, struct point p2, int dotX, int dotY)
  7. {
  8.     struct point dot;
  9.     dot.x = dotX;
  10.     dot.y = dotY;
  11.    
  12.     if (dot.x >= p1.x && dot.x <= p2.x)
  13.     {
  14.         if ( dot.y >= p1.y && dot.y <= p2.y)
  15.         {
  16.             return 1;
  17.         }
  18.     }
  19.     return 0;
  20. }
  21.  
  22. int main(void)
  23. {
  24.     struct point first_p1, first_p2; // Первый прямоугольник
  25.     struct point second_p1, second_p2; // Второй прямоугольник
  26.     struct point dot1, dot2; // Точки пересечения
  27.     char a = 0, b = 0, f = 0, f2 = 0, ni = 1;
  28.     /*
  29.         a - кол-во точек в первом прямоугольнике
  30.         b - кол-во точек в втором прямоугольнике
  31.        
  32.         f, f2 - стороны
  33.        
  34.         Формат прямоугольников и точек прересечения:
  35.         |-----p2
  36.         |      |
  37.         p1-----|
  38.     */
  39.  
  40.     printf("First rectangle:\n");
  41.     printf("x1 y1: ");
  42.     scanf("%d %d", &first_p1.x, &first_p1.y);
  43.     printf("x2 y2: ");
  44.     scanf("%d %d", &first_p2.x, &first_p2.y);
  45.    
  46.     printf("Second rectangle:\n");
  47.     printf("x1 y1: ");
  48.     scanf("%d %d", &second_p1.x, &second_p1.y);
  49.     printf("x2 y2: ");
  50.     scanf("%d %d", &second_p2.x, &second_p2.y);
  51.    
  52.     /*
  53.        Узнаем кол-во точек первого прямоугольника в втором
  54.        и кол-во точек второго прямоугольника в первом.
  55.        + Узнаем сторону.
  56.        
  57.        Проверка точек в порядке(f, f2):
  58.        4     1
  59.        /\    \/
  60.        3  <  2
  61.     */
  62.    
  63.     // Сколько точек второго прямоугольника в первом
  64.    
  65.     if (inRectangle(first_p1, first_p2, second_p2.x, second_p2.y) == 1)
  66.     {
  67.         a++;
  68.         f = 1;
  69.     }
  70.    
  71.     if (inRectangle(first_p1, first_p2, second_p2.x, second_p1.y) == 1)
  72.     {
  73.         a++;
  74.         f = 2;
  75.     }
  76.    
  77.     if (inRectangle(first_p1, first_p2, second_p1.x, second_p1.y) == 1)
  78.     {
  79.         a++;
  80.         f = 3;
  81.     }
  82.    
  83.     if (inRectangle(first_p1, first_p2, second_p1.x, second_p2.y) == 1)
  84.     {
  85.         a++;
  86.         f = 4;
  87.     }
  88.  
  89.     // Скольо точек первого прямоугольника в втором
  90.    
  91.     if (inRectangle(second_p1, second_p2, first_p2.x, first_p2.y) == 1)
  92.     {
  93.         b++;
  94.         f2 = 1;
  95.     }
  96.    
  97.     if (inRectangle(second_p1, second_p2, first_p2.x, first_p1.y) == 1)
  98.     {
  99.         b++;
  100.         f2 = 2;
  101.     }
  102.    
  103.     if (inRectangle(second_p1, second_p2, first_p1.x, first_p1.y) == 1)
  104.     {
  105.         b++;
  106.         f2 = 3;
  107.     }
  108.    
  109.     if (inRectangle(second_p1, second_p2, first_p1.x, first_p2.y) == 1)
  110.     {
  111.         b++;
  112.         f2 = 4;
  113.     }
  114.  
  115.     /*
  116.         Узнаем точки пересечения
  117.     */
  118.    
  119.     if (a == b && a != 0) // Кол-во точек одинаково
  120.     {
  121.         if (f == 1)
  122.         {
  123.             dot1 = first_p1;
  124.             dot2 = second_p2;
  125.         }
  126.         else if (f == 2)
  127.         {
  128.             dot1.x = first_p1.x;
  129.             dot1.y = second_p1.y;
  130.        
  131.             dot2.x = second_p2.x;
  132.             dot2.y = first_p2.y;
  133.         }
  134.         else if (f == 3)
  135.         {
  136.             dot1 = second_p1;
  137.             dot2 = first_p2;
  138.         }
  139.         else if (f == 4)
  140.         {
  141.             dot1.x = second_p1.x;
  142.             dot1.y = first_p1.y;
  143.        
  144.             dot2.x = first_p2.x;
  145.             dot2.y = second_p2.y;
  146.         }
  147.     }
  148.     else if (a == 2 && b == 0) // Если две точки второго в первом
  149.     {
  150.         if (f == 2)
  151.         {
  152.             dot1.x = first_p1.x;
  153.             dot1.y = second_p1.y;
  154.        
  155.             dot2 = second_p2;
  156.         }
  157.         else if (f == 3)
  158.         {
  159.             dot1 = second_p1;
  160.            
  161.             dot2.x = second_p2.x;
  162.             dot2.y = first_p2.y;
  163.         }
  164.         else if (f == 4)
  165.         {
  166.             if (inRectangle(first_p1, first_p2, second_p1.x, second_p1.y) == 1)
  167.             {
  168.                 dot1 = second_p1;
  169.                
  170.                 dot2.x = first_p2.x;
  171.                 dot2.y = second_p2.y;
  172.                
  173.             }
  174.             else if (inRectangle(first_p1, first_p2, second_p2.x, second_p2.y) == 1)
  175.             {
  176.                 dot1.x = second_p1.x;
  177.                 dot1.y = first_p1.y;
  178.            
  179.                 dot2 = second_p2;
  180.             }
  181.         }
  182.     }
  183.     else if (a == 0 && b == 2) // Если две точки первого в втором
  184.     {
  185.         if (f2 == 2)
  186.         {
  187.             dot1.x = second_p1.x;
  188.             dot1.y = first_p1.y;
  189.        
  190.             dot2 = first_p2;
  191.         }
  192.         else if (f2 == 3)
  193.         {
  194.             dot1 = first_p1;
  195.            
  196.             dot2.x = first_p2.x;
  197.             dot2.y = second_p2.y;
  198.         }
  199.         else if (f2 == 4)
  200.         {
  201.             if (inRectangle(second_p1, second_p2, first_p1.x, first_p1.y) == 1)
  202.             {
  203.                 dot1 = first_p1;
  204.                
  205.                 dot2.x = second_p2.x;
  206.                 dot2.y = first_p2.y;
  207.                
  208.             }
  209.             else if (inRectangle(second_p1, second_p2, first_p2.x, first_p2.y) == 1)
  210.             {
  211.                 dot1.x = first_p1.x;
  212.                 dot1.y = second_p1.y;
  213.            
  214.                 dot2 = first_p2;
  215.             }
  216.         }
  217.     }
  218.     else if (a == 4 && b == 0) // Если второй входит в первый
  219.     {
  220.         dot1 = second_p1;
  221.         dot2 = second_p2;
  222.  
  223.     }
  224.     else if (a == 0 && b == 4) // Если первый входит в второй
  225.     {
  226.          dot1 = first_p1;
  227.          dot2 = first_p2;
  228.     }
  229.     else if (a == 0 && b == 0) //
  230.     {
  231.         /* if (first_p1.x == second_p1.x && first_p1.y == second_p1.y) // Если один наложен на другой
  232.         {
  233.             if (first_p2.x == second_p2.x && first_p2.y == second_p2.y)
  234.             {
  235.                 dot1 = first_p1;
  236.                 dot2 = second_p2;
  237.             }
  238.         } */
  239.         if (second_p1.x < first_p1.x && second_p2.x > first_p2.x)
  240.         {
  241.             if (second_p1.y > first_p1.y && second_p1.y < first_p2.y)
  242.             {
  243.                 if (second_p2.y > first_p1.y && second_p2.y < first_p2.y)
  244.                 {
  245.                     dot1.x = first_p1.x;
  246.                     dot1.y = second_p1.y;
  247.                
  248.                     dot2.x = first_p2.x;
  249.                     dot2.y = second_p2.y;
  250.                 }
  251.             }
  252.         }
  253.         else if (second_p1.y < first_p1.y && second_p2.y > first_p2.y)
  254.         {
  255.             if (second_p1.x > first_p1.x && second_p1.x < first_p2.x)
  256.             {
  257.                 if (second_p2.x > first_p1.x && second_p2.x < first_p2.x)
  258.                 {
  259.                     dot1.x = second_p1.x;
  260.                     dot1.y = first_p1.y;
  261.                
  262.                     dot2.x = second_p2.x;
  263.                     dot2.y = first_p2.y;
  264.                 }
  265.             }
  266.         }
  267.        
  268.         else
  269.         {
  270.             ni = 0;
  271.             printf("not intersect\n");
  272.         }
  273.     }
  274.    
  275.     if (ni == 1)
  276.     {
  277.         printf("\n");
  278.         printf("x1 = %d\ny1 = %d\n", dot1.x, dot1.y);
  279.         printf("x2 = %d\ny2 = %d\n", dot2.x, dot2.y);
  280.     }
  281.  
  282.     system("pause");
  283. }
Advertisement
Add Comment
Please, Sign In to add comment