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.
Inside-Outside Test
This method is also known as counting number method. While filling an object,
we often needto 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.
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
boundarydefined 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.
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 colorboundaries.
•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_c
olor); fillcolor(x-
1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_c
olor); fillcolor(x,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_c
olor); fillcolor(x-
1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_c
olor); 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_colo
r); fillcolor(x-
1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_colo
r); fillcolor(x,y-
1,old_color,new_color);
fillcolor(x+1,y+1,old_color,new_c
olor);
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_colo
r); fillcolor(x-
1,y,old_color,new_color);
fillcolor(x,y+1,old_color,new_colo
r); fillcolor(x,y-
1,old_color,new_color);
fillcolor(x+1,y+1,old_color,new_c
olor); 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.
DISADVANTAGE
•Flood fill algorithm isslow.
•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 singlecolor.
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();
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,bc
olor); boundary_fill(x-
1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bc
olor); boundary_fill(x,y-
1,fcolor,bcolor);
}
}
8- connected boundary fillAlgorithm:-
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);
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,bcolo
r); boundary_fill(x-
1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bcolo
r); boundary_fill(x,y-
1,fcolor,bcolor);
boundary_fill(x+1,y+1,fcolor,bc
olor); 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)!
if(getpixel(x,y)==old_color) =b_colour&&getpixel(x,y)! = f_colour)
{ {
putpixel(x,y,new_color); putpixel(x, y, f_colour);
fillcolor(x+1,y,old_color,new_co boundary_fill(x + 1, y, f_colour,
lor); fillcolor(x- b_colour); boundary_fill(x, y + 1,
1,y,old_color,new_color); f_colour, b_colour); boundary_fill(x -1,
fillcolor(x,y+1,old_color,new_co y, f_colour, b_colour); boundary_fill(x,
lor); fillcolor(x,y- y – 1, f_colour, b_colour);
1,old_color,new_color); }
} }
}
void fillcolor(int x,int y,int old_color,int boundary_fill(x, y, f_colour, b_colour)
new_color) {
{ if(getpixel(x, y)!
if(getpixel(x,y)==old_color) =b_colour&&getpixel(x,y)! = f_colour)
{ {
putpixel(x,y,new_color); putpixel(x, y, f_colour);
fillcolor(x+1,y,old_color,new_colo boundary_fill(x + 1, y, f_colour,
r); fillcolor(x- b_colour); boundary_fill(x - 1, y,
1,y,old_color,new_color); f_colour, b_colour); boundary_fill(x, y
fillcolor(x,y+1,old_color,new_colo + 1, f_colour, b_colour);
r); fillcolor(x,y- boundary_fill(x, y - 1, f_colour,
1,old_color,new_color); b_colour); boundary_fill(x + 1, y + 1,
fillcolor(x+1,y+1,old_color,new_c f_colour, b_colour); boundary_fill(x -
olor); fillcolor(x+1,y- 1,y- 1, f_colour,b_colour);
1,old_color,new_color); fillcolor(x- boundary_fill(x+1,y -1,
1,y-1,old_color,new_color); f_colour,b_colour); boundary_fill(x-
fillcolor(x- 1,y+1,f_colour,b_colour);
1,y+1,old_color,new_color); }
} }
}
Scan Line Fill Algorithm
• To successfullyfill in a polygon three maincomponentswill beused: 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
Stepwise Procedure
[Link] n, the number of vertices of polygon.
[Link] x and y coordinates of all vertices in array X[n] and y[n].
[Link] y max and ymin
[Link] 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].
[Link] 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.
[Link] the rows of array, edges[n] [4] in descending order of y1, descending
order of y2, and ascending order of x2
[Link] y=y max
[Link] the active edges and update active edge list:
[Link] ( y>y2
andy<y1) i
{edge
isactive}
[Link]
i {edge is not active}
[Link] 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.
[Link] 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 array.
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
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();
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);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}