Professional Documents
Culture Documents
2 CGR
2 CGR
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.
Miss.Vairagkar T.M.
VAPM,Latur
Properties of Good Line Drawing Algorithm:
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
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");
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
• 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
• Because of round off , errors are introduced and causes the calculated
pixel position to drift away from the true line path.
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 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.
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
10. Stop.
Advantage:
1. It involves only integer arithmetic, so it is simple.
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.
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.
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).
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
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.
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.
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
Miss.Vairagkar T.M.
VAPM,Latur
int gd = DETECT, gm;
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>
//driver code
int main()
{
int gd = DETECT, gm;
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>
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;
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.
• 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.
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.
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.
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) Bitmap Method
3) Starburst Method
1) STROKE METHOD :
• 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 :
• 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.
3) Starbust method:
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.
2. Requires code conversion software to display character from its 24-bit code.
Miss.Vairagkar T.M.
VAPM,Latur