DDA Line Drawing Algorithm in C and C++

Here you will find out about dda line attracting calculation C and C++.

In Computer Graphics the main fundamental line drawing calculation is Digital Differential Analyzer (DDA) Algorithm.

A line interfaces two points. It is an essential component in designs. To draw a line, you need two points between which you can draw a line.

Advanced Differential Analyzer (DDA) Algorithm

Stage 1: Read the contribution of the 2 end purposes of the line as (x1, y1) and (x2, y2) with the end goal that x1 != x2 and y1 != y2

Stage 2: Calculate dx = x2 – x1 and dy = y2 – y1

Stage 3:

if(dx>=dy)

step=dx

else

step=dy

Stage 4: xin = dx/step and yin = dy/step

Stage 5: x = x1 + 0.5 and y = y1 + 0.5

Stage 6:

for(k = 0; k < step; k++)

{

x = x + xin

y = y + yin

putpixel(x, y)

}

Program for DDA Line Drawing Algorithm in C

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
 
void main( )
{
	float x,y,x1,y1,x2,y2,dx,dy,step;
	int i,gd=DETECT,gm;
 
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 
	printf("Enter the value of x1 and y1 : ");
	scanf("%f%f",&x1,&y1);
	printf("Enter the value of x2 and y2: ");
	scanf("%f%f",&x2,&y2);
 
	dx=abs(x2-x1);
	dy=abs(y2-y1);
 
	if(dx>=dy)
		step=dx;
	else
		step=dy;
 
	dx=dx/step;
	dy=dy/step;
 
	x=x1;
	y=y1;
 
	i=1;
	while(i<=step)
	{
		putpixel(x,y,5);
		x=x+dx;
		y=y+dy;
		i=i+1;
		delay(100);
	}
 
	closegraph();
}

Output

DDA Line Drawing Algorithm in C and C++

Program for DDA Line Drawing Algorithm in C++

#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
 
void main( )
{
	float x,y,x1,y1,x2,y2,dx,dy,step;
	int i,gd=DETECT,gm;
 
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 
	cout<<"Enter the value of x1 and y1 : ";
	cin>>x1>>y1;
	cout<<"Enter the value of x2 and y2: ";
	cin>>x2>>y2;
 
	dx=abs(x2-x1);
	dy=abs(y2-y1);
 
	if(dx>=dy)
		step=dx;
	else
		step=dy;
 
	dx=dx/step;
	dy=dy/step;
 
	x=x1;
	y=y1;
 
	i=1;
	while(i<=step)
	{
		putpixel(x,y,5);
		x=x+dx;
		y=y+dy;
		i=i+1;
		delay(100);
	}
 
	closegraph();
}

Remark beneath in the event that you have any questions related above calculation.

Leave a Comment

error: Alert: Content is protected!!