Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

2.

Raster Scan Graphics


2.1 Basic concept in line drawing :
A line connects two points. It is a basic element in graphics. To draw a line, you
need two points between which you can draw a line. In the following three
algorithms, we refer the one point of line as X0,Y0 and the second point of line
as X1,Y1.

A straight line may be defined by two endpoints & an equation. In fig the two
endpoints are described by (x1,y1) and (x2,y2). The equation of the line is used
to determine the x, y coordinates of all the points that lie between these two
endpoints.

Using the equation of a straight line, y = mx + b where m = Scan Converting a


Straight Line & b = the y interrupt, we can find values of y by incrementing x
from x =x1, to x = x2. By scan-converting these calculated x, y values, we
represent the line as a sequence of pixels.

Miss.Vairagkar T.M.
VAPM,Latur
Properties of Good Line Drawing Algorithm:

1. Line should appear Straight:

We must appropriate the line by choosing addressable points close


to it. If we choose well, the line will appear straight, if not, we shall
produce crossed lines.

2. Lines should terminate accurately:

Unless lines are plotted accurately, they may terminate at the


wrong place.

3. Lines should have constant density:

Line density is proportional to the no. of dots displayed divided by


the length of the line.

To maintain constant density, dots should be equally spaced.

4. Line density should be independent of line length and angle:

This can be done by computing an approximating line-length


estimate and to use a line-generation algorithm that keeps line density
constant to within the accuracy of this estimate.

5. Line should be drawn rapidly:

This computation should be performed by special-purpose


hardware.

Line Drawing Algorithms:-


Digital Differential Analyzer (DDA) Algorithm:-

• Digital Differential Analyzer algorithm generates a line from differential


equations of line and hence the name DDA.

Miss.Vairagkar T.M.
VAPM,Latur
• This algorithm generates a line from differential equations of line and
hence the name DDA.
• DDA algorithm is an incremental scan conversion method.
• A DDA is hardware or software used for linear interpolation of variables
over an interval between start and end point.
• DDAs are used for rasterization of lines, triangles and polygons.
• DDA method is referred by this name because this method is very similar
to the numerical differential equations. The DDA is a mechanical device
that solves differential equations by numerical methods.
• Algorithm :-

Step 1: Read the input of the 2 end points of the line as (x1, y1) & (x2, y2) such
that x1 != x2 and y1 != y2
Step 2: Calculate dx = x2 – x1 and dy = y2 – y1
Step 3:

if(dx>=dy)

step=dx

else

step=dy

Step 4: xin = dx / step & yin = dy / step


Step 5: x = x1 + 0.5 & y = y1 + 0.5
Step 6: while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}

Miss.Vairagkar T.M.
VAPM,Latur
C Program of DDA Line Drawing Algo:
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>

void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

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

printf("Enter the value of x1 and y1 : ");


scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
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+dy;

Miss.Vairagkar T.M.
VAPM,Latur
i=i+1;
delay(100);
}

closegraph();
}

Outptut

Merits/Advantages of DDA algorithms:

• It is the simplest algorithm and it does not require special skills for
implementation.

• It is a faster method for calculating pixel positions than the direct use of
equation y = mx + b. It eliminates the multiplication in the equation by
making use of raster characteristics, so that appropriate increments are
applied in the x or v direction to find the pixel positions along the line
path

• Floating point Addition is still needed.

Demerits/Disadvantages of DDA algorithms:

• Because of round off , errors are introduced and causes the calculated
pixel position to drift away from the true line path.

• Because of floating point operations the algorithm is time consuming.

Miss.Vairagkar T.M.
VAPM,Latur
Bresenham's Line Algorithm:-
This algorithm is used for scan converting a line. It was developed by
Bresenham. It is an efficient method because it involves only integer addition,
subtractions, and multiplication operations. These operations can be
performed very rapidly so lines can be generated quickly.

In this method, next pixel selected is that one who has the least distance from
true line.

The method works as follows:

Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our may to


the night, one pixel position at a time in the horizontal direction toward
P2'(x2',y2').

Once a pixel in choose at any step

The next pixel is

1. Either the one to its right (lower-bound for the line)


2. One top its right and up (upper-bound for the line)

The line is best approximated by those pixels that fall the least distance from
the path between P1',P2'.

Miss.Vairagkar T.M.
VAPM,Latur
To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1

• Algorithm :-

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

[ if equal then plot that point and exit ]


2. Δx = |x2 - x1| and Δy = |y2 - y1|
3. [Initialize starting point]
x = x1 and y = y1

4. e = 2 * Δy - Δx
[Initialize value of decision variable or error to compensate for nonzero intercepts]

Miss.Vairagkar T.M.
VAPM,Latur
5. i=1[ Initialize counter ]

6. Plot(x , y)

7. whi1e( e ≥ 0)
{
y=y+1
e = e - 2 * Δx
}
x=x+1
e = e + 2 * Δy

8. i = i + 1

9. if(i ≤ Δx) then go to step6.

10. Stop.

Advantage:
1. It involves only integer arithmetic, so it is simple.

2. It avoids the generation of duplicate points.

3. It can be implemented using hardware because it does not use


multiplication and division.

4. It is faster as compared to DDA (Digital Differential Analyzer) because it does


not involve floating point calculations like DDA Algorithm.

Miss.Vairagkar T.M.
VAPM,Latur
Disadvantage:
1. This algorithm is meant for basic line drawing only Initializing is not a part of
Bresenham's line algorithm. So to draw smooth lines, you should want to look
into a different algorithm.

Bresenham’s circle drawing algorithm:-


We cannot display a continuous arc on the raster display. Instead, we have to
choose the nearest pixel position to complete the arc.
It uses the key feature of circle that it is highly symmetric. So, for whole 360
degree of circle we will divide it in 8-parts each octant of 45 degree.
In order to that we will use Bresenham’s Circle Algorithm for calculation of
the locations of the pixels in the first octant of 45 degrees.
It assumes that the circle is centered on the origin. So for every pixel (a,b) it
calculates, we draw a pixel in each of the 8 octants of the circle as shown
below :

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).

Miss.Vairagkar T.M.
VAPM,Latur
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.

Algorithm
Step 1 − Get the coordinates of the center of the circle and radius, and store them
in x, y, and R respectively. Set P=0 and Q=R.
Step 2 − Set decision parameter D = 3 – 2R.
Step 3 − Repeat through step-8 while P ≤ Q.
Step 4 − Call Draw Circle (X, Y, P, Q).
Step 5 − Increment the value of P.
Step 6 − If D < 0 then D = D + 4P + 6.
Step 7 − Else Set R = R - 1, D = D + 4(P-Q) + 10.
Step 8 − Call Draw Circle (X, Y, P, Q).
Draw Circle Method(X, Y, P, Q).

Call Putpixel (X + P, Y + Q).


Call Putpixel (X - P, Y + Q).
Call Putpixel (X + P, Y - Q).
Call Putpixel (X - P, Y - Q).
Call Putpixel (X + Q, Y + P).
Call Putpixel (X - Q, Y + P).
Call Putpixel (X + Q, Y - P).

Miss.Vairagkar T.M.
VAPM,Latur
Call Putpixel (X - Q, Y - P).

2.3 Polygons:
Polygon is a representation of the surface. It is primitive which is closed in
nature. It is formed using a collection of lines. It is also called as many-sided
figure. The lines combined to form polygon are called sides or edges. The lines
are obtained by combining two vertices.

Types of Polygons:-

1. Convex
2. Concave

Convex:- A polygon is called convex if the line segment joining any


two point within the polygon lies completely inside the polygon. A non-
convex polygon is said to be concave.

Angle between any consecutive pair of edges is always less than 180°.

Concave:- A concave polygon has one interior angle greater than 180°.
The line segment joining any two point within the polygon may not lie
completely inside the polygon.

Polygon Filling:-

SeedFill Algorithms:-
Boundary-fill algorithm:-

Miss.Vairagkar T.M.
VAPM,Latur
This is an area filling algorithm. This is used where we have to do an interactive
painting in computer graphics, where interior points are easily selected. If we
have a specified boundary in a single color, then the fill algorithm proceeds
pixel by pixel until the boundary color is encountered. This method is called
the boundary-fill algorithm.

Boundary Fill Algorithm is recursive in nature. It takes an interior point(x, y), a


fill color, and a boundary color as the input. The algorithm starts by checking
the color of (x, y). If it’s color is not equal to the fill color and the boundary
color, then it is painted with the fill color and the function is called for all the
neighbours of (x, y). If a point is found to be of fill color or of boundary color,
the function does not call its neighbours and returns. This process continues
until all points up to the boundary color for the region have been tested.
Problem with boundary fill

1. It may not fill regions correctly, if same interior pixels are also displayed
in fill color.
2. In 4-connected there is a problem. Sometimes it does not fill the corner
pixel as it checks only the adjacent position of the given pixel.

The boundary fill algorithm can be implemented by 4-connected pixels or 8-


connected pixels.
4-connected pixels : After painting a pixel, the function is called for four
neighboring points. These are the pixel positions that are right, left, above and
below the current pixel. Areas filled by this method are called 4-connected.
Below given is the algorithm :

Algorithm for boundary fill (4-connected)


Boundary fill (x, y, fill, boundary)

1) Initialize boundary of the region, and variable fill with color.


2) Let the interior pixel(x,y)
(Now take an integer called current and assign it to (x,y))
current=getpixel(x,y)
3) If current is not equal to boundary and current is not equal
to fill then set pixel (x, y, fill)
boundary fill 4(x+1,y,fill,boundary)

Miss.Vairagkar T.M.
VAPM,Latur
boundary fill 4(x-1,y,fill,boundary)
boundary fill 4(x,y+1,fill,boundary)
boundary fill 4(x,y-1,fill,boundary)

4) End.
Procedure:-
// Function for 4 connected Pixels

void boundaryFill4(int x, int y, int fill_color,int boundary_color)


{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
Program:-
#include <graphics.h>

void boundaryFill4(int x, int y, int fill_color,int boundary_color)


{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
int main()
{

Miss.Vairagkar T.M.
VAPM,Latur
int gd = DETECT, gm;

initgraph(&gd, &gm, "");

int x = 250, y = 200, radius = 50;

circle(x, y, radius);

boundaryFill4(x, y, 6, 15);

delay(10000);

getch();

closegraph();

return 0;
}

8-connected pixels :
More complex figures are filled using this approach. The pixels to be tested are
the 8 neighbouring pixels, the pixel on the right, left, above, below and the 4
diagonal pixels. Areas filled by this method are called 8-connected.
Algorithm :
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);

Miss.Vairagkar T.M.
VAPM,Latur
}
}

Program:-
#include <graphics.h>

// Function for 8 connected Pixels


void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}

//driver code
int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "");

rectangle(50, 50, 100, 100);

boundaryFill8(55, 55, 4, 15);

delay(10000);

getch();

closegraph();

return 0;
}

Miss.Vairagkar T.M.
VAPM,Latur
Flood fill Algorithm:-

Sometimes we come across an object where we want to fill the area and its
boundary with different colors. We can paint such objects with a specified interior
color instead of searching for particular boundary color as in boundary filling
algorithm.
Instead of relying on the boundary of the object, it relies on the fill color. In other
words, it replaces the interior color of the object with the fill color. When no more
pixels of the original interior color exist, the algorithm is completed.
Once again, this algorithm relies on the Four-connect or Eight-connect method of
filling in the pixels. But instead of looking for the boundary color, it is looking for all
adjacent pixels that are a part of the interior.

Algorithm:-
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4
and 5 till the boundary pixels reached.
If getpixel(x, y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighborhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
Step 6 – Exit
Program for flood fill:-
#include<stdio.h>
#include<graphics.h>
#include<dos.h>

void floodFill(int x,int y,int oldcolor,int newcolor)


{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);

Miss.Vairagkar T.M.
VAPM,Latur
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel

int main()
{
int gm,gd=DETECT,radius;
int x,y;

printf("Enter x and y positions for circle\n");


scanf("%d%d",&x,&y);
printf("Enter radius of circle\n");
scanf("%d",&radius);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(x,y,radius);
floodFill(x,y,0,15);
delay(5000);
closegraph();

return 0;
}

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 point; and if the number of interactions is even, then the point
(x,y) is an exterior point. The following example depicts this concept.

Miss.Vairagkar T.M.
VAPM,Latur
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:-


In another alternative method, give directions to all the edges of the polygon.
Draw a scan line from the point to be test towards the left most of X direction.

• 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.

Miss.Vairagkar T.M.
VAPM,Latur
• 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.

Scan Line Algorithm:-


This algorithm works by intersecting scanline with polygon edges and fills the
polygon between pairs of intersections. The following steps depict how this
algorithm works.
Step 1 − Find out the Ymin and Ymax from the given polygon.

Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax.
Name each intersection point of the polygon. As per the figure shown above, they
are named as p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0,
p1), (p1, p2), and (p2, p3).
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the
alternate pairs.

2.4 Scan Conversion:-


It is a process of representing graphics objects a collection of pixels. The
graphics objects are continuous. The pixels used are discrete. Each pixel can
have either on or off state.

The circuitry of the video display device of the computer is capable of


converting binary values (0, 1) into a pixel on and pixel off information. 0 is

Miss.Vairagkar T.M.
VAPM,Latur
represented by pixel off. 1 is represented using pixel on. Using this ability
graphics computer represent picture having discrete dots.

Any model of graphics can be reproduced with a dense matrix of dots or


points. Most human beings think graphics objects as points, lines, circles,
ellipses. For generating graphical object, many algorithms have been
developed.

Advantage of developing algorithms for scan


conversion
1. Algorithms can generate graphics objects at a faster rate.
2. Using algorithms memory can be used efficiently.
3. Algorithms can develop a higher level of graphical objects.

Examples of objects which can be scan converted


1. Point
2. Line
3. Sector
4. Arc
5. Ellipse
6. Rectangle
7. Polygon
8. Characters
9. Filled Regions

The process of converting is also called as rasterization. The algorithms


implementation varies from one computer system to another computer
system. Some algorithms are implemented using the software. Some are
performed using hardware or firmware. Some are performed using various
combinations of hardware, firmware, and software.

Frame Buffer:-
Picture definition is stored in memory area called the Refresh Buffer or Frame
Buffer. This memory area holds the set of intensity values for all the screen

Miss.Vairagkar T.M.
VAPM,Latur
points. Stored intensity values are then retrieved from the refresh buffer and
“painted” on the screen one row (scan line) at a time as shown in the
following illustration.

2.5 Character Generator Methods:


1) Stroke Method

2) Bitmap Method

3) Starburst Method

1) STROKE METHOD :

• Stroke method is based on natural method of text written by human


being. In this method graph is drawing in the form of line by line.

• Line drawing algorithm DDA follows this method for line drawing. This
method uses small line segments to generate a character.

• The small series of line segments are drawn like a stroke of pen to form
a character.

• We can build our own stroke method character generator by calls to the
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
algorithm.

Miss.Vairagkar T.M.
VAPM,Latur
2) BITMAP METHOD :

• Bitmap method is a called dot-matrix method as the name suggests this


method use array of bits for generating a character. These dots are the
points for array whose size is fixed.

• In bit matrix method when the dots is stored in the form of array the
value 1 in array represent the characters i.e. where the dots appear we
represent that position with numerical value 1 and the value where dots
are not present is represented by 0 in array.

• It is also called dot matrix because in this method characters are


represented by an array of dots in the matrix form. It is a two
dimensional array having columns and rows.

• A 5x7 array is commonly used to represent characters. However 7x9 and


9x13 arrays are also used. Higher resolution devices such as inkjet
printer or laser printer may use character arrays that are over 100x100.

3) Starbust method:

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


characters. Out of these 24 line segments, segments required to display
for particular character are highlighted.
• This method of character generation is called starbust method because
of its characteristic appearance.

Miss.Vairagkar T.M.
VAPM,Latur
• The starbust patterns for characters A and M. the patterns for particular
characters are stored in the form of 24 bit code, each bit representing
one line segment.

• The bit is set to one to highlight the line segment; otherwise it is set to
zero. For example, 24-bit code for Character A is 0011 0000 0011 1100
1110 0001 and for character M is 0000 0011 0000 1100 1111 0011.

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.

Miss.Vairagkar T.M.
VAPM,Latur

You might also like