0% found this document useful (0 votes)
17 views

lab

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

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)
17 views

lab

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

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

ID: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;

// to calculate dx and dy
dx = x2 - x1;
dy = y2 - y1;

// to determine the number of steps for the DDA algoritm


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

// to calculate the increment in x and y for each steps performed


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