[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

Bresenham's Line Drawing Algorithm

Uploaded by

derulo645
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Bresenham's Line Drawing Algorithm

Uploaded by

derulo645
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Bresenham’s Line drawing algorithm

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

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;
int sx, sy; // Declare here for Turbo C compatibility

dx = abs(x1 - x0);
dy = abs(y1 - y0);

sx = (x1 > x0) ? 1 : -1;


sy = (y1 > y0) ? 1 : -1;

x = x0;
y = y0;

if (dx > dy) // slope < 1


{
p = 2 * dy - dx;
while (x != x1)
{
putpixel(x, y, WHITE);
x += sx;

if (p >= 0)
{
y += sy;
p += 2 * dy - 2 * dx;
}
else
{
p += 2 * dy;
}
}
}
else // slope >= 1
{
p = 2 * dx - dy;
while (y != y1)
{
putpixel(x, y, WHITE);
y += sy;

if (p >= 0)
{
x += sx;
p += 2 * dx - 2 * dy;
}
else
{
p += 2 * dx;
}
}
}

putpixel(x1, y1, WHITE); // Draw the last pixel


}

int main()
{
int gdriver = DETECT, gmode;
int x0, y0, x1, y1;

initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

printf("Enter coordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter coordinates of second point: ");


scanf("%d%d", &x1, &y1);

drawline(x0, y0, x1, y1);

getch(); // Keeps graphics screen open


closegraph(); // Close graphics mode
return 0;
}

You might also like