DEPARTMENT OF COMPUTER
ENGINEERING
Subject: Computer Graphics Subject Code: 313001
Semester: 03 Course: CO3KA
Laboratory No: V120 Name of Subject Teacher: Mr. Pradeep Shirke
Name of Student: Chintan Gupte Roll ID: 23203A0061
Experiment No 2
Title of Experiment Write a C program to draw line using DDA algorithm.
Program:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main() { float x1, x2, y1, y2, x, y,
step, dx, dy;
int i, gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "C:\\TC\\BGI");
printf("enter the value of x1,y1:");
scanf("%f%f", &x1, &y1);
printf("enter the value of x2,y2:");
scanf("%f%f", &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) >=
abs(dy)) step =
abs(dx); else
step = abs(dy);
dx = dx / step;
dy = dy / step;
x = x1; i = 1;
y = y1;
while (i <= step) {
putpixel(x, y, 15);
x = x + dx; y=y
+ dy; i = i + 1;
delay(70);
}
getch();
closegraph(); return
0;
}
OUTPUT:-
EXERCISE:-
1. Give following values for every iteration of DDA algorithm to draw a line from (3,4)
to(6,8).
dx dy steps Xinc Yinc
3.0 4.0 0 0.75 1.0
3.75 5.0 1 0.75 0.75
4.5 6.0 2 0.75 0.75
5.25 7.0 3 0.75 0.75
6.0 8.0 4 0.75 0.75
2. Give following values for every iteration of DDA algorithm to draw a line from (5,-5)
to(-12,-12).
x y steps Xinc Yinc
-5.0 -5.0 0 -1.0 -1.0
-6.0 -6.0 1 -1.0 -1.0
-7.0 -7.0 2 -1.0 -1.0
-8.0 -8.0 3 -1.0 -1.0
-9.0 -9.0 4 -1.0 -1.0
-10.0 -10.0 5 -1.0 -1.0
-11.0 -11.0 6 -1.0 -1.0
-12.0 -12.0 7 -1.0 -1.0
3. Implement DDA algorithm to Draw line using For loop.
#include <graphics.h>
#include <math.h>
#include <stdio.h>
void ddaLine(int x0, int y0, int x1, int y1) {
int dx = x1 - x0;
int dy = y1 - y0;
int steps = (abs(dx) > abs(dy)) ? abs(dx) : abs(dy);
float x_inc = (float)dx / steps;
float y_inc = (float)dy / steps;
float x = x0;
float y = y0;
for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += x_inc;
y += y_inc;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x0 = 100, y0 = 100; // Starting point
int x1 = 400, y1 = 300; // Ending point
ddaLine(x0, y0, x1, y1);
getch();
closegraph();
return 0;
}
PRACTICAL RELATED QUESTION:-
1. Define the term Rasterization.
Rasterization is a fundamental process in computer graphics that involves
converting vector-based images into a pixel-based format suitable for display or printing.
This technique is essential for rendering images on devices like monitors and printers,
which operate using a grid of pixels.
2. Write slope intercept form of a line.
The slope-intercept form of a line is:
y = mx + c
where:
• m is the slope of the line,
• c is the y-intercept (the point where the line crosses the y-axis).
3. Write advantage and disadvantage of DDA algorithm.
Advantages Disadvantages
Simple and easy to implement Vulnerable to rounding errors
Efficient for basic 3D line-drawing tasks Limited precision
Handles lines of varying steepness Less efficient when dealing with vertical lines
Faster than other line drawing algorithms Not suitable for drawing thick lines
No special hardware requirements Increased time complexity with floating point
operations
Marks Obtained Dated signature of
Teacher
Process Product Total (25)
Related (10) Related (15)