Draw-Line-using-Bresenham-s-Algorithm.md 1.05 KB
Newer Older
Rohan Sakhale's avatar
Rohan Sakhale committed
1 2
---
title: Draw Line using Bresenham's Algorithm
3
date: 2011-11-12 12:25:40
Rohan Sakhale's avatar
Rohan Sakhale committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
categories:
	- Codes
tags:
	- code-example
	- c/cpp
---
This is another algorithm which can be used to draw a line and this algorithm is better and gives a more accurate output than DDA algorithm we previously discussed

This is another example shown in Computer Graphics using C Language

#### Bresenhams.cpp

```cpp
#include "stdio.h"
#include "conio.h"
#include "graphics.h"


void value(int x1,int y1,int x2,int y2)
{
	int dx,dy,x,y,p,k;
	dx = x2 - x1;
	dy = y2 - y1;
	x = x1;
	y = y1;
	p = (2 * dy) - (dx);
	for(k=0;k<dx;k++)
	{
		if(p<0)
		{
			x++;
			putpixel(x,y,WHITE);
			p = p + (2 * dy);
		}
		else
		{
			x++;
			y++;
			putpixel(x+1,y+1,WHITE);
			p = p + (2 * dy) - (2 * dx);
		}
	}
}

void main()
{
	int gd,gm,xa,xb,ya,yb;
	detechgraph(&gd,&gm);
	gd = DETECT;
	initgraph(&gd,&gm,"C:\\TC\\BGI");
	printf("Enter the starting point:=> ");
	scanf("%d %d",&xa,&ya);
	printf("Enter the end point:=> ");
	scanf("%d %d",&xb,&yb);
	value(xa,ya,xb,yb);
	getch();
	closegraph();
}
```