Advertisement
nguyenvanquan7826

update Xe duong thang - SutherlandHodgman

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