0% found this document useful (0 votes)
663 views4 pages

Generalized Bresenham's Line Algorithm

This document contains the code for a C program that implements Bresenham's line drawing algorithm. It takes x and y coordinates for two points as input, calculates the slope and distance between the points, and uses conditional logic to iteratively plot pixels along the line between the points. It handles both cases where the change in x is greater than or less than the change in y. The program initializes graphics mode, gets the start/end points from the user, runs the line drawing algorithm to plot the points, and ends by closing the graphics window.

Uploaded by

Neha Chindaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
663 views4 pages

Generalized Bresenham's Line Algorithm

This document contains the code for a C program that implements Bresenham's line drawing algorithm. It takes x and y coordinates for two points as input, calculates the slope and distance between the points, and uses conditional logic to iteratively plot pixels along the line between the points. It handles both cases where the change in x is greater than or less than the change in y. The program initializes graphics mode, gets the start/end points from the user, runs the line drawing algorithm to plot the points, and ends by closing the graphics window.

Uploaded by

Neha Chindaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2. Write a program for Generalized Bresenhams line Drawing Algorithm.

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
int main(void)
{

float x,y,x1,y1,x2,y2,dx,dy,e,temp;
int i,gd,gm,s1,s2,ex;
clrscr();
printf("\n enter the value x1: ");
scanf("%f",&x1);
printf("\n enter the value y1: ");
scanf("%f",&y1);
printf("\n enter the value x2: ");
scanf("%f",&x2);
printf("\n enter the value y2: ");
scanf("%f",&y2);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);

dy=abs(y2-y1);
x=x1;
y=y1;
if(x2>x1)
{
s1=1;
}
if(x2==x1)
{
s1=0;
}
if(x2<x1)
{
s1=-1;
}
if(y2>y1)
{
s2=1;
}
if(y2==y1)
{
s2=0;
}
if(y2<y1)
{

s2=-1;
}
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
ex=1;
}
else
{
ex=0;
}
e=2*dy-dx;
i=1;
do
{
putpixel(x,y,15);
while(e>=0)
{
if(ex==1)
{
x=x+s1;
}
else

{
y=y+s2;
}
e=e-2*dx;
}
if(ex==1)
{
y=y+s2;
}
else
{
x=x+s1;
}
e=e+2*dy;
i=i+1;
}
while(i<=dx+1);
getch();
closegraph();
}

You might also like