Sunteți pe pagina 1din 29

SEMINAR REPORT

ON
POLYGON FILLING














lnuLx CCn1Ln1
1 lnLroducLlonolygon
2 1ypes of polygon
3 lllllng wlLh colours
4 lllllng approaches
3 Scan llne algorlLhm
6 8oundary flll meLhod
7 llood flll meLhod

















1ln18CuuC1lCn
A polygon is traditionally a plane Iigure that is bounded
by a closed path, composed oI a Iinite sequence oI straight
line segments (i.e., by a closed polygonal chain).


A closed Iigure represented by a collection oI line segments
connected end to end.
The line segments are known as 'Edge oI the Polygon and make up
the Polygon boundary.
Endpoints oI the edges are known as 'Vertex oI the Polygon.
Polygons are used in computer graphics to compose images that are
three-dimensional in appearance. Usually (but not always) triangular,
polygons arise when an object's surIace is modeled, vertices are
selected, and the object is rendered in a wire Irame model 1hls ls
qulcker Lo dlsplay Lhan a shaded model Lhus Lhe polygons are a
sLage ln compuLer anlmaLlon 1he polyqoo cooot refers Lo Lhe
number of polygons belng rendered per frame

Types oI polygon:
Polygon are oI two types:
A)Convex polygon: A convex polygon is a polygon in
which the line segment joining any two points within
the polygon lies completely inside the polygon.
All interior angles less than 180.

B)Concave polygon: A concave polygon is a polygon in
which the line segment joining any two points within
the polygon may not lie completely inside the polygon.
At least one interior angle is greater than 180.



llng w9 colours:
llng a Polygon is the process oI coloring every
pixel that comes inside the Polygon region.
e can paint a object only when the area will
closed on itselI.So,iI a body is not closed then we
can not paint it.
t will be similar incident iI we want to keep some
color to pot having leakageor hole present it.the
color will come out Irom the pot.
To paint a closed Iigure there are two approaches:
By determining the overlap intervals oI the scan
lines through given area.
By starting the painting Irom a given interior
position and paint outward Irom this point and to
be continued upto the speciIied boundary
conditions.






oundary fll me9od:
n this method any closed loop area can be painted
starting Irom a point ,inside a point.
Here ,iI the boundary oI the body exists in a single
color,then this algorithm proceeds outwards,pixel by
pixel,until the boundary color is not reached.
User can choose the same border color oI the body so
that a solid color region can be seen.
n this method,a pixel co-ordinate (x,y)is accepted Irom
the user which is placed inside the body.The boundary
color exists is also entered .Now ,it starts painting the
neighbouring co-ordinate oI(x,y) iI it is not exists in the
boundary color.
Neighbouring pixel can be traced out using two
method,one is Iour connected method and another is
eight-connected method.
n Iour-connected method ,the pixel to be painted in the
upper,lower,leIt,right direction pixels as shown below:




n the eight-connected method ,the Iour diagonal pixel
are also considered ,we can Iill with color oI a body
more accurately and correctly






Also known as "Seed-Fill Method"
A Seed-Point i.e. an arbitrary interior point is taken
as the initial or the starting point.
A considerable stack is used to store pixel
inIormation.
t is used in games.(go,minesweaper)
Basically, it is oI two types :
1.-Connected Seed Fill
2.8-Connected Seed Fill


Procedure:
Draw Polygon boundaries
Fill up the seed point
Test neighboring pixels to determine whether they
correspond to the boundary pixel
I not, paint them with the Iill-color and test their
neighboring pixels (store neighbors in stack)
Continue until all pixels have been tested
n algor9m of boundary fll for 4-connec9ed pel:
Procedure Ilood(x,y,Iill,boundary:integer)
begin
iI (getpixel(x,y)! boundary && getpixel(x,y)! Iill) then
begin
putpixel(x,y,Iill);
Ilood(x,y,Iill,boundary)
Ilood(x1,y,Iill,boundary);
Ilood(x-1,y,Iill,boundary);
Ilood(x,y1,Iill,boundary);
Ilood(x,y-1,Iill,boundary);
end
end
lgor9m:8 connec9ed boundary fll
Procedure Ilood(x,y,Iill,boundary:integer)
begin
iI (getpixel(x,y)! boundary && getpixel(x,y)! Iill) then
begin
putpixel(x,y,Iill);
Ilood(x1,y,Iill,boundary);
Ilood(x-1,y,Iill,boundary);
Ilood(x,y1,Iill,boundary);
Ilood(x,y-1,Iill,boundary);
Ilood(x1,y1,Iill,boundary);
Ilood(x-1,y-1,Iill,boundary);
Ilood(x1,y-1,Iill,boundary);
Ilood(x-1,y1,Iill,boundary);
end
end






lood -ll me9od
Sometimes we want to Iill in (or recolor) an area that
is not deIined within a single color boundary.we can
paint such area by replacing a speciIied interior color
insread oI searching Ior a boundary color value.This
approach is called a Ilood Iill algorithm.
e start Irom a speciIied interior point (x,y)and
reassign all pixel values that are currently set to a
given interior color with the desired Iill color.I the
area we want to paint has more than one interior
color,we can Iirst reassign pixel values so that all
interior points have the same color.

ModiIied Iorm oI oundary Fill Method.
Basic concept is just like Boundary Filling.
Like Boundary Fill Method, it is also used in
games.
FloodIill Iills boundary oI diIIerent color.




lgor9m:4 connec9ed flood fll
Procedure Ilood(x,y,Iill,oldcolor:integer)
begin
iI (getpixel(x,y)oldcolor) then
begin
setpixel(x,y,Iill);
Ilood(x,y,Iill,boundary)
Ilood(x1,y,Iill,boundary);
Ilood(x-1,y,Iill,boundary);
Ilood(x,y1,Iill,boundary);
Ilood(x,y-1,Iill,boundary);
end
end;









lgor9m:8 connec9ed flood fll
Procedure Ilood(x,y,Iill,oldcolor:integer)
begin
iI (getpixel(x,y)oldcolor) then
begin
setpixel(x,y,Iill);
Ilood(x,y,Iill,boundary)
Ilood(x1,y,Iill,boundary);
Ilood(x-1,y,Iill,boundary);
Ilood(x,y1,Iill,boundary);
Ilood(x,y-1,Iill,boundary);
Ilood(x1,y1,Iill,boundary);
Ilood(x-1,y-1,Iill,boundary);
Ilood(x1,y-1,Iill,boundary);
Ilood(x-1,y1,Iill,boundary);
end
end;





Scan ||ne a|gor|thm
The scanline Iill algorithm is an ingenious way oI
Iilling in irregular polygons. The algorithm begins with
a set oI points. Each point is conected to the next, and
the line between them is considered to be an edge oI the
polygon. The points oI each edge are adjusted to ensure
that the point wih the smaller y value appears Iirst.
Next, a data structure is created that contains a list oI
edges that begin on each scanline oI the image. The
program progresses Irom the Iirst scanline upward. For
each line, any pixels that contain an intersection
between this scanline and an edge oI the polygon are
Iilled in. Then, the algorithm progresses along the
scanline, turning on when it reaches a polygon pixel and
turning oII when it reaches another one, all the way
across the scanline.

There are two special cases that are solved by this
algorithm. First, a problem may arise iI the polygon
contains edges that are partially or completely out oI the
image. The algorithm solves this problem by moving
pixel values that are outside the image to the boundaries
oI the image. This method is preIerable to eliminating
the pixel completely, because its deletion could result in
a "backwards" coloring oI the scanline i.e. pixels that
should be on are oII and vice versa.

The second case has to do with the concavity oI the
polygon. I the polygon has a concave portion, the
algorithm will work correctly. The pixel on which the
two edges meet will be marked twice, so that it is turned
oII and then on. I, however, the polygon is convex at
the intersection oI two edges, the coloring will turn on
and then immediately oII, resulting in "backwards"
coloring Ior the rest oI the scanline. The problem is
solved by using the vertical location oI the next point in
the polygon to determine the concavity oI the current
portion. Overall, the algorithm is very robust. t turns
out that the only diIIiculty comes with polygons that
have large amounts oI edges, like circles and ellipses.
Filling in such a polygon would be very costly. Luckily,
there is a better way to Iill circles and ellipses. Below is
an image oI a polygon Iilled in with scanIill.
Scan||ne f||| method
used ln 8asLer Scan uevlces
1he scanllne algorlLhm works as follows
l llnd lnLersecLlons of Lhe scanllne wlLh all edges
ll SorL lnLersecLlons ln lncreaslng x
lll llll all Lhe plxels beLween palrs of lnLersecLlons
Speclal Cases Lo handle
lv Lxclude horlzonLal edges
v lor verLlces lylng on scanllne
CounL Lwlce
ShorLen Lhe edge by one scanllne


Program:
includestdio.h~
includeconio.h~
includegraphics.h~
struct edge

int x1;
int y1;
int x2;
int y2;
int Ilag;
};
void main()

int gd DETECT, gm, n, i, j, k;
struct edge ed|10|,temped;
Iloat dx,dy,m|10|,xint|10|,interx|10|;
int x|10|,y|10|,ymax 0, ymin 80, yy,temp;
initgraph (&gd, &gm, "c:\\tc\\bgi ");

printI("Enter the number vertices oI the graph: ");
scanI("d", &n);


printI("Enter the vertices: \n");
Ior(i 0; i n; i)

printI("x|d| : ", i);
scanI("d", &x|i|);
printI("y|d| : ", i);
scanI("d", &y|i|);
iI(y|i| ~ ymax)
ymax y|i|;
iI(y|i| ymin)
ymin y|i|;
ed|i|.x1 x|i|;
ed|i|.y1 y|i|;
}

/* Store the edge inIormation
-------------------------------*/
Ior(i0;in-1;i)

ed|i|.x2 ed|i1|.x1;
ed|i|.y2 ed|i1|.y1;
ed|i|.Ilag0;
}
ed|i|.x2 ed|0|.x1;
ed|i|.y2 ed|0|.y1;
ed|i|.Ilag0;

/* Check Ior y1~y2, iI not interchange y1 and y2
with corresponding x1 and x2
--------------------------------*/
Ior(i0;in;i)

iI(ed|i|.y1 ed|i|.y2)

temp ed|i|.x1;
ed|i|.x1ed|i|.x2;
ed|i|.x2temp;
temp ed|i|.y1;
ed|i|.y1ed|i|.y2;
ed|i|.y2temp;
}
}

/* Draw the polygon
---------------------- */
Ior(i0;in;i)

line(ed|i|.x1,ed|i|.y1,ed|i|.x2,ed|i|.y2);
}

/* sorting oI edges in the order oI y1,y2,x1
--------------------------------------------- */
Ior(i0;in-1;i)

Ior(j0;jn-1;j)

iI(ed|j|.y1ed|j1|.y1)

temped ed|j|;
ed|j|ed|j1|;
ed|j1| temped;
}
iI(ed|j|.y1ed|j1|.y1)

iI(ed|j|.y2ed|j1|.y2)

temped ed|j|;
ed|j|ed|j1|;
ed|j1| temped;
}
iI(ed|j|.y2ed|j1|.y2)

iI(ed|j|.x1ed|j1|.x1)

temped ed|j|;
ed|j|ed|j1|;
ed|j1| temped;
}
}
}
}
}
/* calculating 1/slope oI each edge and storing top x
coordinate oI the edge --------------------------- */
Ior(i0;in;i)

dx ed|i|.x2 - ed|i|.x1;
dy ed|i|.y2 - ed|i|.y1;
iI(dy0)

m|i|0;
}
else

m|i| dx/dy;
}
interx|i| ed|i|.x1;
}
yyymax;
while(yy~ymin)

/* Marking active egdes
-------------------------- */
Ior(i0;in;i)

iI(yy ~ ed|i|.y2 && yy ed|i|.y1 && ed|i|.y1!ed|i|.y2)

ed|i|.Ilag 1;
}
else

ed|i|.Ilag 0;
}
}
/* Finding the x intersections
-------------------------------- */
j0;
Ior(i0;in;i)

iI(ed|i|.Ilag1)

iI(yyed|i|.y1)

xint|j|ed|i|.x1;
j;
iI(ed|i-1|.y1yy && ed|i-1|.y1yy)

xint|j|ed|i|.x1;
j;
}
iI(ed|i1|.y1yy && ed|i1|.y1yy)

xint|j|ed|i|.x1;
j;
}
}
else

xint|j| interx|i|(-m|i|);
interx|i|xint|j|;
j;
}
}
}
/* Sorting the x intersections
--------------------------------*/
Ior(i0;ij;i)

Ior(k0;kj-1;k)

iI(xint|k|~xint|k1|)

temp xint|k|;
xint|k| xint|k1|;
xint|k1| temp;
}
}
}

/* Extracting pairs oI x values to draw lines
---------------------------------------------- */
Ior(i0;ij;i2)

line(xint|i|,yy,xint|i1|,yy);
}
yy--;
delay(50);
}
getch();
}













Program:boundary fll me9od usng 4 connec9ed pel
includestdlib.h~
includestdio.h~
includegraphics.h~
void Ilood(int x,int y,int Iill,int boundary);
void main()

int gdDETECT,gm;
int xx,yy,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printI("\t ***********Boundary Iill algorithm
connected***********");
Ior(i0;i1000;i)

xxrand()639;
yyrand()80;
putpixel(xx,yy,6);
}
line(0,0,0,80); //leIt
line(639,0,639,80); //right
line(639,9,0,9); //lower
line(0,80,639,80); //upper

rectangle(200,200,260,260);
Ilood(230,230,,15);
rectangle(180,180,200,200);
Ilood(190,190,,15);
rectangle(260,260,280,280);
Ilood(20,20,,15);
getch();
closegraph();
}
void Ilood(int x,int y,int Iill,int boundary)

iI (getpixel(x,y)! boundary && getpixel(x,y)! Iill)

delay(3);
putpixel(x,y,Iill);
Ilood(x1,y,Iill,boundary);
Ilood(x-1,y,Iill,boundary);
Ilood(x,y1,Iill,boundary);
Ilood(x,y-1,Iill,boundary);
}
}

Program :flood fll algor9m 4-connec9ed pel
includestdlib.h~
includestdio.h~
includegraphics.h~
void Ilood(int x,int y,int Iill,int boundary);
void main()

int gdDETECT,gm;
int xx,yy,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printI("\t ***********Boundary Iill algorithm
connected***********");
Ior(i0;i1000;i)

xxrand()639;
yyrand()80;
putpixel(xx,yy,6);
}
line(0,0,0,80); //leIt
line(639,0,639,80); //right
line(639,9,0,9); //lower
line(0,80,639,80); //upper

rectangle(200,200,260,260);
Ilood(230,230,,15);
rectangle(180,180,200,200);
Ilood(190,190,,15);
rectangle(260,260,280,280);
Ilood(20,20,,15);
getch();
closegraph();
}
void Ilood(int x,int y,int Iill,int boundary)

iI (getpixel(x,y)! boundary && getpixel(x,y)! Iill)

delay(3);
putpixel(x,y,Iill);
Ilood(x1,y,Iill,boundary);
Ilood(x-1,y,Iill,boundary);
Ilood(x,y1,Iill,boundary);
Ilood(x,y-1,Iill,boundary);
}
}

S-ar putea să vă placă și