Advertisement
nguyenvanquan7826

My Paint

Oct 13th, 2013
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.21 KB | None | 0 0
  1. #include <winbgim.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. #define Round(a) (int)(a+0.5)
  9. #define color_text 2
  10. #define color_bg 15
  11. #define color_bg1 1
  12. #define color_bg2 6
  13. #define color_line1 15
  14. #define color_line2 1
  15.  
  16. #include <stdlib.h>
  17. #include <stack>
  18. using namespace std;
  19. #define Round(a) (int)(a+0.5)
  20.  
  21. struct myPoint
  22. {
  23.     int x, y;
  24. };
  25.  
  26. struct myLine
  27. {
  28.     myPoint A, B;
  29.     float m;
  30. };
  31.  
  32. struct myRectangle
  33. {
  34.     int n;
  35.     myLine *a;
  36. };
  37.  
  38. struct myButton
  39. {
  40.     int x1, y1, x2, y2;
  41. };
  42.  
  43. stack <myPoint> S;
  44.  
  45.  
  46.  
  47. void raw_button(myButton bt, int color_fill);
  48. void raw_button_line(myButton bt, int color_fill, int color_line);
  49. void raw_button_crop(myButton bt, int color_fill, int color_line);
  50. void raw_button_del(myButton bt, int color_fill, int color_line);
  51. int hover_button(myButton bt, myPoint p);
  52. void raw_menu(int x1, int y1, int x2, int y2);
  53. int click_button(myButton bt, myPoint p);
  54. void SutherlandHodgman(myRectangle R, int n, myLine P1P2);
  55. void check_myLine(myLine L, myLine P1P2);
  56. int check_myPoint(myLine L, myPoint p);
  57.  
  58. int main(){
  59.     setbkcolor(1);
  60.     int x, y;
  61.     initwindow(800,500);
  62.     /*int mh = EGA, mode = EGALO;
  63.     initgraph(&mh, &mode,"");
  64.     */
  65.     begin:
  66.     setlinestyle(0, 0, 1);
  67.     setbkcolor(color_bg);
  68.     cleardevice();
  69.    
  70.     int x1 = 0, x2 = 50, y1 = 0, y2 = 300;
  71.     int size_x = x2 - x1, size_y = 50, space = 1;
  72.     int hover_line = 0, hover_crop = 0, hover_del = 0;
  73.     int click_line = 0, click_crop = 0, click_del = 0;
  74.     int click = 0;
  75.     int n = 4;
  76.     myPoint A[n];
  77.     myPoint P[2];
  78.     myLine P1P2;
  79.     myLine *L = (myLine *) malloc(n*sizeof(myLine));
  80.     myRectangle R;
  81.    
  82.    
  83.     int x1_line = x1, y1_line = y1, x2_line = x1_line+size_x, y2_line = y1_line + size_y;
  84.     int x1_crop = x1_line, y1_crop = y2_line + space, x2_crop = x1_crop+size_x, y2_crop = y1_crop + size_y;
  85.     int x1_del = x1_crop, y1_del = y2_crop + space, x2_del = x1_del+size_x, y2_del = y1_del + size_y;
  86.     int xi = 0, yi = 0, xm = 0, ym = 0;
  87.  
  88.     myButton btLine = {x1_line, y1_line, x2_line, y2_line};
  89.     myButton btCrop = {x1_crop, y1_crop, x2_crop, y2_crop};
  90.     myButton btDel = {x1_del, y1_del, x2_del, y2_del};
  91.    
  92.     setcolor(color_bg1);
  93.     line(x2, 0, x2, 500);
  94.     raw_button_line(btLine, color_bg1, color_line1);
  95.     raw_button_crop(btCrop, color_bg1, color_line1);
  96.     raw_button_del(btDel, color_bg1, color_line1);
  97.    
  98.     while (1){
  99.         clearmouseclick(WM_MBUTTONDOWN);
  100.         clearmouseclick(WM_MBUTTONUP);
  101.         clearmouseclick(WM_MBUTTONDBLCLK);
  102.         clearmouseclick(WM_MOUSEMOVE);
  103.         delay(0.01);
  104.        
  105.         if (ismouseclick(WM_LBUTTONDOWN)){
  106.             getmouseclick(WM_LBUTTONDOWN, x, y);
  107.             //printf("left click : (%d,%d)\n", x, y);
  108.             myPoint p = {x, y};
  109.             if(click_button(btLine, p))
  110.             {
  111.                 click_line = 1;
  112.                 click_crop = 0;
  113.             }
  114.            
  115.             if(click_button(btCrop, p))
  116.             {
  117.                 click_line = 0;
  118.                 click_crop = 1;
  119.             }
  120.            
  121.             if(click_button(btDel, p))
  122.             {
  123.                 click_line = 0;
  124.                 click_crop = 0;
  125.                 goto begin;
  126.             }
  127.                
  128.             click = 1;
  129.             xi = x;
  130.             yi = y;
  131.            
  132.             if (click_line && x>x2){
  133.                 P[0].x = x; P[0].y = y;
  134.                 printf("P1 (%d,%d)\n", x, y);
  135.             }
  136.             if (click_crop && x>x2){
  137.                 A[0].x = x; A[0].y = y;
  138.                 A[1].y = y;
  139.                 A[3].x = x;
  140.                 printf("A (%d,%d)\n", A[0].x, A[0].y);
  141.             }
  142.             clearmouseclick(WM_LBUTTONDOWN);
  143.         }
  144.  
  145.         if (ismouseclick(WM_LBUTTONUP)){
  146.             getmouseclick(WM_LBUTTONUP, x, y);
  147.             //printf("left up click : (%d,%d)\n", x, y);
  148.             //line(xi, yi, x, y);
  149.             click = 0;
  150.            
  151.             if (click_line && x>x2){
  152.                 P[1].x = x; P[1].y = y;
  153.                 printf("P2 (%d,%d)\n", x, y);
  154.                 P1P2.A = P[0]; P1P2.B = P[1]; P1P2.m = (float)(P1P2.B.y - P1P2.A.y)/(P1P2.B.x - P1P2.A.x);
  155.             }
  156.             if (click_crop && x>x2){
  157.                 A[2].x = x; A[2].y = y;
  158.                 A[1].x = x;
  159.                 A[3].y = y;
  160.                 printf("B (%d,%d)\n", A[1].x, A[1].y);
  161.                 printf("C (%d,%d)\n", A[2].x, A[2].y);
  162.                 printf("D (%d,%d)\n", A[3].x, A[3].y);
  163.                
  164.                
  165.                 for (int i=0; i<n; i++)
  166.                 {
  167.                     L[i].A = A[i];
  168.                     L[i].B = A[(i+1)%n];
  169.                 }
  170.                
  171.                 R.n = n; R.a = L;
  172.                
  173.                 SutherlandHodgman(R, n, P1P2);
  174.                
  175.                 setcolor(4);
  176.                 setlinestyle(1, 0, 1);
  177.                 rectangle(A[0].x, A[0].y, A[2].x, A[2].y);
  178.                    
  179.                 if (S.empty())
  180.                 {
  181.                     printf("Khong co doan thang nao duoc giu lai");
  182.                     //return 0;
  183.                 }
  184.                 else
  185.                 {
  186.                     printf("Cac doan thang duoc giu lai tao nen tu cac diem:\n");
  187.                     int i = 0;
  188.                     myPoint P[10];
  189.                     setlinestyle(0, 0, 3);
  190.                     setcolor(4);
  191.                     while (!S.empty())
  192.                     {
  193.                         P[i] = S.top();
  194.                         printf("(%d, %d)\n", P[i].x, P[i].y);
  195.                         S.pop();
  196.                         if (i>0) line(P[i-1].x, P[i-1].y, P[i].x, P[i].y);
  197.                         i++;
  198.                     }
  199.                     setlinestyle(0, 0, 1);
  200.                 }
  201.                
  202.             }
  203.             clearmouseclick(WM_LBUTTONUP);
  204.         }
  205.        
  206.        
  207.        
  208.         if (ismouseclick(WM_LBUTTONDBLCLK)){
  209.             getmouseclick(WM_LBUTTONDBLCLK, x, y);
  210.             printf("left double click : (%d,%d)\n", x, y);
  211.             clearmouseclick(WM_LBUTTONDBLCLK);
  212.         }
  213.  
  214.         if (ismouseclick(WM_RBUTTONDOWN)){
  215.             getmouseclick(WM_RBUTTONDOWN, x, y);
  216.             printf("right click : (%d,%d)\n", x, y);
  217.             clearmouseclick(WM_RBUTTONDOWN);
  218.        
  219.         }
  220.  
  221.         if (ismouseclick(WM_RBUTTONUP)){
  222.             getmouseclick(WM_RBUTTONUP, x, y);
  223.             printf("right up click : (%d,%d)\n", x, y);
  224.             clearmouseclick(WM_RBUTTONUP);
  225.        
  226.         }
  227.        
  228.         if (ismouseclick(WM_RBUTTONDBLCLK)){
  229.             getmouseclick(WM_RBUTTONDBLCLK, x, y);
  230.             printf("right double click : (%d,%d)\n", x, y);
  231.             clearmouseclick(WM_RBUTTONDBLCLK);
  232.         }
  233.        
  234.        
  235.         if (ismouseclick(WM_MOUSEMOVE)){
  236.             getmouseclick(WM_MOUSEMOVE, x, y);
  237.             //printf("move : (%d,%d)\n", x, y);
  238.             myPoint p = {x, y};
  239.             if (hover_button(btLine, p))
  240.             {
  241.                 raw_button_line(btLine, color_bg2, color_line2);
  242.                 hover_line = 1;
  243.             }
  244.             if (!hover_button(btLine, p) && hover_line == 1)
  245.             {
  246.                 raw_button_line(btLine, color_bg1, color_line1);
  247.                 hover_line = 0;
  248.             }
  249.            
  250.            
  251.             if (click_line && click && xi>x2 && x > x2 && (x != xm || y != ym))
  252.             {
  253.                 setcolor(color_bg);
  254.                 line(xi, yi, xm, ym);
  255.             }
  256.             xm = x, ym = y;
  257.             if (click_line && click && xi>x2 && xm > x2)
  258.             {
  259.                 setcolor(2);
  260.                 line(xi, yi, xm, ym);
  261.             }      
  262.            
  263.             // done Line
  264.            
  265.             // Crop
  266.             if (hover_button(btCrop, p))
  267.             {
  268.                 raw_button_crop(btCrop, color_bg2, color_line2);
  269.                 hover_crop = 1;
  270.             }
  271.             if (!hover_button(btCrop, p) && hover_crop == 1)
  272.             {
  273.                 raw_button_crop(btCrop, color_bg1, color_line1);
  274.                 hover_crop = 0;
  275.             }
  276.            
  277.             // done Crop
  278.            
  279.             // Del
  280.             if (hover_button(btDel, p))
  281.             {
  282.                 raw_button_del(btDel, color_bg2, color_line2);
  283.                 hover_del = 1;
  284.             }
  285.             if (!hover_button(btDel, p) && hover_del == 1)
  286.             {
  287.                 raw_button_del(btDel, color_bg1, color_line1);
  288.                 hover_del = 0;
  289.             }
  290.            
  291.            
  292.            
  293.             clearmouseclick(WM_MOUSEMOVE);
  294.         }
  295.     }
  296.    
  297.     system("pause");
  298.     closegraph();
  299. }
  300.  
  301. void raw_button(myButton bt, int color_fill)
  302. {
  303.     settextjustify(1, 1);
  304.     setfillstyle(1,color_fill);
  305.     bar(bt.x1, bt.y1, bt.x2, bt.y2);
  306. }
  307.  
  308.  
  309. void raw_button_line(myButton bt, int color_fill, int color_line)
  310. {
  311.     raw_button(bt, color_fill);
  312.     setcolor(color_line);
  313.     line((bt.x2-bt.x1)/4, (bt.y2-bt.y1)/4, (bt.x2-bt.x1)/4*3, (bt.y2-bt.y1)/4*3);
  314. }
  315.  
  316. void raw_button_crop(myButton bt, int color_fill, int color_line)
  317. {
  318.     raw_button(bt, color_fill);
  319.     setcolor(color_line);
  320.     int space = 10;
  321.     line(bt.x1 + space, bt.y1 + 2*space, bt.x2 - 2*space, bt.y1 + 2*space);
  322.     line(bt.x1 + 2*space, bt.y2 - 2*space, bt.x2 - space, bt.y2 - 2*space);
  323.     line(bt.x1 + 2*space, bt.y1 + space, bt.x1 + 2*space, bt.y2 - 2*space);
  324.     line(bt.x2 - 2*space, bt.y1 + 2*space, bt.x2 - 2*space, bt.y2 - space);
  325. }
  326.  
  327. void raw_button_del(myButton bt, int color_fill, int color_line)
  328. {
  329.     raw_button(bt, color_fill);
  330.     settextjustify(1, 1);
  331.     setfillstyle(1,color_line);
  332.     bar(bt.x1 + (bt.x2-bt.x1)/4, bt.y1 + (bt.y2-bt.y1)/4, bt.x1 + (bt.x2-bt.x1)/4*3, bt.y1 + (bt.y2-bt.y1)/4*3);
  333. }
  334.  
  335. int hover_button(myButton bt, myPoint p)
  336. {
  337.     if((p.x>bt.x1) && (p.x<bt.x2) && (p.y>bt.y1) && (p.y<bt.y2)) return 1;
  338.     return 0;
  339. }
  340.  
  341. int click_button(myButton bt, myPoint p)
  342. {
  343.     if((p.x>bt.x1) && (p.x<bt.x2) && (p.y>bt.y1) && (p.y<bt.y2)) return 1;
  344.     return 0;
  345. }
  346.  
  347. int check_myPoint(myLine L, myPoint p)
  348. {
  349.     int c = (L.B.x - L.A.x)*(p.y - L.A.y) - (L.B.y - L.A.y)*(p.x - L.A.x);
  350.     if (c >= 0) return 1;
  351.     else return 0;
  352. }
  353.  
  354. void check_myLine(myLine L, myLine P1P2)
  355. {
  356.     int a = check_myPoint(L, P1P2.A), b = check_myPoint(L, P1P2.B);
  357.     if (!a && !b)
  358.         return;
  359.     if (a && b)
  360.     {
  361.         S.push(P1P2.A);
  362.         S.push(P1P2.B);
  363.         return;
  364.     }
  365.     myPoint I;
  366.     if (b)
  367.     {
  368.         myPoint temp = P1P2.A;
  369.         P1P2.A = P1P2.B;
  370.         P1P2.B = temp;
  371.     }
  372.  
  373.     if (L.A.x == L.B.x)
  374.     {
  375.         I.x = L.A.x;
  376.         I.y = P1P2.A.y + Round((float)(I.x - P1P2.A.x)*P1P2.m);
  377.     }
  378.     else if (L.A.y == L.B.y)
  379.     {
  380.         I.y = L.A.y;
  381.         I.x = P1P2.A.x + Round((float)(I.y - P1P2.A.y)/P1P2.m);
  382.     }
  383.  
  384.     S.push(P1P2.A);
  385.     S.push(I);
  386. }
  387.  
  388. void SutherlandHodgman(myRectangle R, int n, myLine P1P2)
  389. {
  390.     //P1P2.m = (float)(P1P2.B.y - P1P2.A.y)/(P1P2.B.x - P1P2.A.x);
  391.  
  392.     check_myLine(R.a[0], P1P2);
  393.     for (int i=1; i<n; i++)
  394.     {
  395.         if (S.empty()) break;
  396.         P1P2.A = S.top();
  397.         S.pop();
  398.         P1P2.B = S.top();
  399.         S.pop();
  400.         printf("%d,%d\n", P1P2.A.x, P1P2.A.y);
  401.         printf("%d,%d\n", P1P2.B.x, P1P2.B.y);
  402.         check_myLine(R.a[i], P1P2);
  403.     }
  404.  
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement