F How can we implement the Brasenhnam line algorithm using C? | CodeTheta

How can we implement the Brasenhnam line algorithm using C?

May 18, 2013

Here is a Brasenhnam line coding algorithm which is basically related to computer graphics program. This is very popular computer algorithm. In this programming code you can see the "math.h" and "graphics.h" library functions. math function will help us to calculate mathematical calculation it provide us to mathematical library function and graphics function provide us graphical function which help us to run this code and gives us graphics circle or any other structure

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void b_line(int, int, int, int);
void main()
{
int x1, y1, x2, y2;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
printf("\nEnter co-ordinates:\n");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
b_line(x1, x2, y1, y2);
getch();
}
void b_line(int x1, int x2, int y1, int y2)
{
int p, dy, dx, dydx, dy2, x, y, xend;
dy=abs(y2-y1);
dx=abs(x2-x1);
if(x1>x2)
{
x=x2;
y=y1;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x, y, WHITE);
dydx=2*(dy-dx);
dy2=2*dy;
p=2*(dy-dx);
while(x<xend)
{
x++;
if(p<0)
p+=dy2;
else
{
p+=dydx;
y++;
}
putpixel(x, y, WHITE);
}
}
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us