Return to Snippet

Revision: 68878
at March 10, 2015 03:17 by ZeusEm


Initial Code
//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();
}

Initial URL


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

Initial Title
Graphic Design | Bresenham's Line Algorithm

Initial Tags


Initial Language
C