-
Notifications
You must be signed in to change notification settings - Fork 0
/
bresenham_line.c
77 lines (56 loc) · 1.19 KB
/
bresenham_line.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<graphics.h>
int main()
{
int xa, ya, xb, yb;
int gd=DETECT, gm;
printf("\n Enter the coordinates of initial point : \t");
scanf("%d%d", &xa, &ya);
printf("\n Enter the coordinates of final point : \t");
scanf("%d%d", &xb, &yb);
initgraph(&gd, &gm,"");
draw_line_using_bresenham(xa, ya, xb, yb);
getch();
closegraph();
return 0;
}
int draw_line_using_bresenham (int xa,int ya,int xb,int yb)
{
int x = xa, y = ya;
int dx = xb-xa, dy = yb-ya;
if ( dy/dx < 1)
{
int dT = 2*(dy-dx), dS = 2*dy;
int d = 2*dy-dx;
putpixel(x,y,WHITE);
while(x<xb)
{
x++;
if (d<0) d = d+dS;
else
{
y++;
d=d+dT;
}
putpixel(x,y,WHITE);
}
}
else
{
int dT = 2*(dx-dy), dS = 2*dx;
int d = 2*dx-dy;
putpixel(x,y,WHITE);
while(y<yb )
{
y++;
if (d<0) d = d+dS;
else
{
x++;
d=d+dT;
}
putpixel(x,y,WHITE);
}
}
return 0;
}