Q:

Write a C Program to Draw Line using Bresenham’s Line Drawing Algorithm

0

Write a C Program to Draw Line using Bresenham’s Line Drawing Algorithm. Here’s simple Program to Draw Line using Bresenham’s Line Drawing Algorithm in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Below is the source code for C Program to Draw Line using Bresenham’s Line Drawing Algorithm which is successfully compiled and run on Windows System to produce desired output as shown below :

 
 


SOURCE CODE : :

/*  C Program to Draw Line using Bresenham’s Line Drawing Algorithm */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void swap(int &x, int &y) {
        int k = x;
        x = y;
        y = k;
}
void main() {
        int gd = DETECT, gm = DETECT, x1, x2, y1, y2, dx, dy, p, k;
        float m = 0;
        clrscr();
        printf("Enter the sarting point x1 & y1\n");
        scanf("%d%d", &x1, &y1);
        printf("Enter the end point x2 & y2\n");
        scanf("%d%d", &x2, &y2);
        dx = abs(x2 - x1);
        dy = abs(y2 - y1);
        m = (float) (y2 - y1) / (x2 - x1);
        initgraph(&gd, &gm, "");
        cleardevice();
        if (fabs(m) > 1) {
                swap(x1, y1);
                swap(x2, y2);
                swap(dx, dy);
        }
        if ((x1 > x2)) {
                x1 = x2;
                y1 = y2;
        }
        p = 2 * dy - dx;
        for (k = 0; k < abs(dx); k++) {
                if (p < 0) {
                        p = p + 2 * dy;
                } else {
                        if (m < 0)
                                        y1--; else
                                        y1++;
                        p = p + (2 * dy) - (2 * dx);
                }
                if (fabs(m) <= 1)
                            putpixel(x1++, y1, 15); else
                            putpixel(y1, x1++, 15);
        }
        getch();
}

Above is the source code for C Program to Draw Line using Bresenham’s Line Drawing Algorithm which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Graphic Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to Draw a Circle using Mid-Point... >>
<< Write a C Program to Draw Line using Graphics...