Advertisement
nguyenvanquan7826

Xe duong thang - SutherlandHodgman

Oct 7th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stack>
  4. using namespace std;
  5. #define Round(a) (int)(a+0.5)
  6.  
  7. struct point
  8. {
  9.     int x, y;
  10. };
  11.  
  12. struct line
  13. {
  14.     point A, B;
  15.     float m;
  16. };
  17.  
  18. struct rectangle
  19. {
  20.     line AB, BC, CD, DA;
  21. };
  22.  
  23. stack <point> S;
  24.  
  25. int check_point(line L, point P)
  26. {
  27.     int c = (L.B.x - L.A.x)*(P.y - L.A.y) - (L.B.y - L.A.y)*(P.x - L.A.x);
  28.     if (c >= 0) return 1;
  29.     else return 0;
  30. }
  31.  
  32. void check_line(line L, line P1P2)
  33. {
  34.     int a = check_point(L, P1P2.A), b = check_point(L, P1P2.B);
  35.     if (a && b)
  36.     {
  37.         S.push(P1P2.A);
  38.         S.push(P1P2.B);
  39.     }
  40.     else
  41.     {
  42.         point I;
  43.         if (b)
  44.         {
  45.             point temp = P1P2.A;
  46.             P1P2.A = P1P2.B;
  47.             P1P2.B = temp;
  48.         }
  49.  
  50.         if (L.A.x == L.B.x)
  51.         {
  52.             I.x = L.A.x;
  53.             I.y = P1P2.A.y + Round((float)(I.x - P1P2.A.x)*P1P2.m);
  54.         }
  55.         else if (L.A.y == L.B.y)
  56.         {
  57.             I.y = L.A.y;
  58.             I.x = P1P2.A.x + Round((float)(I.y - P1P2.A.y)/P1P2.m);
  59.         }
  60.  
  61.         S.push(P1P2.A);
  62.         S.push(I);
  63.     }
  64. }
  65.  
  66. void SutherlandHodgman(rectangle R, line P1P2)
  67. {
  68.     P1P2.m = (float)(P1P2.B.y - P1P2.A.y)/(P1P2.B.x - P1P2.A.x);
  69.  
  70.     check_line(R.AB, P1P2);
  71.     P1P2.A = S.top();
  72.     S.pop();
  73.     P1P2.B = S.top();
  74.     S.pop();
  75.  
  76.     check_line(R.BC, P1P2);
  77.     P1P2.A = S.top();
  78.     S.pop();
  79.     P1P2.B = S.top();
  80.     S.pop();
  81.  
  82.     check_line(R.CD, P1P2);
  83.     P1P2.A = S.top();
  84.     S.pop();
  85.     P1P2.B = S.top();
  86.     S.pop();
  87.     check_line(R.DA, P1P2);
  88. }
  89.  
  90. int main()
  91. {
  92.     point A[] = {
  93.             {2,2}//A
  94.             {8,2}//B
  95.             {8,6}//C
  96.             {2,6}//D
  97.             {1,3}// P1
  98.             {11,8}  // P2
  99.         };
  100.     line L[] = {
  101.             {A[0], A[1]},   // AB
  102.             {A[1], A[2]},   // BC
  103.             {A[2], A[3]},   // CD
  104.             {A[3], A[0]},   // DA
  105.             {A[4], A[5]},   // P1P2
  106.         };
  107.     rectangle R = {L[0], L[1], L[2], L[3]}; // HCN ABCD
  108.     SutherlandHodgman(R, L[4]);     // Xen P1P2 bang ABCD
  109.  
  110.     if (S.empty())
  111.     {
  112.         printf("Khong co doan thang nao duoc giu lai");
  113.         return 0;
  114.     }
  115.     printf("Cac doan thang duoc giu lai tao nen tu cac diem:\n");
  116.     while (!S.empty())
  117.     {
  118.         printf("(%d, %d)\n", S.top().x, S.top().y);
  119.         S.pop();
  120.     }
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement