Advertisement
nguyenvanquan7826

Bresenham

Aug 19th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <graphics.h>
  2. #define DELAY 10
  3. int color = RED;
  4.  
  5. void lineBresenham(int x1, int y1, int x2, int y2)  // Ve duong thang Bresenham hoc tren truong
  6. {
  7.     int x, y, Dx, Dy, p, c1, c2;
  8.     Dx = abs(x2 - x1);
  9.     Dy = abs(y2 - y1);
  10.     p = 2*Dy - Dx;
  11.     c1 = 2*Dy;
  12.     c2 = 2*(Dy-Dx);
  13.     x = x1;
  14.     y = y1;
  15.    
  16.     int x_unit = 1, y_unit = 1;
  17.    
  18.     if (x2 - x1 < 0)
  19.         x_unit = -x_unit;
  20.     if (y2 - y1 < 0)
  21.         y_unit = -y_unit;
  22.        
  23.     putpixel(x,y,color);
  24.     while(x != x2){
  25.         delay(DELAY);
  26.         if (p<0) p += c1;
  27.         else{
  28.             p += c2;
  29.             y += y_unit;
  30.         }
  31.         x += x_unit;
  32.         putpixel(x,y,color);
  33.     }
  34. }
  35.  
  36.  
  37. void lineBresenham_1(int x1, int y1, int x2, int y2) // Ve duong thang Bresenham wiki
  38. {
  39.     int c2, c, Dx, Dy, x, y;
  40.     Dx = abs(x2 - x1);
  41.     Dy = abs(y2 - y1);
  42.     c = Dx - Dy;
  43.     c2 = 2*c;
  44.     x = x1;
  45.     y = y1;
  46.    
  47.     int x_unit = 1, y_unit = 1;
  48.    
  49.     if (x2 - x1 < 0)
  50.         x_unit = -x_unit;
  51.     if (y2 - y1 < 0)
  52.         y_unit = -y_unit;
  53.  
  54.     while(x != x2)
  55.     {
  56.         delay(DELAY);
  57.         putpixel(x, y, color+1);
  58.         if (x1 == x2 && y1 == y2) return;
  59.         c2 =2*c;
  60.         if (c2 > -Dy)  
  61.         {
  62.             c = c - Dy;
  63.             x = x + x_unit;
  64.         }
  65.         if (c2 < Dx)   
  66.         {
  67.             c = c + Dx;
  68.             y = y + y_unit;
  69.         }
  70.     }
  71. }
  72. int main(){
  73.     int gd,gm=VGAMAX; gd=DETECT;
  74.     initgraph(&gd,&gm,NULL);
  75.     lineBresenham(200,150, 150, 50);        // ve duong thang
  76.     lineBresenham(200,150, 150, 400);
  77.     lineBresenham(200,150, 250, 400);
  78.     lineBresenham(200,150, 250, 10);
  79.    
  80.     lineBresenham_1(200,150, 150, 50);      // ve duong thang
  81.     lineBresenham_1(200,150, 150, 400);
  82.     lineBresenham_1(200,150, 250, 400);
  83.     lineBresenham_1(200,150, 250, 10);
  84.    
  85.     outtextxy(250, 400, "YES");
  86.     outtextxy(250, 10, "YES");
  87.     outtextxy(150, 400, "YES");
  88.     outtextxy(150, 50, "YES");
  89.    
  90.     getchar();
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement