Wednesday 22 July 2015

Line drawing using Bresenham's Line drawing Algorithm

Program to draw a line using Bresenham's Algorithm

Here's a program to draw a line in C language using Bresenham's line drawing algorithm.
This program asks you for starting and ending co-ordinates of line and outputs an animation effect by drawing a line using Bresenham's line drawing algorithm starting from the points given by user in the input .
Output Image will be something like this: 
Computer Graphics, C program for Bresenham's line drawing algorithm


#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void bresAlgo(int, int, int, int);

void main()
{
    int x1, y1, x2, y2;
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
   printf("Enter the X Co-ordinate of starting point of line: ");
    scanf("%d", &x1);
    printf("Enter the Y Co-ordinate of starting point of line: ");
    scanf("%d", &y1);
    printf("Enter the X Co-ordinate of ending point of line: ");
    scanf("%d", &x2);
    printf("Enter the X Co-ordinate of ending point of line: ");
    scanf("%d", &y2);
   bresAlgo(x1, y1, x2, y2);
    getch();
}

void bresAlgo(int x1, int y1, int xn, int yn)
{
    int dx = xn - x1, dy = yn - y1;
    int di = 2 * dy - dx;
    int ds = 2 * dy, dt = 2 * (dy - dx);
    putpixel(x1, y1, RED);
    while (x1 < xn)
    {
        x1++;
        if (di < 0)
        di = di + ds;
        else
        {
        y1++;
        di = di + dt;
        }
        putpixel(x1, y1, RED);
        delay(20);
    }

}

No comments:

Post a Comment