0% found this document useful (0 votes)
13 views43 pages

CGR 2.1 Line-Drawing

The document is a course outline for a Computer Graphics class, detailing key concepts in line drawing, including algorithms such as Digital Differential Analyzer (DDA) and Bresenham's algorithm. It explains the process of scan conversion and character generation methods, along with examples and advantages/disadvantages of the discussed algorithms. The content is structured to guide students through the fundamental principles of computer graphics and their practical applications.

Uploaded by

aherpushkar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views43 pages

CGR 2.1 Line-Drawing

The document is a course outline for a Computer Graphics class, detailing key concepts in line drawing, including algorithms such as Digital Differential Analyzer (DDA) and Bresenham's algorithm. It explains the process of scan conversion and character generation methods, along with examples and advantages/disadvantages of the discussed algorithms. The content is structured to guide students through the fundamental principles of computer graphics and their practical applications.

Uploaded by

aherpushkar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Computer Graphics

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

Basic Concepts in line drawing


Unit-II

Raster Scan Graphics


(18 M)
Unit 2
2.1 Basic concepts in line drawing : Line drawing
algorithms: Digital Differential Analyzer (DDA)
algorithm, Bresenham’s algorithm.
2.2 Circle generation algorithms : Symmetry of circle,
Bresenham’s circle drawing algorithm.
2.3 Polygons : Types of polygons, inside-outside test,
Polygon Filling : Seed fill algorithms : Flood fill,
Boundary fill, scan line algorithms.
2.4 Scan conversion, Frame Buffers.
2.5 Character generation methods: stroke, starburst,
bitmap.
Scan Conversion
• The process in which the object is represented as the collection of
discrete pixels is called Scan Conversion.

• Scan conversion of a point is nothing but turning ON a particular point


or pixel.

• We know that pixel is a smallest element of the screen which can be


controlled i.e., either on/off. If we wish to display a point P(3, 5)then
it means we want to illuminate this pixel.
(3,5)

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.

 A line connects two points. It is a basic elements in graphics. To


draw a line, you need two points between which you can draw a
line.

 In the following algorithms, we refer the one point of a line


as X1, Y1
and the second point of line
as X2, Y2

 Where x represents the horizontal distance and y represents vertical


distance.
(3,5)

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

• Digital Differential Analyzer (DDA)


algorithm is the simple line generation
algorithm based on increment method
which is explained step by step here.
DDA Algorithm

Step 1 – Start Algorithm

Step 2 –Get the input of two end points (X1,Y1)and (X2,Y2).

Step 3 –Calculate dx = x2-x1


dy = y2-y1
Also find Line of Slope as M,
M=dy/dx
Step 4 – If ABS(dx) > ABS(dy)
Then step = abs(dx)
Else
step=abs(dy)

Step 5 –Calculate the increment in X coordinate and Y coordinate.


X increment = dx/ step
Y increment = dy/ step

Step 6-Put the pixel by successfully incrementing x and y coordinates


accordingly and complete the drawing of the line.
Step 7 – Set pixel (x,y)

Step 8 – End algorithm


DDA line drawing Program

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 :

Step 1 : Start Algorithm

Step 2 : Declare x1,y1,x2,y2,dx,dy,x,y as integer variables


x1 = 5, y1 = 6;
x2 = 8, y2 = 12;
Step 3 : Enter value of x1,y1,x2,y2
dx= x2-x1 dy = y2-y1
=8-5 =12-6
=3 =6
Step 4 – Find the value of Decision Parameter M as
M = dy/dx
= 6/3
M= 2

Step 5 – Calculate the number of steps


As |dx| < |dy| = 3<6, so number of steps is = |dy|=6

Check condition M <1 , M > 1 and M = 1 then


Here, case 2 is satisfied.. Then

X new= X old + 1/m 1/m


Y new = Y old + 1 ½=0.5
Step 6 – Find next points as follows..
X new= 5+0.5=5.5
Y new= 6+1= 7
Plot 2nd point = (6,7)

Similarly add For next points..


X new= 5.5+0.5=6
Y new= 7+1 = 8
Plot 3rd point = (6,8)

For next points..


X new= 6+0.5=6.5
Y new= 8+1 =9
Plot 4th point = (7,9)
For next points..
X new= 6.5+0.5=7
Y new= 9+1 = 10
Plot 5 point = (7,10)
th

For next points..


X new= 7+0.5=7.5
Y new= 10+1 = 11
Plot 6 point = (8,11)
th
Plotting Points Table Graph of Plotting Points
2.Example of DDA
If a line is drawn from (5,4) to (12,7) with use of DDA. How many
points will needed to generate such line???
Solution :

Step 1 : Start Algorithm

Step 2 : Declare x1,y1,x2,y2,dx,dy,x,y as integer variables


x1 = 5, y1 = 4;
x2 = 12, y2 = 7;
Step 3 : Enter value of x1,y1,x2,y2
dx= x2-x1 dy = y2-y1
=12-5 =7-4
=7 =3
Step 4 – Find the value of Decision Parameter M as
M = dy/dx
= 3/7
M= 0.4

Step 5 – Calculate the number of steps


As |dx|>|dy| = 7>3, so number of steps is => |dx|=7

Check condition M <1 , M > 1 and M = 1 then


Here, case 2 is satisfied.. Then

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

 It requires no special skill for implementation.

 The DDA algorithms is faster method for calculating


pixel position than direct use of the line
equation(y=mx + b).
Disadvantages of DDA
 It deals with the rounding off operation and floating
point arithmetic so it has high time complexity.

 As it is orientation dependent, so it has poor endpoint


accuracy

 Because of round off, errors are introduced and causes the


calculated pixel position to drift away from the true line
path.
Bresenham’s algorithm.
 The Bresenham’s algorithm is another incremental
scan conversion algorithm.

 The big advantages of this algorithm is that, it uses


only integer calculations. Moving across X-axis in
unit intervals and at each step choose between two
different y coordinates.

 Following are the steps of Bresenham’s Line


Generation algorithm..
Steps for Bresenham’s Algorithm
Step 1 – Start

Step 2 – Now, we consider starting point as (x1,y1) and


ending points (x2,y2)

Step 3 - Now, we have to calculate dx and dy as


dx = x2-x1
and dy = y2-y1
Step 4 – Now, we will calculate the decision parameter P, with
following formula..
P = 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 P..
Case 1 :If P > = 0 Then
P + 1 = P + 2dy - 2dx
Xnew = Xold + 1
Ynew = Yold + 1

Case 2 :If P < 0 Then


P+ 1 = P + 2dy
Xnew =Xold + 1
Ynew= Yold

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.

Solution : We have two coordinates

Starting Point = (x1,y1) = (9,18)


Ending Point = (x2,y2) =(14,22)

Step 1 : First, we calculate dx, dy.


dx= x2-x1 =14-9 =5 2dx=10
dy= y2-y1 =22-18 =4 2dy=8
No of steps= 5
Step 2 : Now, we are going to calculate the decision parameter
(Pk)
Pk =2dy-dx
Pk =2 *4-5= 3
The value of Pk = 3

Step 3 : Now, we will check both the cases.


If Pk >= 0 Then
Case 1 is satisfied.. Thus
Pk+1= Pk+2dy-2dx
= 3+(2*4)-(2*5) =1
X new= Xk+1 X & Y increment
X new =9+1 = 10
Y new =18+1 = 19
Plot 2nd point =(10,19)
Step 4 : Now move to next step. We will calculate the coordinates
until we reach the end point of the line.

For next point we have again calculate Pk as..


Pk=1+2*4-2*5
= 1+ 8- 10= -1
X new = 10+1= 11
Y new = 19 +1 = 20
Plot 3rd point = (11,20)
For next point we have again calculate Pk as..
In previous iteration the value of Pk = -1 < 0 then..
Pk= Pk+ 2dy
= -1 + 2*4= 7
Then next coordinates are
X new = 11+1 =12
Y new = 20
Plot 3 point = (12,20)
rd

For next point we have again calculate Pk as..


In previous iteration the value of Pk = 7 >0 Then,
Update Pk is as,
Pk= Pk+2dy-2dx
= 7+2(4) -2(5) =5
Then next coordinates are
X new = 12+1 = 13
Y new = 20+1= 21
Plot 4th point = (13,21)

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)

dx= x2-x1= 14-9= 5 2dx=2*5=10 No of Steps:=5


dy=y2-y1= 22-18= 4 2dy=2*4=8
Pk=2dy-dx= 8-5= 3
Pk X Y Case 1:-
If, Pk>=0
Pk=3 9 18 Pk= Pk+2dy-2dx
Pk=3+8-10 = 10 19 Xnew= Xold+1
1 Ynew=Yold+1

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 faster as compared to DDA (Digital Differential Analyzer)


because it does not involve floating point calculations like
DDA Algorithm.

• It is easy to implement.
Disadvantages

• Though it improves the accuracy of generated points


but still the resulted line is not smooth.

• This algorithm is for the basic line drawing.


Bresenham’s line drawing Program
#include<stdio.h> dx=abs(x2-x1); else
#include<conio.h> dy=abs(y2-y1); {
p=2*dy-dx; putpixel(x1,y1,7);
#include<graphics.h>
//printf("xy is %f %f %f %f",x1,y1,x2,y2); p=p+2*dy;
#include<math.h> i=0; }
void main() while(i<dx) x1=x1+1;
{ { i++;
int i,x,y,x1,y1,x2,y2,dx,dy,p; if(p>=0) }
{ getch();
int gd=DETECT,gm;
putpixel(x1,y1,7); closegraph();
initgraph(&gd,&gm,"c:\\tc\\bgi"); y1=y1+1; }
printf("Enter values of x1,y1,x2,y2:"); p=p+2*dy-2*dx;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2); }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy