Code Competitions

Coding competition

Tuesday, 14 May 2013

program to draw line using Bresenham’s Algorithm



Code for header file “Myhdr.h”



#include<conio.h>
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void lB(int x1,int y1,int xn, int yn)
{
int dx,dy,di,ds,dt;
dx = xn - x1,
dy = yn - y1;
di = 2 * dy - dx;
ds = 2 * dy;
dt = 2 * (dy - dx);
putpixel(x1, y1, RED);
while (x1 < xn)
{
x1++;
if (di < 0)
di = di + ds;
else
{
y1++;
di = di + dt;
}
putpixel(x1, y1, RED);
}

 Code For Main Program      
 #include<conio.h>
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include"z:Myhdr.h"
void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter start points of line : ");
scanf("%d %d", &x1, &y1);
printf("Enter ending points of line : ");
scanf("%d %d", &xn, &yn);
lB(x1, y1, xn, yn);
getch();
}    

OUTPUT:

No comments:

Post a Comment