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

VU2F2324071 AIM: Implemetation of DDA Line Drawing Algorithm

The document presents a C program that implements the Digital Differential Analyzer (DDA) line drawing algorithm using graphics.h library. It includes functions for rounding numbers and drawing a line based on user-provided coordinates. The program initializes graphics mode, takes input for two points, and visually draws the line on the screen.

Uploaded by

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

VU2F2324071 AIM: Implemetation of DDA Line Drawing Algorithm

The document presents a C program that implements the Digital Differential Analyzer (DDA) line drawing algorithm using graphics.h library. It includes functions for rounding numbers and drawing a line based on user-provided coordinates. The program initializes graphics mode, takes input for two points, and visually draws the line on the screen.

Uploaded by

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

PRATHAMESH KAMBLI

VU2F2324071
AIM: Implemetation of DDA line drawing algorithm
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

int my_round(float num) {


return (num > 0.0) ? (int)(num + 0.5) : (int)(num - 0.5);
}

void drawDDA(int x1, int y1, int x2, int y2) {


int i,dx, dy, steps;
float x_inc, y_inc, x = x1, y = y1;

// Calculate dx and dy
dx = x2 - x1;
dy = y2 - y1;

// Determine the number of steps


if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);

// Calculate the increment in x and y for each step


x_inc = dx / (float)steps;
y_inc = dy / (float)steps;

// Draw the line


putpixel(my_round(x), my_round(y), WHITE);
for ( i = 0; i < steps; i++) {
x += x_inc;
y += y_inc;
putpixel(my_round(x), my_round(y), WHITE);
delay(10); // Slow down the drawing for better visibility
}
}

int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2;

// Initialize the graphics mode


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Take input for the coordinates of the line
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 DDA


drawDDA(x1, y1, x2, y2);

// Close the graphics mode


getch();
closegraph();
return 0;
}

You might also like