0% found this document useful (0 votes)
83 views28 pages

Raster Scan Graphics

The document discusses algorithms for drawing graphics objects like lines and circles in raster graphics. It explains the DDA and Bresenham's line drawing algorithms, providing the steps, examples and comparisons. It also covers circle generating algorithms and explains Bresenham's algorithm for circle drawing.
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)
83 views28 pages

Raster Scan Graphics

The document discusses algorithms for drawing graphics objects like lines and circles in raster graphics. It explains the DDA and Bresenham's line drawing algorithms, providing the steps, examples and comparisons. It also covers circle generating algorithms and explains Bresenham's algorithm for circle drawing.
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/ 28

Unit II: Raster Scan Graphics (18 marks)

(Marks 18)

CO2: Implement standard algorithms to draw various


graphics objects using C programs.

Basic concepts in line drawing:

We say these computers have vector graphics capability (the line is a vector). The process is turning
on pixels for a line segment is called vector generation and algorithm for them is known as vector
generation 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.

Line Drawing Algorithms

1. DDA Algorithm (Digital Differential Analyzer)

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

Step3: Enter value of x1,y1,x2,y2.

Step4: Calculate dx = x2-x1

Step5: Calculate dy = y2-y1

Step6: If ABS (dx) > ABS (dy)


Then step = abs (dx)
Else

Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1

Step8: Set pixel (x, y)

Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))

Step10: Repeat step 9 until x = x2

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int x1,y1,x2,y2,dx,dy,length,i;

float x,y,xinc,yinc;

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("Enter the starting coordinates");

scanf("%d%d",&x1,&y1);

printf("Enter the ending coordinates");

scanf("%d%d",&x2,&y2);

dx=x2-x1;

dy=y2-y1;

if(abs(dx)>abs(dy))

length=abs(dx);

else

length=abs(dy);

xinc=dx/(float)length;

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

yinc=dy/(float)length;

x=x1

y=y1

putpixel(x,y,10);

for(i=0;i<length;i++)

putpixel(x,y,10);

x=x+xinc;

y=y+yinc;

delay(10);

getch();

closegraph();

Advantages of DDA Algorithm


1. It is the simplest algorithm and it does not require special skills for implementation.
2. It is a faster method for calculating pixel positions .
Disadvantages of DDA Algorithm
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. Hence end point accuracy is poor.

Example Consider the line from (0, 0) to (-6, -6). Use the simple DDA algorithm line.

Sol. Evaluating steps 1 to 5 in the DDA algorithm we have

Xl = 0 Y1 = 0
X2 = - 6 Y2 = - 6
Length = l X2-X1l |Y2-Y1l = 6

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

∆X = ∆Y = -1
Initial values for
X = 0 + 0.5 * Sign (-1) =-0.5
Y = 0 + 0.5 * Sign (-1) =-0.5
Tabulating the results of each iteration in the step 6 we get,

2. Bresenham's Algorithm.
It’s an accurate and efficient raster line generating Algorithm. Bresenham's line-drawing
algorithm uses an iterative scheme. A key feature of the algorithm is that it requires only integer
data and simple arithmetic. This makes the algorithm very efficient and fast.

Procedure for Bresenham’s algorithm:

1.Read line end points (x1,y1) and (x2,y2) such that they are not equal.

2. dx=|x2-x1| and dy=|y2-y1|

3.x=x1 and y=y1

4.e=2*dy-dx

5.i=1

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

6.plot (x,y)

7.while (e>0)

x=y+1

e=e-2*dx

x=x+1

e=e+2*dy

8.i=i+1

9. if(i<dx) then goto step 6.

10. Stop.

Program- Bresenham Line Drawing Algorithm -:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm=DETECT,length,dx,dy,x1,y1,x2,y2,e;
float x,y;
clrscr();
printf("\n enter coordinates of x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
e=2*(dy-dx);
x=x1;
y=y1;
initgraph(&gd,&gm,"c://Turboc3//BGI");
while(x<=x2)
{
if(e<0)

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

{
x=x+1;
y=y;
e=e+2*dy;
}
else
{
x=x+1;
y=y+1;
e=e+2*(dy-dx);
}
putpixel(x,y,15);
}
getch();
closegraph();
}

Problem: - Consider the line from (5, 5) to (13, 9). Use the Bresenham's algorithm to rasterize the
line.
Solution :-
Evaluating steps 1 through 4 in the Bresenham’s algorithm we have,
Given: -
step 1 :- x1 = 5 , y1 = 5 and x2 = 13 , y2 = 9 .
step 2 :- Δx = | 13 - 5| =8 Δy = | 9 - 5 | = 4
step 3 :- x = 5 , y = 5
step 4 :- e = 2 * Δy - Δx = 2 * 4 - 8 = 0 .
Tabulating the results of each iteration in the step 5 through 10.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

DDA Line Drawing Algorithm vs Bresenhams Line Drawing Algorithm

Arithmetic uses floating points Integer points

Operations Uses multiplication and division in its Uses only subtraction and addition
operations. in its operations.

Speed slowly than Bresenhams algorithm faster than DDA

Accuracy & not as accurate and efficient more efficient &much accurate
Efficiency

Drawing DDA algorithm can draw circles and curves Draw circles and curves with much
but that are not as accurate as Bresenhams more accuracy than DDA algorithm.
algorithm.

Round Off DDA algorithm round off the coordinates to Bresenhams algorithm does
not round off but takes the

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

integer that is nearest to the line. incremental value in its operation.

Expensive expensive. less expensive

Circle generating algorithm:

Symmetry of circle:

A circle is a geometric figure which is round, and can be divided into 360 degrees. A circle is a
symmetrical figure which follows 8-way symmetry.

8- Way symmetry: Any circle follows 8-way symmetry. This means that for every point (x,y) 8
points can be plotted. These (x,y), (y,x), (-y,x), (-x,y), (-x,-y), (-y,-x), (y,-x), (x,-y).

Drawing a circle: To draw a circle we need two things, the coordinates of the centre and the radius
of the circle.

Here r is the radius of the circle. If the circle has origin (0,0) as its centre then the above equation
can be reduced to x2 + y2 = r2

Bresenham’s Algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest
pixel position to complete the arc.

From the following illustration, you can see that we have put the pixel at (X, Y) location and now
need to decide where to put the next pixel − at N (X+1, Y) or at S (X+1, Y-1).

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

This can be decided by the decision parameter d.

 If d <= 0, then N(X+1, Y) is to be chosen as next pixel.


 If d > 0, then S(X+1, Y-1) is to be chosen as the next pixel.

Breshenham’s Algorithm For Circle Drawing

1. Read the radius of circle.


2. Calculate initial decision parameter di=3-2r
3. x=0 and y=r [initialize the starting point]
4. Do
{
5. Plot(x,y)//plot point(x,y) and other 7 points in all octant
if(di<0)then
di=di+4x=6
else
{
di=di+4(x-y)+10
y=y-1
}
x=x+1
}while(x<y);

6. stop

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Program : bresenhams circle drawing algorihm

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<dos.h>

#include<graphics.h>

void bressn(int,int,int);

void plot(int,int,int,int);

void plot(int xc,int yc,int x,int y)

putpixel(xc+x,yc+y,WHITE);

putpixel(xc-x,yc+y,RED);

putpixel(xc-x,yc-y,GREEN);

putpixel(xc+x,yc-y,YELLOW);

putpixel(xc+y,yc+x,WHITE);

putpixel(xc-y,yc+x,GREEN);

putpixel(xc-y,yc-x,YELLOW);

putpixel(xc+y,yc-x,RED);

void bressn(int r,int xc,int yc)

int d,x,y;

d=3-(2*r);

x=0;

y=r;

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

while(x<y)

plot(xc,yc,x,y);

x=x+1;

if(d<0)

d=d+(4*x)+6;

else

y=y-1;

d=d+4*(x-y)+10;

plot(xc,yc,x,y);

void main()

int gd,gm;

int xc,yc,radius;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("\n enter xc,yc,radius");

scanf("%d%d%d",&xc,&yc,&radius);

bressn(xc,yc,radius);

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

getch();

closegraph();

Example :

Calculate the pixel position along the circle path with radius r=10 centered on the origin using
Bresenham’s circle drawing algorithm from point (0,10) to point x=y.

Solution:

The value of d is

d=3-2r=3-2*10=-17

Tabulating results of step 4

x y d
0 10 -17
1 10 -11
2 10 -1
3 10 13
4 9 -5
5 9 17
6 8 11
7 7 13
Polygon:

A polygon is any 2-dimensional shape formed with straight lines. Triangles, quadrilaterals,
pentagons, and hexagons are all examples of polygons.

Types of polygon:

Every polygon is either convex or concave.

The difference between convex and concave polygons lies in the measures of their angles.

For a polygon to be convex, all of its interior angles must be less than 180 degrees. Otherwise, the
polygon is concave.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Inside-Outside Test

This method is also known as counting number method. While filling an object, we often need to
identify whether particular point is inside the object or outside it. There are two methods by which
we can identify whether particular point is inside an object or outside.

 Odd-Even Rule
 Nonzero winding number rule
Odd-Even Rule
In this technique, we will count the edge crossing along the line from any point (x,y) to infinity. If
the number of interactions is odd, then the point (x,y) is an interior poin t; and if the number of
interactions is even, then the point (x,y) is an exterior point. The following example depicts this
concept.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

From the above figure, we can see that from the point (x,y), the number of interactions point on the
left side is 5 and on the right side is 3. From both ends, the number of interaction points is odd, so
the point is considered within the object.

Nonzero Winding Number Rule


This method is also used with the simple polygons to test the given point is interior or not.

 Give the value 1 to all the edges which are going to upward direction and all other -1 as
direction values.

 Check the edge direction values from which the scan line is passing and sum up them.

 If the total sum of this direction value is non-zero, then this point to be tested is an interior
point, otherwise it is an exterior point.

 In the above figure, we sum up the direction values from which the scan line is passing then
the total is 1 – 1 + 1 = 1; which is non-zero. So the point is said to be an interior point.
Polygon filling:

Polygon filling algorithms are classified as: seed fill algorithm and scan line algorithm.

One way to fill polygon is to start from given “seed”, point known to be inside the polygon and
highlight outward from this point i.e. neighboring pixels until we encounter the boundary pixels.
This approach is called seed fill.

Seed fill algorithm further classified as flood fill and boundary fill algorithm.

Algorithms the fill interior defined regions are flood fill algorithms; those that fill boun dary defined
regions are called boundary fill algorithm.

Another approach to fill the polygon is to apply the inside test i.e. to check whether the pixels is
inside the polygon or outside the polygon and then highlight pixels which lie inside the polygon.
This approach is known as scan-line algorithm.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Flood Fill Algorithm


• Flood fill colors an entire area in an enclosed figure through interconnected pixels using a single
color. It is an easy way to fill color in the graphics. One just takes the shape and starts flood fill. The
algorithm works in a manner so as to give all the pixels inside the boundary the same color leaving
the boundary and the pixels outside.

• Flood fill algorithm is used for filling the interior of a polygon.

• Used when an area defined with multiple color boundaries.

• Start at a point inside a region− Replace a specified interior color (old color) with fill color

• Fill the 4−connected or 8−connected region until all interior points being replaced.

4−Connected Regions
From a given pixel, the region that you can get to by a series of 4 way moves (north, south, east,
west)

void fillcolor(int x,int y,int old_color,int new_color)


{
if(getpixel(x,y)==old_color)
{
delay(5);
putpixel(x,y,new_color);
fillcolor(x+1,y,old_color,new_color);
fillcolor(x-1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_color);
fillcolor(x,y-1,old_color,new_color);
}
}

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fillcolor(int,int,int,int);
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,50,100,100);
fillcolor(55,55,0,11);
getch();
closegraph();
}
void fillcolor(int x,int y,int old_color,int new_color)
{
if(getpixel(x,y)==old_color)
{
delay(5);
putpixel(x,y,new_color);
fillcolor(x+1,y,old_color,new_color);
fillcolor(x-1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_color);
fillcolor(x,y-1,old_color,new_color);
}
}

8−Connected Regions
From a given pixel, the region that you can get to by a series of 8-way moves (north, south, east,
west, NE, NW, SE, SW)

void fillcolor(int x,int y,int old_color,int new_color)


{
if(getpixel(x,y)==old_color)
{
delay(5);
putpixel(x,y,new_color);
fillcolor(x+1,y,old_color,new_color);
fillcolor(x-1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_color);
fillcolor(x,y-1,old_color,new_color);
fillcolor(x+1,y+1,old_color,new_color);

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

fillcolor(x+1,y-1,old_color,new_color);
fillcolor(x-1,y-1,old_color,new_color);
fillcolor(x-1,y+1,old_color,new_color);
}
}

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fillcolor(int,int,int,int);
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,50,100,100);
fillcolor(55,55,0,11);
getch();
closegraph();
}
void fillcolor(int x,int y,int old_color,int new_color)
{
if(getpixel(x,y)==old_color)
{
delay(5);
putpixel(x,y,new_color);
fillcolor(x+1,y,old_color,new_color);
fillcolor(x-1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_color);
fillcolor(x,y-1,old_color,new_color);
fillcolor(x+1,y+1,old_color,new_color);
fillcolor(x+1,y-1,old_color,new_color);
fillcolor(x-1,y-1,old_color,new_color);
fillcolor(x-1,y+1,old_color,new_color);
}
}

ADVANTAGE
• Flood fill algorithm is simplest algorithm.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

DISADVANTAGE
• Flood fill algorithm is slow.
• For large polygon flood fill algorithm is fail because it requires a large frame.

Boundary Fill Algorithm


Start at a point inside the figure and paint with a particular color. Filling continues until a boundary
color is encountered.

• In boundary fill algorithm Recursive method is used to fill the whole boundary.
• Start at a point inside a region.
• Paint the interior outward toward the boundary.
• The boundary is specified in a single color.

4-connected boundary fill Algorithm:-

boundary_fill(x, y, f_colour, b_colour)


{
if(getpixel(x, y)! = b_colour && getpixel(x, y)! = f_colour)
{
putpixel(x, y, f_colour);
boundary_fill(x + 1, y, f_colour, b_colour);
boundary_fill(x, y + 1, f_colour, b_colour);
boundary_fill(x -1, y, f_colour, b_colour);
boundary_fill(x, y – 1, f_colour, b_colour);
}
}
Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void boundary_fill(int,int,int,int);
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,50,100,100);
boundary_fill(55,55,6,15);
getch();

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

closegraph();
}
void boundary_fill(int x,int y,int fcolor,int bcolor)
{
if((getpixel(x,y)!=bcolor)&&(getpixel(x,y)!=fcolor))
{
delay(5);
putpixel(x,y,fcolor);
boundary_fill(x+1,y,fcolor,bcolor);
boundary_fill(x-1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bcolor);
boundary_fill(x,y-1,fcolor,bcolor);
}
}

8- connected boundary fill Algorithm:-

boundary_fill(x, y, f_colour, b_colour)


{
if(getpixel(x, y)! = b_colour && getpixel(x, y)! = f_colour)
{
putpixel(x, y, f_colour);
boundary_fill(x + 1, y, f_colour, b_colour);
boundary_fill(x - 1, y, f_colour, b_colour);
boundary_fill(x, y + 1, f_colour, b_colour);
boundary_fill(x, y - 1, f_colour, b_colour);
boundary_fill(x + 1, y + 1, f_colour, b_colour);
boundary_fill (x - 1, y - 1, f_colour, b_colour);
boundary_fill(x + 1, y - 1, f_colour, b_colour);
boundary_fill(x - 1, y + 1, f_colour, b_colour);
}
}
Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void boundary_fill(int,int,int,int);
void main()
{
int gd,gm;
detectgraph(&gd,&gm);

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,50,100,100);
boundary_fill(55,55,6,15);
getch();
closegraph();
}
void boundary_fill(int x,int y,int fcolor,int bcolor)
{
if((getpixel(x,y)!=bcolor)&&(getpixel(x,y)!=fcolor))
{
delay(5);
putpixel(x,y,fcolor);
boundary_fill(x+1,y,fcolor,bcolor);
boundary_fill(x-1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bcolor);
boundary_fill(x,y-1,fcolor,bcolor);
boundary_fill(x+1,y+1,fcolor,bcolor);
boundary_fill(x-1,y+1,fcolor,bcolor);
boundary_fill(x+1,y-1,fcolor,bcolor);
boundary_fill(x-1,y-1,fcolor,bcolor);
}
}

void fillcolor(int x,int y,int old_color,int boundary_fill(x, y, f_colour, b_colour)


new_color) {
{ if(getpixel(x, y)! = b_colour && getpixel(x, y)! =
if(getpixel(x,y)==old_color) f_colour)
{ {
putpixel(x,y,new_color); putpixel(x, y, f_colour);
fillcolor(x+1,y,old_color,new_color); boundary_fill(x + 1, y, f_colour, b_colour);
fillcolor(x-1,y,old_color,new_color); boundary_fill(x, y + 1, f_colour, b_colour);
fillcolor(x,y+1,old_color,new_color); boundary_fill(x -1, y, f_colour, b_colour);
fillcolor(x,y-1,old_color,new_color); boundary_fill(x, y – 1, f_colour, b_colour);
} }
} }

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

void fillcolor(int x,int y,int old_color,int boundary_fill(x, y, f_colour, b_colour)


new_color) {
{ if(getpixel(x, y)! = b_colour && getpixel(x, y)! =
if(getpixel(x,y)==old_color) f_colour)
{ {
putpixel(x,y,new_color); putpixel(x, y, f_colour);
fillcolor(x+1,y,old_color,new_color); boundary_fill(x + 1, y, f_colour, b_colour);
fillcolor(x-1,y,old_color,new_color); boundary_fill(x - 1, y, f_colour, b_colour);
fillcolor(x,y+1,old_color,new_color); boundary_fill(x, y + 1, f_colour, b_colour);
fillcolor(x,y-1,old_color,new_color); boundary_fill(x, y - 1, f_colour, b_colour);
fillcolor(x+1,y+1,old_color,new_color); boundary_fill(x + 1, y + 1, f_colour, b_colour);
fillcolor(x+1,y-1,old_color,new_color); boundary_fill (x - 1, y - 1, f_colour, b_colour);
fillcolor(x-1,y-1,old_color,new_color); boundary_fill(x + 1, y - 1, f_colour, b_colour);
fillcolor(x-1,y+1,old_color,new_color); boundary_fill(x - 1, y + 1, f_colour, b_colour);
} }
} }

Scan Line Fill Algorithm

• To successfully fill in a polygon three main components will be used: Edge Buckets, an Edge
Table and an Active List.

• These components will contain an edge’s information, hold all of the edges that compose the
figure and maintain the current edges being used to fill in the polygon, respectively.

• When a scan line intersects an edge endpoint, it intersects two edges. Consider, Two cases:

– Case A: edges are monotonically increasing or decreasing, we should consider this as only ONE
edge intersection

– Case B: edges reverse direction at endpoint; we should consider this as TWO edge intersections

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Stepwise Procedure

1. Read n, the number of vertices of polygon.

2. Read x and y coordinates of all vertices in array X[n] and y[n].

3. Find y max and y min

4. Store the initial x value (x1),y values (y1 and y2) and x increment dx from scan line to scan line
for each edge in the array edges[n] [4].

a. While doing this check that y1> y2, if not , interchange y1 and y2 corresponding x1 and x2 so that
for each edge, y1 represents its maximum y coordinate and y2 represents its minimum y coordinate.

5. Sort the rows of array, edges[n] [4] in descending order of y1, descending order of y2, and
ascending order of x2

6. Set y=y max

7. Find the active edges and update active edge list:

a. if ( y>y2 and y<y1)

i {edge is active}

b. Else

i {edge is not active}

8. Compute the x intersects for all active edges for current y value. Initially x intersect is x1 and x
intersects for successive y values can be given as

a. x(i+1)dxi+dx

b. where dx=-1/m and m=y2-y1/x2-x1 i.e. .slope of a line segment.

9. if x intersect is vertex i.e. x intersect =x1 and y=y1 then apply vertex test to check whether to
consider one intersect or two intersects. Store all x intersects in the x intersects array.

10. Store x intersects array in the ascending order.

11. Extract pairs of intersects from the sorted x intersect arr ay.

12. Pass pairs of x values to line drawing routine to draw corresponding line segments.

13.-set y=y-1

14. Repeat steps 7 through 13 until y> ymin

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

15. Stop

Program:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>

main()
{
int n,i,j,k,gd,gm,dy,dx;
int x,y,temp;
int a[20][2],xi[20];
float slope[20];
clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}

a[n][0]=a[0][0];
a[n][1]=a[0][1];

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");

/*- draw polygon -*/

for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}

getch();

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];

if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/
{
slope[i]=(float) dx/dy;
}
}

for(y=0;y< 480;y++)
{
k=0;
for(i=0;i<n;i++)
{

if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}

for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/


for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}

setcolor(35);

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}

Scan conversion

• The process of representing continuous graphics objects as a collection of discrete pixels is


called scan conversion. Scan conversion serves as a bridge between TV and computer
graphics technology.

• Scan conversion or scan converting rate is a video processing technique for changing the
vertical / horizontal scan frequency of video signal for different purposes and applications.

• The device which performs this conversion is called a scan converter.

• The application of scan conversion: video projectors, cinema equipment, TV and video
capture cards, standard and HDTV televisions, LCD monitors, radar displays and many
different aspects of picture processing.

• There are two distinct methods for changing a picture's data rate:

• Analog Methods
(Non retentive, memory-less or real time method)

• This conversion is done using large numbers of delay cells and is appropriate for analog
video.It may also be performed using a specialized scan converter vacuum tube.

• In this case polar coordinates (angle and distance) data from a source such as a radar
receiver, so that it can be displayed on a raster scan (TV type) display.

• Digital methods
(Retentive or buffered method)

• In this method, a picture is stored in a line or frame buffer with n1 speed (data rate) and is
read with n2 speed.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

Frame buffer:

• Each screen pixel corresponds to a particular entry in a 2D array residing in memory. This
memory is called a frame buffer or a bit map.

• The number of rows in the frame buffer equals to the number of raster lines on the display
screen. The number of columns in this array equals to the number of pixels on each raster
line.

• Frame buffer is a large part of computer memory used to store display image. Different kind
of memory can be used for frame buffers like drums, disk or IC – shift registers.

• To generate a pixel of desired intensity to read the disk or drum. The information stored in
disk or drum is in digital for, hence it is necessary to convert it into analog from using DAC
and then this analog signal is used to generate the pixel.

Character Generation

• Most of the times characters are builts into the graphics display devices, usallay as hardware
but sometimes through software.

• There are basic three methods:

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

– Stroke method

– Starbust method

– Bitmap method

1. Stroke method

• This method uses small line segments to generate a character.

• The small series of line segments are drawn like a strokes of a pen to form a character as
shown in figure.

• We can build our own stroke method.

• By calling a line drawing algorithm.

• Here it is necessary to decide which line segments are needed for each character and

• Then drawing these segments using line drawing algo.

• This method supports scaling of the character.

• It does this by changing the length of the line segments used for character drawing.

2. Starbust method

• In this method a fix pattern of line segments are used to generate characters.

• As shown in figure, there are 24 line segments.

• Out of 24 line segments, segments required to display for particular character, are
highlighted

• This method is called starbust method because of its characteristic appearance.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon


Unit II: Raster Scan Graphics (18 marks)
(Marks 18)

This method of character generation has some disadvantages. They are

1. The 24-bits are required to represent a character. Hence more memory is required

2. Requires code conversion software to display character from its 24-bit code

3. Character quality is poor. It is worst for curve shaped characters.

3. Bitmap Method

• Also known as dot matrix because in this method characters are represented by an array of
dots in the matrix form.

• It’s a two dimensional array having columns and rows : 5 X 7 as shown in figure.

• 7 X 9 and 9 X 13 arrays are also used.

• Higher resolution devices may use character array 100 X 100.

Course Coordinator: Mrs.Deshmukh A.P. M.M.Polytechnic , Thergaon

You might also like