Posted By

ZeusEm on 03/10/15

Statistics

Viewed 386 times
Favorited by 0 user(s)

Graphic Design | Bresenham's Line Algorithm

/ Published in: C
Save to your folder(s)

This is an implementation of the Bresenham's Line Algorithm in Computer Graphics Design in the C programming language.

Copy this code and paste it in your HTML
`//Shubham Mehta, Write a program to implement Bresenham's Line Algorithm.#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){	int gd, gm;	float x1, x2, y1, y2, dx, dy, p, xk, yk, slope;	clrscr();	printf("%s", "Please enter x1: ");	scanf("%f", &x1);	printf("%s", "Please enter y1: ");	scanf("%f", &y1);	printf("%s", "Please enter x2: ");	scanf("%f", &x2);	printf("%s", "Please enter y2: ");	scanf("%f", &y2);	gd=DETECT, gm;	initgraph(&gd, &gm, "C:\\TC\\BGI");	putpixel(x1, y1, RED);	slope=(y2-y1)/(x2-x1);	dx=x2-x1;	dy=y2-y1;	xk=x1;	yk=y1;	if(slope<1)	{		p=(2*dy)-dx;		for( ; x1<=x2; x1++)		{			if(p<0)			{				putpixel(++xk, yk, RED);				p+=(2*dy);			}			else			{				putpixel(++xk, ++yk, RED);				p=p+(2*dy)-(2*dx);			}		}	}	else	{		p=(2*dx)-dy;		for( ; y1<=y2; y1++)		{			if(p<0)			{				putpixel(xk, ++yk, RED);				p+=(2*dx);			}			else			{				putpixel(++xk, ++yk, RED);				p=p+(2*dx)-(2*dy);			}		}	}	getch();	closegraph();}`