Software & Finance Forums

### Author Topic: Turbo C - Bresenham Line Drawing Algorithm (text mode output of line)  (Read 2804 times)

#### kathir

• Sr. Member
• Posts: 283
##### Turbo C - Bresenham Line Drawing Algorithm (text mode output of line)
« on: December 13, 2011, 12:54:10 pm »
The source code for Bresenham Line Algorithm is given here.

The formula is,

y = m * (x - x0) + y0

where m = slope (y1 - y0) / (x1 - x0)
x0 and y0 are the starting points
x is the next x axis point in the loop
y is the corresponding y axis point to the x

Code:
```//Source Code Listing

#include <stdio.h>
#include <conio.h>

void Draw_Line(int left, int top, int right, int bottom)
{
// Bresenham Line Algorithm
// y = m * (x - x0) + y0
// where m = slope
// x0 and y0 are the starting points
// x is the next x axis point in the loop
// y is the corresponding y axis point to the x

int x = 0, y = 0;
double m = ((double) bottom - top) / ((double) right - left);
for(x = left; x <= right; x++)
{
y = m * (x - left) + top;
gotoxy(x, y);
printf("x");
}

}

void main()
{
Draw_Line(10,10,20,20);
Draw_Line(10,20,20,10);

Draw_Line(30,0,60,20);
Draw_Line(30,20,60,0);

gotoxy(0,0);
}

```
« Last Edit: December 13, 2011, 12:56:12 pm by kathir »

Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.