An Algorithm To Find The Largest Circle Inside A Polygon
An Algorithm To Find The Largest Circle Inside A Polygon
1Postgraduate
Institute of Science, University of Peradeniya
2Department of Computer Science, Faculty of Science, University of Ruhuna
3Department of Statistics and Computer Science, Faculty of Science, University of Peradeniya
ABSTRACT
INTRODUCTION
Finding the largest circle inside any the algorithm is provided in the appendix
polygon is a well known problem. The section in order to increase the
simplest polygon is the triangle. The largest understandability.
circle inside a triangle is unique and called
the in-circle of the triangle. The center of the Representation of the Polygon
in-circle is the intersection point of internal
bisectors of any of its angles. The radius of The polygon is represented using two
the in-circle is the perpendicular distance arrays x and y. The points variable stores the
from the center to any of the edges of the number of vertices in the polygon. Then (x[i],
triangle. If all the internal angles of a polygon y[i]) where i = 0, 1…, (points – 1) represent
are equal then that polygon is called a the vertices. The inbuilt drawing functions
regular polygon. The largest circle inside a such as circle, line take their parameters in
regular polygon is also unique and it can be integer form. If floating point values were
found in the same way as the triangle. But sent as arguments to such functions the
when it comes to an irregular polygon, the decimal parts are truncated leading to
above method is not applicable. And also it is incorrect approximation. To maintain the
not necessary for in-circle to be unique in smoothness of the graphics output, a better
this situation (S. L. Green, 1954). Therefore, approximation is required. This is done using
finding the largest circle inside any arbitrary dpx and dpy functions. Here 0.5 is added to
polygon is not a straightforward task. each argument before truncating the decimal
A randomized approach is used in the parts of them. In this way, better
proposed work. Random points are approximation is achieved. The polygon is
generated inside the polygon. They are drawn using drawPolygon function. It uses
candidates for the center of the largest circle. the cyclic nature of vertices in the polygon by
Then for each candidate, the largest possible modulo (%) operator.
circle is estimated. After that the largest
circle is selected out of those candidate Generating Random Points inside the
largest circles. Bounding Rectangle
The variable p is maintained. For each side of the new point, this situation is
new point, p is set into zero at the beginning. ignored.
For each edge which satisfies the condition
xp > xc, the value of p should be incremented Case 4: This situation can be described as
by one. At this moment p stores the number the horizontal line goes through an edge as
of intersections of the horizontal line in the shown in Figure 7.
right side of the new point. The value of p is
expected to decide whether the new point is
inside or outside the polygon. There are four
special cases which should be handled
carefully when incrementing the value of p in
addition to intersections. These four cases
Figure 7: Line y = yc Goes through the Edge
are not detected under intersections.
In this kind of a situation, if at least one
Case 1: This situation can be described as a of the end points of the touching edge is on
vertex touching the horizontal line as shown the right side of the new point, then value of
in Figure 4. p should be incremented by one.
Suppose the horizontal line begins from
the new point and extends in the right
direction. In above four cases, case 1 and
case 3 are not considered, because those
situations do not cause the horizontal line to
Figure 4: Vertex is Touching the Line y = yc go inside or outside the polygon. But case 2
and case 4 should be considered as
Even if the touching point is on the right intersections since those situations cause
side of the new point, this situation is horizontal line to go inside or outside the
ignored. polygon.
The first two cases deal with a vertex.
Case 2: This situation can be described as Touching vertices which are on the right side
the horizontal line goes through a vertex as of the new point can be identified by
shown in Figure 5. checking the condition (y = yc AND x > xc)
where (x, y) is the coordinates of a vertex.
Distinguishing case 1 and case 2 should be
done as follows. Suppose i is the index of the
vertex that is touching the horizontal line.
Then indices of its two neighboring vertices
a and b can be computed as a = i – 1, b = i + 1.
But there are two exceptional cases. If i = 0
Figure 5: Line y = yc Goes through the Vertex then i - 1 = -1. Therefore, it should be set to
(points - 1) since it is the actual index of the
In this kind of a situation, if the vertex is neighboring vertex. If i = points - 1 then i + 1
on the right side of the new point then the = points. Therefore, it should also be set to 0
value of p should be incremented by one. since it is the actual index of the neighboring
vertex. This is due to the cyclic ordering of
Case 3: This situation can be described as an the vertices. If pos (a) * pos (b) < 0 then it
edge touching the horizontal line as shown in means the vertices a and b are in two sides of
Figure 6. the horizontal line. That means it is the case
2. If this condition is satisfied only the value
of p is incremented by one.
The second two cases deal with an edge.
Let i and j be the indices of the end points of
Figure 6: Edge Touching Line y = yc the touching edge where j = (i + 1) % points.
The edges which satisfies the condition (y =
In this kind of a situation, even if the end yc AND x > xc) where (x, y) is the coordinates
points of the touching edge are on the right of the jth point are considered. Then the
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54
indices of neighboring vertices a and b of the should fall inside the edge. Therefore, the
edge can be computed a = i – 1, b = j + 1. In distance to the (1, 2) edge is ignored since
this situation, two exceptional cases arise the intersection point is outside the edge.
due to the cyclic ordering of the vertices.
8 6
That should also be resolved as in first two
cases. By considering pos (a) * pos (b) value
as above two cases, case 3 and case 4 can be
distinguished. Here only in case 4, value of p 9
is incremented by one.
Finally, by checking the value of p, it can 7
5
be decided that whether the new point is 4
inside or outside the polygon. If p is even
that means the new point is outside the 3
polygon. And if p is odd that means the new C
point is inside the polygon. This fact is
obvious and does not need a proof. Also this 2
is done using module (%) operator.
1
Calculating the Closest Vertex Distance 0
(CVD) to the New Point Figure 8: Distances to Edges of the Polygon
The method of generating points inside After calculating the distances, the
the polygon has been described in the above minimum distance is stored in the dem
sections. Once this new point is identified, variable of the main method. This calculation
the distance to the closest vertex should be is done inside the d_e_m method. The
calculated and stored in the dpm variable of indeterminate cases may arise during the
the main method. This is done inside the calculation process. Therefore, they should
d_p_m function. The Euclidian distance be carefully handled separately. Suppose the
between two points (x1, y1) and (x2, y2) is end points of the edge are (x1, y1) and (x2, y2)
calculated as follows. The inbuilt sqrt and the new point is C (x0, y0) as shown in
function is used to calculate the square root. Figure 9.
(x1, y1)
D = √ ((x1 – x2)2 + (y1 – y2)2);
APPENDIX
} {
if(found==0)
if((p%2)==0) {
{ dem=ab(xc-x[i]);
return 0; found=1;
} }
else
return 1; {
} de=ab(xc-x[i]);
if(dem>de)
double ab(double x) {
{ dem=de;
if(x>0) }
{ }
return x; }
} }
else
return -x; {
} if(y[i]==y[j])
{
double d_p_m() if((x[i]-xc)*(x[j]-xc)<0)
{ {
int i; if(found==0)
double dp,dpm; {
dem=ab(yc-y[i]);
dpm=sqrt((xc-x[0])*(xc-x[0])+(yc- found=1;
y[0])*(yc-y[0])); }
else
for(i=1;i<points;i++) {
{ de=ab(yc-y[i]);
dp=sqrt((xc-x[i])*(xc-x[i])+(yc- if(dem>de)
y[i])*(yc-y[i])); {
dem=de;
if(dp<dpm) }
{ }
dpm=dp; }
} }
} else
{
return dpm; m=(y[i]-y[j])/(x[i]-x[j]);
} c=y[i]-m*x[i];
{
de=ab(m*xc- for(i=0;i<n;i++)
yc+c)/sqrt(m*m+1); {
if(dem>de) xc=random(maxx);
{ if(xc<minx)
dem=de; {
} xc+=minx;
} }
} yc=random(maxy);
} if(yc<miny)
} {
} yc+=miny;
}
return dem;
} if(isInside())
{
void main() dpm=d_p_m();
{ dem=d_e_m();
int i;
long n=1000; if(dpm<dem)
double minx,maxx,miny,maxy; {
double dpm,dem,r,maxr; r=dpm;
double xcm,ycm; }
int f=0; else
{
ginit(); r=dem;
randomize(); }
setcolor(15); if(f==0)
drawPolygon(); {
maxr=r;
minx=x[0]; xcm=xc;
maxx=x[0]; ycm=yc;
miny=y[0]; f=1;
maxy=y[0]; }
else
for(i=0;i<points;i++) {
{ if(maxr<r)
if(minx>x[i]) {
{ maxr=r;
minx=x[i]; xcm=xc;
} ycm=yc;
if(maxx<x[i]) }
{ }
maxx=x[i];
} }
if(miny>y[i]) }
{
miny=y[i]; circle(dpx(xcm),dpy(ycm),dpx(maxr));
}
if(maxy<y[i]) getch();
{ gexit();
maxy=y[i]; }
}
}