Draw Line using Bresenham's Algorithm
Rohan Sakhale 11/12/2011 code-examplecpp
# Summary
This is another algorithm which can be used to draw a line and this algorithm is better and gives a more accurate output than DDA algorithm we previously discussed This is another example shown in Computer Graphics using C Language
# Code
#include "stdio.h"
#include "conio.h"
#include "graphics.h"
void value(int x1,int y1,int x2,int y2)
{
int dx,dy,x,y,p,k;
dx = x2 - x1;
dy = y2 - y1;
x = x1;
y = y1;
p = (2 * dy) - (dx);
for(k=0;k<dx;k++)
{
if(p<0)
{
x++;
putpixel(x,y,WHITE);
p = p + (2 * dy);
}
else
{
x++;
y++;
putpixel(x+1,y+1,WHITE);
p = p + (2 * dy) - (2 * dx);
}
}
}
void main()
{
int gd,gm,xa,xb,ya,yb;
detechgraph(&gd,&gm);
gd = DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the starting point:=> ");
scanf("%d %d",&xa,&ya);
printf("Enter the end point:=> ");
scanf("%d %d",&xb,&yb);
value(xa,ya,xb,yb);
getch();
closegraph();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45