p2 CGM
p2 CGM
Objective
Write a program to draw a line using DDA algorithm.
Theory
The Digital Differential Analyzer (DDA) algorithm is a simple and efficient
differences in the x and y coordinates of the endpoints of the line segment and
determining how many steps are needed to generate the line. The algorithm
then increments the x and y coordinates to plot each point along the line.
Key Concepts:
Endpoints: The start and end points of the line segment.
Algorithm
1. Input the two endpoints of the line segment, (x1, y1) (x1,
2. Calculate:
dx =x2−x1dx=x2−x1
dy =y2−y1dy=y2−y1
steps=max(∣dx∣,∣dy∣)steps=max(∣dx∣,∣dy∣)
4. Calculate increments:
Xinc =dx/steps
Yinc = dy/steps
7. End.
Code Part:
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
int dx = x1 - x0;
int dy = y1 - y0;
float x = x0;
float y = y0;
x += xIncrement;
y += yIncrement;
int main() {
getch();
closegraph();
return 0;
Output: