1void drawline(int x0, int y0, int x1, int y1)
2{
3 int dx, dy, p, x, y;
4 dx=x1-x0;
5 dy=y1-y0;
6 x=x0;
7 y=y0;
8 p=2*dy-dx;
9 while(x<x1)
10 {
11 if(p>=0)
12 {
13 putpixel(x,y,7);
14 y=y+1;
15 p=p+2*dy-2*dx;
16 x+=1;
17 }
18 else
19 {
20 putpixel(x,y,7);
21 p=p+2*dy;}
22 x+=1;
23 }
24}
1#include<iostream.h>
2#include<graphics.h>
3
4void drawline(int x0, int y0, int x1, int y1)
5{
6 int dx, dy, p, x, y;
7
8 dx=x1-x0;
9 dy=y1-y0;
10
11 x=x0;
12 y=y0;
13
14 p=2*dy-dx;
15
16 while(x<x1)
17 {
18 if(p>=0)
19 {
20 putpixel(x,y,7);
21 y=y+1;
22 p=p+2*dy-2*dx;
23 }
24 else
25 {
26 putpixel(x,y,7);
27 p=p+2*dy;
28 }
29 x=x+1;
30 }
31}
32
33int main()
34{
35 int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
36 initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
37
38 cout<<"Enter co-ordinates of first point: ";
39 cin>>x0>>y0;
40
41 cout<<"Enter co-ordinates of second point: ";
42 cin>>x1>>y1;
43 drawline(x0, y0, x1, y1);
44
45 return 0;
46}
47
1static void bresenham(int x1, int y1, int x2, int y2)
2 {
3 int m_new = 2 * (y2 - y1);
4 int slope_error_new = m_new - (x2 - x1);
5
6 for (int x = x1, y = y1; x <= x2; x++)
7 {
8 System.out.print("(" +x + "," + y + ")\n");
9
10 // Add slope to increment angle formed
11 slope_error_new += m_new;
12
13 // Slope error reached limit, time to
14 // increment y and update slope error.
15 if (slope_error_new >= 0)
16 {
17 y++;
18 slope_error_new -= 2 * (x2 - x1);
19 }
20 }
21 }