Second Year Diploma Courses in Computer Science & Engineering / Computer
Engineering / Computer Technology / Information Technology Branch.
Computer Graphics
As per MSBTE ‘I’ Scheme Syllabus
CGR-22318
Unit-II
RASTER SCAN GRAPHICS
Total Marks- 12
Contents:
2.1 Basic concepts in line drawing,:
2.1.1 Line.
2.1.2 Drawing algorithms:
1. Digital Differential Analyzer (DDA) algorithm,
2. Bresenham's algorithm.
2.2 Circle generating algorithms:
2.2.1 Symmetry of circle,
2.2.2 Bresenham’s circle drawing algorithm.
2.3 Polygons –
2.3.1 Types of polygons,
2.3.2 inside-outside test,
2.3.3 Polygon Filling:
A. Seed fill algorithms:
A.1 Flood fill.
A.2 Boundary fill,
B. scan line algorithms
2.4 Scan conversion,
2.5 Frame Buffers.
2.6 Character generation methods:
1. stroke,
2. starburst,
3. bitmap.
CGR-22318 www.freestudyroom.xyz Page 1
, Unit-II Raster Scan Graphics
Unit-II Raster Scan Graphics
2.1 Basic Concept in Line Drawing:
2.1.1 Line: Line is a straight object with no curves, no thickness and it extends in both
directions without end. If it does have ends it is called as a line segment.
The process is turning on pixels for a line segment is called vector generation or line
generation and algorithm for them is known as vector generation algorithm or line drawing
algorithm.
Problems of vector generation:
Line is straight but width is no constant.
Brightness of line dependent on orientation of the line.
Calculate only an approximate line length.
Reduce the calculations using simple integer arithmetic.
2.1.2 Line Drawing Algorithms:
Line drawing algorithms are classified as Digital Differential Analyzer (DDA) algorithm
and Bresenham’s algorithm.
1. DDA Algorithm (Digital Differential Analyzer)
DDA algorithm is an incremental scan conversion method. Here we perform calculations at
each step using the results from the previous step.
We know that the slop of a straight line is given as -
Slop(
Where
m=Slop of line
dy=distance of y co-ordinates
dx=distance of x co-ordinates
The above differential equation can be used to obtain a rasterized straight line. For any given
x interval x along a line, we can compute the corresponding y interval y.
Similarly, we can obtain the x interval x corresponding to a specified y.
Once the intervals are known the values for next x and next y on the straight line can be
obtained.
Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm which
is explained step by step here.
Vector generation/DDA Line Algorithm –
1. Read line end points(x1,y1) and (x2,y2) such that they are not equal.
(If equal then plot that point and exit)
2. Compute Horizontal and Vertical differences-
dy=|y2-y1| and dx=|x2-x1|
3. if (|dx|>=|dy) then
steps=dx
else
steps=dy
4. Increment x, xinc=dx/steps
CGR-22318 www.freestudyroom.xyz Page 2
, Unit-II Raster Scan Graphics
5. Increment y, yinc=dy/steps
6. x=x1+0.5*sign(dx)
y=y1+0.5*sign(dy)
(here, sign function makes the algorithm work in all quadrant.)
7. for(i=0;i<steps;i++)
{
Plot (Integer(x), Integer(y))
x1=x+xinc
y1=y+yinc
}
8. Stop
‘C’ Code for DDA Line Drawing Algorithm:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int sign(float val)
{
if(val<0)
return -1;
else
return 1;
}
int main()
{
int gd=DETECT,gm,i;
float x,y,x1,y1,x2,y2,dx,dy,xinc,yinc,steps;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter x1,y1");
scanf("%f%f",&x1,&y1);
printf("Enter x2,y2");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
steps=dx;
}
else
{
steps=dy;
}
xinc=dx/steps;
yinc=dy/steps;
x=x1+0.5*sign(dx); (Initial value with adding the factor 0.5 to make round value for x,
also sign function work in all quadrant.)
y=y1+0.5*sign(dy); (Initial value with adding the factor 0.5 to make round value for
y, also sign function work in all quadrant.)
putpixel(x,y,WHITE);
for(i=1;i<=steps;i++)
Example 1: Consider the line from (0,0) to (4,6). Use the simple DDA algorithm to rasterize
this line.
Solution: Evaluating steps 1 to 5 in the DDA algorithm we have,
We have,
xl = 0, y1 = 0
x2 = 4, y2 = 6
dx=|x2 – x1| =4-0=4
dy =|y2 – yl|=6-0=6
here, dy is greater than dx
so, steps = dy = 6
Increment x, xinc=dx/steps=4/6=0.667
Increment y, yinc=dy/steps=6/6=1
Initial value with adding the factor 0.5 to make round value for x & y, also sign function work
in all quadrant.
x=0 + 0.5 sign (dx) = 0.5
y =0 + 0.5 sign (dy) = 0.5
Plot integer now:
1. Plot (0, 0), x=0.5, y=0.5
2. Plot (1, 1), x=x+ dx =0.5+0.667=1.167, y=y+ dy=0.5+1=1.5
3. Plot (1, 2), x=x+ dx =1.167+0.667=1.833 y=y+ dy =1.5+1=2.5
4. Plot (2, 3), x=x+ dx =1.833+0.667=2.5 y=y+ dy =2.5+1=3.5
5. Plot (3, 4), x=x+ dx =2.5+0.667=3.167 y=y+ dy =3.5+1=4.5
6. Plot (3, 5), x=x+ dx =3.167+0.667=3.833 y=y+ dy =4.5+1=5.5
7. Plot (4, 6) x=x+ dx =3.833+0.667=4.5 y=y+ dy =5.5+1=6.5
Tabulating the result of each iteration-
i x y plot
1 0.50 0.50 (0,0)
2 1.17 1.50 (1,1)
3 1.83 2.50 (1,2)
4 2.50 3.50 (2,3)
CGR-22318 www.freestudyroom.xyz Page 4
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller shreyasshirsath. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $8.59. You're not tied to anything after your purchase.