CGR 2.1 Line-Drawing
CGR 2.1 Line-Drawing
By
Mr. Parag R. Sali
Lecturer
Department of Computer Technology
SNJB’s Shri. Hiralal Hastimal ( Jain Brothers)
Polytechnic, Chandwad
Program Name: Computer Engineering Group
Program Code : CO/CM/CW
Semester : Third
Course Title : Computer Graphics
Course Code : 22318
5
4
Y
3
2
1
1 2 3 4 5
(0,0)
X
2.1 Basic concepts in line drawing
Line is a straight object with no curves, no thickness and it extends
in both directions without end.
5
4
Y
3
(2,2)
2
1
1 2 3 4 5
(0,0)
X
• There is difference between line and line segment. If it have ends it is
called a Line Segment.
• Lets consider only those points on a line which lie between two end
points P1 and P2 as shown in fig. This is called line segment.
P2
P1
The process of ‘Turning ON’ the pixel for a line segment is called as
Vector generation or Line Generation and the algorithms for them are
known as Vector generation algorithms or Line drawing algorithms.
Line Drawing Algorithms
Line Drawing
Algorithms
DDA Bresenham’s
Algorithm Algorithm
DDA Line Drawing Algorithm
dx=abs(x2-x1); i=1.0;
#include<stdio.h>
dy=abs(y2-y1); while(i<= step)
#include<conio.h> if(dx>=dy) {
#include<graphics.h> step = dx; putpixel(x,y,RED);
#include<math.h> else x = x + delx;
void main() step= dy; y = y + dely;
delx = dx/step; i = i+1;
{
dely = dy/step; printf("\n%f %f",x,y);
float x,y,i,x1,y1,x2,y2,dx,dy,step; x= x1 +0.5; }
float delx,dely; y= y1 +0.5; getch();
int gd=DETECT,gm; closegraph();
initgraph(&gd,&gm,"c:\\tc\\bgi"); }
printf("Enter values of x1,y1,x2,y2:");
scanf("%f %f %f %f",&x1,&y1,&x2,&y2);
1.Example of DDA
If a line is drawn from (5,6) to (8,12) with use of DDA. How many
points will needed to generate such line???
Solution :
X new = X old + 1
Y new = Y old + M
X Y
5 4
(5+1) 6 (4+0.4=4.4) 4
(6+1) 7 (4.4+0.4=4.8) 5
(7+1) 8 (4.8+0.4=5.2) 5
(8+1) 9 (5.2+0.4=5.6) 6
(9+1) 10 (5.6+0.4=6) 6
(10+1) 11 (6+0.4=6.4) 6
(11+1) 12 (6.4+0.4=6.8) 7
Advantages of DDA
It is simple and easy to implement algorithm
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 P..
Case 1 :If P > = 0 Then
P + 1 = P + 2dy - 2dx
Xnew = Xold + 1
Ynew = Yold + 1
Step 6 – We will repeat step 5 until we found the ending points of the
line.
Step 7 – Stop
10
9
8
7
6
5
3 4
1 2
1 2 3 4 5 6 7 8 9 10 11 12
Decision Parameter
1.Example of Bresenham’s Algorithm
A line has a starting point (9,18) and ending point (14,22). Apply the
bresenham’s line drawing algorithm to plot a line.
Update Pk is as,
Pk= Pk+2dy-2dx
= 5+2(4) -2(5) =3
Then next coordinates are
X new = 13+1 = 14
Y new = 21+1= 22
Plot 5th point = (14,22)
Ex 1:- (x1,y1)=(9,18)
(x2,y2)=(14,22)
Pk=1+8-10 = - 11 20
1 Case 2:-
If, Pk<0
Pk= -1+8 = 7 12 20 Pk=Pk+2dy
Xnew= Xold+1
Ynew=Yold
Plotting Points Table Graph of Plotting Points
Advantages
• It involves only integer arithmetic, so it is simple.
• It is easy to implement.
Disadvantages