Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <graphics.h>
- #define DELAY 10
- int color = RED;
- void lineBresenham(int x1, int y1, int x2, int y2) // Ve duong thang Bresenham hoc tren truong
- {
- int x, y, Dx, Dy, p, c1, c2;
- Dx = abs(x2 - x1);
- Dy = abs(y2 - y1);
- p = 2*Dy - Dx;
- c1 = 2*Dy;
- c2 = 2*(Dy-Dx);
- x = x1;
- y = y1;
- int x_unit = 1, y_unit = 1;
- if (x2 - x1 < 0)
- x_unit = -x_unit;
- if (y2 - y1 < 0)
- y_unit = -y_unit;
- putpixel(x,y,color);
- while(x != x2){
- delay(DELAY);
- if (p<0) p += c1;
- else{
- p += c2;
- y += y_unit;
- }
- x += x_unit;
- putpixel(x,y,color);
- }
- }
- void lineBresenham_1(int x1, int y1, int x2, int y2) // Ve duong thang Bresenham wiki
- {
- int c2, c, Dx, Dy, x, y;
- Dx = abs(x2 - x1);
- Dy = abs(y2 - y1);
- c = Dx - Dy;
- c2 = 2*c;
- x = x1;
- y = y1;
- int x_unit = 1, y_unit = 1;
- if (x2 - x1 < 0)
- x_unit = -x_unit;
- if (y2 - y1 < 0)
- y_unit = -y_unit;
- while(x != x2)
- {
- delay(DELAY);
- putpixel(x, y, color+1);
- if (x1 == x2 && y1 == y2) return;
- c2 =2*c;
- if (c2 > -Dy)
- {
- c = c - Dy;
- x = x + x_unit;
- }
- if (c2 < Dx)
- {
- c = c + Dx;
- y = y + y_unit;
- }
- }
- }
- int main(){
- int gd,gm=VGAMAX; gd=DETECT;
- initgraph(&gd,&gm,NULL);
- lineBresenham(200,150, 150, 50); // ve duong thang
- lineBresenham(200,150, 150, 400);
- lineBresenham(200,150, 250, 400);
- lineBresenham(200,150, 250, 10);
- lineBresenham_1(200,150, 150, 50); // ve duong thang
- lineBresenham_1(200,150, 150, 400);
- lineBresenham_1(200,150, 250, 400);
- lineBresenham_1(200,150, 250, 10);
- outtextxy(250, 400, "YES");
- outtextxy(250, 10, "YES");
- outtextxy(150, 400, "YES");
- outtextxy(150, 50, "YES");
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement