0% found this document useful (0 votes)
16 views3 pages

p2 CGM

The document outlines an experiment to draw a line using the Digital Differential Analyzer (DDA) algorithm in computer graphics. It details the theory, key concepts, and step-by-step algorithm for implementing the DDA method, followed by a C code example for drawing the line. The code initializes graphics, takes user input for line endpoints, and uses the DDA algorithm to plot the line on the screen.

Uploaded by

Aditya Rai
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)
16 views3 pages

p2 CGM

The document outlines an experiment to draw a line using the Digital Differential Analyzer (DDA) algorithm in computer graphics. It details the theory, key concepts, and step-by-step algorithm for implementing the DDA method, followed by a C code example for drawing the line. The code initializes graphics, takes user input for line endpoints, and uses the DDA algorithm to plot the line on the screen.

Uploaded by

Aditya Rai
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/ 3

EXPERIMENT 2

Objective
Write a program to draw a line using DDA algorithm.

Theory
The Digital Differential Analyzer (DDA) algorithm is a simple and efficient

method for rasterizing lines in computer graphics. It works by calculating the

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.

 Steps: The number of intervals required to draw the line.

 Increments: The change in x and y for each step.

Algorithm
1. Input the two endpoints of the line segment, (x1, y1) (x1,

y1) and (x2, y2) (x2, y2).

2. Calculate:

 dx =x2−x1dx=x2−x1

 dy =y2−y1dy=y2−y1

3. Determine the number of steps:

 steps=max(∣dx∣,∣dy∣)steps=max(∣dx∣,∣dy∣)

4. Calculate increments:

 Xinc =dx/steps

 Yinc = dy/steps

5. Initialize starting point at (x1, y1) (x1, y1).

6. For each step from 0 to steps:

 Plot pixel at current (x, y) (x, y).

 Update xx and yy using increments.

7. End.

Code Part:
#include <graphics.h>

#include <stdio.h>

#include <math.h>
#include <conio.h>

int customRound(float value) {

return (int)(value + 0.5);

void drawLineDDA(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 xIncrement = dx / (float) steps;

float yIncrement = dy / (float) steps;

float x = x0;

float y = y0;

for (int i = 0; i <= steps; ++i) {

putpixel(customRound(x), customRound(y), WHITE);

x += xIncrement;

y += yIncrement;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

int x0 = 50, y0 = 100;

int x1 = 300, y1 = 200;

printf("Enter the X Co-ordinate of starting point of line: %d\n", x0);

printf("Enter the Y Co-ordinate of starting point of line: %d\n", y0);


printf("Enter the X Co-ordinate of ending point of line: %d\n", x1);

printf("Enter the Y Co-ordinate of ending point of line: %d\n", y1);

drawLineDDA(x0, y0, x1, y1);

getch();

closegraph();

return 0;

Output:

You might also like