SlideShare a Scribd company logo
3
Most read
DDA line drawing algorithm
Stage 1: Read the contribution of the 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);
}
C program for DDA
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i, gd=DETECT,gm;
initgraph(&gd,&gm,"c:tcbgi");
printf("Enter the starting coordinates");
scanf("%f%f",&x1,&y1);
printf("Enter the ending coordinates");
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+dx;
i=i+1;
delay(100);
}
getch();
closegraph();
}
Bresenham’s line drawing algorithm
Step 1: Start.
Step 2: Now, we consider Starting point as (x1, y1) and ending point (x2, y2).
Step 3: Now, we have to calculate dx and dy.
dx = x2-x1
dy = y2-y1
m = dy/dx
Step 4: Now, we will calculate the decision parameter pk with following formula.
pk = 2dy-dx
Step 5: The initial coordinates of the line are (xk, yk), and the next coordinates are (xk+1,
yk+1). Now, we are going to calculate two cases for decision parameter pk
Case 1: If
pk < 0
Then
pk+1 =pk +2dy
xk+1 = xk +1
yk+1 = yk
Case 2: If
pk >= 0
Then
pk+1 =pk +2dy-2dx
xk+1 =xk +1
yk+1 =yk +1
Step 6: We will repeat step 5 until we found the ending point of the line and the total number of
iterations =dx-1.
Step 7: Stop.
C program to implement Bresenham’s line drawing
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
int main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
printf("nntEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("nntEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:tcbgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

More Related Content

PDF
2D array
PDF
Digital Differential Analyzer Line Drawing Algorithm in C
DOC
Computer graphics
DOCX
Computer Graphics Lab File C Programs
PDF
Computer graphics lab report with code in cpp
DOCX
Computer graphics lab assignment
PDF
Computer graphics lab manual
PPTX
Gauss in java
2D array
Digital Differential Analyzer Line Drawing Algorithm in C
Computer graphics
Computer Graphics Lab File C Programs
Computer graphics lab report with code in cpp
Computer graphics lab assignment
Computer graphics lab manual
Gauss in java

What's hot (19)

DOCX
Cg my own programs
PPTX
Digital differential analyzer
DOC
SE Computer, Programming Laboratory(210251) University of Pune
DOCX
Wap in c to draw a line using DDA algorithm
DOC
COMPUTER GRAPHICS LAB MANUAL
TXT
Dvst
PPTX
Cse 121 presentation on matrix [autosaved]
PPT
computer graphics practicals
PPTX
Mat lab lecture part 1
PDF
Rsa Signature: Behind The Scenes
PDF
Solution of matlab chapter 3
PPT
Arrays
PPT
Bresenham's line algo.
DOCX
Computer graphics programs in c++
DOCX
Experement no 6
PPT
Thesis PPT
PPT
PDF
Matlab assignment
DOCX
Advance java
Cg my own programs
Digital differential analyzer
SE Computer, Programming Laboratory(210251) University of Pune
Wap in c to draw a line using DDA algorithm
COMPUTER GRAPHICS LAB MANUAL
Dvst
Cse 121 presentation on matrix [autosaved]
computer graphics practicals
Mat lab lecture part 1
Rsa Signature: Behind The Scenes
Solution of matlab chapter 3
Arrays
Bresenham's line algo.
Computer graphics programs in c++
Experement no 6
Thesis PPT
Matlab assignment
Advance java
Ad

Similar to Line drawing algorithm (20)

DOC
Computer graphics
PDF
Computer Graphics_Module 2_Output Primitives.pdf
PDF
Line drawing Algorithm DDA in computer Graphics.pdf
DOC
Computer Aided Manufacturing Design
PPTX
DDA algorithm
PDF
C++ TUTORIAL 9
PDF
2Bytesprog2 course_2014_c9_graph
PPTX
Comuter graphics dda algorithm
DOCX
Computer graphics File for Engineers
PPTX
dda-line-algorithm.pptx of computer graphics
PDF
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
PPTX
Dda algorithm
PDF
PPT
Lect3cg2011
PPTX
4 CG_U1_M3_PPT_4 DDA.pptx
PDF
raster algorithm.pdf
PDF
Open GL T0074 56 sm4
PPT
PDF
Line circle draw
PPTX
Bresenham's line drawing algorithm
Computer graphics
Computer Graphics_Module 2_Output Primitives.pdf
Line drawing Algorithm DDA in computer Graphics.pdf
Computer Aided Manufacturing Design
DDA algorithm
C++ TUTORIAL 9
2Bytesprog2 course_2014_c9_graph
Comuter graphics dda algorithm
Computer graphics File for Engineers
dda-line-algorithm.pptx of computer graphics
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
Dda algorithm
Lect3cg2011
4 CG_U1_M3_PPT_4 DDA.pptx
raster algorithm.pdf
Open GL T0074 56 sm4
Line circle draw
Bresenham's line drawing algorithm
Ad

More from A. S. M. Shafi (20)

DOCX
Data Warehouse Schema (Star, Snowflake).docx
PDF
Correlation Analysis in Machine Learning.pdf
PDF
Naive Bayes and Decision Tree Algorithm.pdf
PDF
Frequent Pattern Growth Mining Algorithm.pdf
PDF
Direct Hashing and Pruning Algorithm in Data MIning.pdf
PDF
Association Rule Mining with Apriori Algorithm.pdf
PDF
HITS Algorithm in Data and Web MIning.pdf
PDF
Page Rank Algorithm in Data Mining and Web Application.pdf
PDF
K Nearest Neighbor Classifier in Machine Learning.pdf
PDF
K Means Clustering Algorithm in Machine Learning.pdf
PDF
2D Transformation in Computer Graphics
PDF
3D Transformation in Computer Graphics
PDF
Projection
PDF
2D Transformation
PDF
Fragmentation
PDF
File organization
PDF
Bankers algorithm
PDF
RR and priority scheduling
PDF
Fcfs and sjf
PDF
Applications of stack
Data Warehouse Schema (Star, Snowflake).docx
Correlation Analysis in Machine Learning.pdf
Naive Bayes and Decision Tree Algorithm.pdf
Frequent Pattern Growth Mining Algorithm.pdf
Direct Hashing and Pruning Algorithm in Data MIning.pdf
Association Rule Mining with Apriori Algorithm.pdf
HITS Algorithm in Data and Web MIning.pdf
Page Rank Algorithm in Data Mining and Web Application.pdf
K Nearest Neighbor Classifier in Machine Learning.pdf
K Means Clustering Algorithm in Machine Learning.pdf
2D Transformation in Computer Graphics
3D Transformation in Computer Graphics
Projection
2D Transformation
Fragmentation
File organization
Bankers algorithm
RR and priority scheduling
Fcfs and sjf
Applications of stack

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPH.pptx obstetrics and gynecology in nursing
Microbial disease of the cardiovascular and lymphatic systems
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Basic Mud Logging Guide for educational purpose
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Line drawing algorithm

  • 1. DDA line drawing algorithm Stage 1: Read the contribution of the 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); } C program for DDA #include<stdio.h> #include<conio.h> #include<math.h> #include<dos.h> #include<graphics.h> void main() { float x,y,x1,y1,x2,y2,dx,dy,step; int i, gd=DETECT,gm; initgraph(&gd,&gm,"c:tcbgi");
  • 2. printf("Enter the starting coordinates"); scanf("%f%f",&x1,&y1); printf("Enter the ending coordinates"); 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+dx; i=i+1; delay(100); } getch(); closegraph(); }
  • 3. Bresenham’s line drawing algorithm Step 1: Start. Step 2: Now, we consider Starting point as (x1, y1) and ending point (x2, y2). Step 3: Now, we have to calculate dx and dy. dx = x2-x1 dy = y2-y1 m = dy/dx Step 4: Now, we will calculate the decision parameter pk with following formula. pk = 2dy-dx Step 5: The initial coordinates of the line are (xk, yk), and the next coordinates are (xk+1, yk+1). Now, we are going to calculate two cases for decision parameter pk Case 1: If pk < 0 Then pk+1 =pk +2dy xk+1 = xk +1 yk+1 = yk Case 2: If pk >= 0 Then pk+1 =pk +2dy-2dx xk+1 =xk +1 yk+1 =yk +1 Step 6: We will repeat step 5 until we found the ending point of the line and the total number of iterations =dx-1. Step 7: Stop.
  • 4. C program to implement Bresenham’s line drawing # include <stdio.h> # include <conio.h> # include <graphics.h> int main() { int dx,dy,x,y,p,x1,y1,x2,y2; int gd,gm; printf("nntEnter the co-ordinates of first point : "); scanf("%d %d",&x1,&y1); printf("nntEnter the co-ordinates of second point : "); scanf("%d %d",&x2,&y2); dx = (x2 - x1); dy = (y2 - y1); p = 2 * (dy) - (dx); x = x1; y = y1; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:tcbgi"); putpixel(x,y,WHITE); while(x <= x2) { if(p < 0) { x=x+1; y=y; p = p + 2 * (dy); }
  • 5. else { x=x+1; y=y+1; p = p + 2 * (dy - dx); } putpixel(x,y,WHITE); } getch(); closegraph(); }