Bresenham Line Drawing Algorithm – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 20 Aug 2022 15:07:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Bresenham Line Drawing Algorithm – Programmerbay https://programmerbay.com 32 32 C Program to Draw Line using Bresenham Line Drawing Algorithm https://programmerbay.com/bresenhams-line-drawing-algorithm/ https://programmerbay.com/bresenhams-line-drawing-algorithm/#respond Thu, 07 Jul 2022 17:59:43 +0000 http://www.programmerbay.com/?p=1583 Bresenham line drawing algorithm takes 2 coordinates and their starting and ending point to draw a line or a slope by considering the screen as a graph.

In this, the points are (X1, Y1) which is the starting point and (X2, Y2) which is the ending point. Where X2 can’t be smaller than X1.

Also Read : C program to draw a line using DDA algorithm in computer graphics

The code of this algorithm in C language is given below.

C Program to draw a line using Bresenham Line Drawing Algorithm

#include<graphics.h>
#include<stdio.h>
void main()
{
int x,y,x1,y1,delx,dely,m,grtr_d,smlr_d,d;
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("******* BRESENHAM'S LINE DRAWING ALGORITHM *****\n\n");
printf("enter initial coordinate = ");
scanf("%d %d",&x,&y);
printf("enter final coordinate = ");
scanf("%d %d",&x1,&y1);
delx=x1-x;
dely=y1-y;
grtr_d=2*dely-2*delx;&nbsp; &nbsp;// when d > 0
smlr_d=2*dely;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // when d< 0
d=(2*dely)-delx;

do{
putpixel(x,y,1);
if(d<0) {
d=smlr_d+d;
}
else
{
d=grtr_d+d;
y=y+1;
}
x=x+1;
}while(x<x1);
getch();
}

 

 

Output

image

]]>
https://programmerbay.com/bresenhams-line-drawing-algorithm/feed/ 0