Write a c program to implement Bresenham’s line drawing algorithm.
Bresenham's line drawing algorithm is an efficient algorithm used to draw a line between two points in a computer graphics system. Below is a C program that implements Bresenham's line drawing algorithm:
#include <stdio.h> #include <graphics.h> // Function to implement Bresenham's line drawing algorithm void drawLine(int x1, int y1, int x2, int y2) { int dx, dy, p, x, y; // Calculate differences dx = x2 - x1; dy = y2 - y1; // Initial decision parameter x = x1; y = y1; p = 2 * dy - dx; // Plot the initial point putpixel(x, y, WHITE); // Loop to plot the line while (x < x2) { x++; if (p < 0) { p = p + 2 * dy; } else { y++; p = p + 2 * (dy - dx); } putpixel(x, y, WHITE); } } int main() { int gd = DETECT, gm; int x1, y1, x2, y2; // Initialize graphics mode initgraph(&gd, &gm, NULL); // Input coordinates of the two points printf("Enter the coordinates of the first point (x1 y1): "); scanf("%d %d", &x1, &y1); printf("Enter the coordinates of the second point (x2 y2): "); scanf("%d %d", &x2, &y2); // Draw the line using Bresenham's algorithm drawLine(x1, y1, x2, y2); // Wait for a key press getch(); // Close the graphics mode closegraph(); return 0; }