You are on page 1of 10

Ceylon Journal of Science (Physical Sciences) 19 (2015) 45 – 54 Computer Science

AN ALGORITHM TO FIND THE LARGEST CIRCLE INSIDE A


POLYGON
K. R. Wijeweera1, 2 and S. R. Kodituwakku1, 3

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

(*Corresponding author’s email:krw19870829@gmail.com)


(Received: 31 April 2014/ Accepted after revision: 14 July 2015)

ABSTRACT

Poles of Inaccessibility (PIA) is a popular problem in geographical information


systems. The PIA can be defined as the most difficult point to reach within a landmass.
That means it is the point furthest from any coastline. If the landmass is represented
using a polygon then the PIA of landmass is identical to the center of the largest circle
inside it. Converting this pure mathematical problem into a computerized algorithm is
not a straightforward task. The algorithm should be able to address the issues like
indeterminate forms, special cases and precision error. The proposed algorithm has
the capability of handling those issues and works with any type of arbitrary polygon.
This paper proposes a randomized algorithm to approximate the maximum circle that
can be included inside a given polygon. Random points are generated inside the
polygon as candidate centers for the largest circle. The largest circle inside the polygon
corresponding to each candidate center is computed. The largest circle at any point is
the largest circle among those largest circles. The algorithm has been implemented
using C programming language. The experimental results proved that the algorithm
performs with considerable amount of accuracy and efficiency.

© University of Peradeniya 2015

INTRODUCTION

A precise mathematical definition for a dering is the single point shared


polygon can be given as follows. Let v0, v1, between them: ei ∩ ei+1 = vi+1, for all i
v2…, vn-1 be n points in the plane. Here the = 0…, n - 1.
index arithmetic is mod n, implying a cyclic
2. Nonadjacent segments do not
ordering of the points, with v0 following vn-1,
since (n - 1) + 1 ≡ n ≡ 0 (mod n). Let e0 = v0v1, intersect: ei ∩ ej = Ø, for all j ≠ i + 1.
e1 = v1v2…, ei = vivi+1…, en-1 = vn-1v0 be n 3. None of three consecutive points vi
segments connecting the points. Then these are collinear.
segments bound a polygon if and only if
The points vi are called the vertices of the
1. The intersection of each pair of polygon, and the segments ei are called its
segments adjacent in the cyclic or edges. Note that a polygon of n vertices has n
edges (J. O’Rourke, 1998).
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

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

METHODOLOGY The inbuilt random function is used to


generate random points. This function
In order to find the largest circle within generates a random integer between 0 and (t
an arbitrary polygon is carried out as – 1), if t is passed as its argument. The
follows. random points are generated inside a
bounding rectangle to the polygon as shown
1. Generate a random point P. in Figure 1. This is done inside main function.
2. If the point P is outside the polygon To find the bounding rectangle following
then go to step 1. values should be computed.
3. Find the largest possible circle that
minx = x-coordinate of the leftmost vertex of
can be drawn inside the polygon by the polygon;
taking point P as the center. Let r be
the radius of that circle. maxx = x-coordinate of the rightmost vertex
4. Iterate the procedure from step 1 to of the polygon;
step 3 for preferred number of times
miny = y-coordinate of the bottommost
and store P and corresponding r
vertex of the polygon;
values taken from step 3.
5. Find the largest value of r from step 4 maxy = y-coordinate of the topmost vertex of
and corresponding point P. That is the polygon;
the center and radius of the largest
circle inside the polygon.

Following subsections explain how to


perform above steps. The implementation of
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

the vertex is downside of the y = yc line.


Suppose the point is P then the vertices 6
and 8 should return +1 while others return -
1. And if the point is Q then the vertices 0, 1
and 2 should return -1 while others return
+1. This idea can be used to check whether
edges intersect the horizontal line which
goes through the given point. If i and j are the
indices of the vertices of an edge then pos (i)
* pos (j) < 0 means that the edge intersects
the horizontal line.
8 6

Figure 1: Bounding Rectangle of the Polygon 9 P

For an example, the random (maxx) 7


5
generates a random integer value between 0 4
and (maxx – 1). If that value is greater than
minx then generated value is between minx 3
and maxx. Else if that generated value is less
then minx then again by adding minx to that
Q
generated value takes the value between 2
minx and maxx. Same thing can be done for
the y-value as well. In this way random 1
points can be generated inside the rectangle. 0
Figure 2: Random Points inside the Polygon
Extracting the Points inside the Polygon
If an edge intersects with the y = yc
As mentioned in section: Generating line then the intersection point should be
Random Points inside the Bounding calculated as follows (Figure 3).
Rectangle, random points can be generated
inside the bounding rectangle. As shown in The equation of the edge can be written as,
Figure 2, those points can be either inside or (y – y[i]) / (x – x[i]) = (y[j] – y[i]) / (x[j] –
outside the polygon (points P and Q x[i]);
respectively). Extraction of points inside the x = x[i] + (y – y[i]) * (x[j] – x[i]) / (y[j] –
polygon is done using isInside function. If y[i]);
horizontal infinite line to the right side is
drawn beginning from a given point then Let (xp, yp) be the intersection point.
number of its intersections with the polygon Then by substituting y = yc in the above
can be counted. For the point P, it is 3 and for equation,
the point Q, it is 2. Having an intersection xp = x[i] + (yc – y[i]) * (x[j] – x[i]) / (y[j] –
means that horizontal line either goes inside y[i]);
or outside the polygon. Therefore, it is
obvious that if the number of intersections is (x[i], y[i])
odd then the point is inside the polygon and
if the number of intersections is even then
the point is outside the polygon.
Let (xc, yc) be the point that should be (xc, yc)
checked whether inside or outside. The pos
function takes index of a particular vertex as (x[j], y[j])
the argument and returns 0, if that vertex is
on the y = yc line. Also it returns +1, if the Figure 3: Intersection with the Line y = yc
vertex is upside of the y = yc line and -1, if
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

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

Calculating the Closest Edge Distance


(CED) to the New Point

Suppose P (x0, y0) is a point and L: a * x +


b * y + c = 0 is a line. The shortest distance
from the point to the line can be computed (x2, y2)
using following formula (S. L. Green, 1954). (x0, y0)
Absolute value is calculated using the ab
function and square root value is calculated Figure 9: Intersection of a Normal and an Edge
using the sqrt function. All the possible cases can be represented
using a tree as shown in Figure 10. Edge can
D = |a * x0 + b * y0 + c| / √ (a2 + b2); be parallel to y-axis and then its gradient is
undefined and normal is parallel to x-axis.
There is n number of edges in the When edge is not parallel to y-axis, situation
polygon. Distances are calculated only with can further be divided into two cases as
the edges which satisfy a certain condition. normal parallel to y-axis and normal is not
Suppose the new point is C as shown in parallel to y-axis.
Figure 8. A normal can be drawn to each
edge. In the figure, normal have been drawn
to edges (9, 0) and (1, 2). The condition is the
intersection point of the edge and the normal
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

written as, (m1 * x1 – y1 +c1) * (m1 * x2 – y2 +c1)


< 0. The distance can be calculated using the
modified formula, |m * x0 – y0 +c|√ (m2 + 1).
x1 = x2 x1 ≠ x2
The Largest Circle by Taking the New
Point as the Center
y1 = y2 y1 ≠ y2
The radius of the largest circle that can
Figure 10: Classification of Cases be drawn by taking new point is the
minimum of CVD and CED values.
Case 1; (x1 = x2): Considering only one of those values can
lead to false results as shown in Figure 11.
In this situation, the edge is parallel to y-
axis and the normal is parallel to x-axis. If
two end points of the edge are in two sides of
the y = y0 line, then that means the
intersection point is inside the edge. If so, the Figure 11: Counter Examples
distance is calculated. This condition can be
mathematically expressed as (y1 – y0) * (y2 – If only CED is considered then it will lead
y0) < 0. The distance can be calculated by |x0 the error like the large circle shown in the
– x1|. figure. And if only CVD is considered then it
will lead the error like the small circle shown
Case 2; ((x1 ≠ x2) AND (y1 = y2)): is the figure. Therefore, taking the minimum
of those two values is necessary.
In this situation, the edge is parallel to x-
axis and normal is parallel to y-axis. The Iterative Process
condition is (x1 – x0) * (x2 – x0) < 0 to check
for the eligibility to calculate the distance. Once a random point is generated inside
The distance can be calculated by |y0 – y1|. the polygon, the largest possible circle that
can be drawn by taking that point as the
Case 3; ((x1 ≠ x2) AND (y1 ≠ y2)): center is found. This process can be iterated
n times. At each time corresponding largest
In this situation, none of the edge or circle is computed. From these n largest
normal is parallel to y-axis. That means their circles, again the largest circle is selected.
gradient is determinate. Let gradient and y- That is the approximation of the largest
intercept of the edge are m and c; and that of circle inside the polygon.
the normal are m1 and c1. Then following
equations can be written. RESULTS AND DISCUSSION

m = (y1 – y2) / (x1 – x2); The algorithm was implemented using C


c = y1 – m * x1; programming language. Following hardware
m1 = - (1/m); and software were used.
c1 = y0 – m1 * x0;
Computer: Intel(R) Pentium(R) Dual CPU;
The standard format of the equation of a E2180 @ 2.00 GHz; 2.00 GHz, 0.98 GB of
line is, a * x + b * y + c = 0. The equations of RAM;
the edge and the normal can be written in IDE: Turbo C++; Version 3.0; Copyright(c)
standard format as, m * x – y +c = 0 and m1 * x 1990, 1992 by Borland International, Inc;
– y +c1 = 0 respectively. If (a * x1 + b * y1 + c) *
(a * x2 + b * y2 + c) < 0 then (x1, y1) and (x2, y2) Several polygons were used with
points are in two sides of the line, a * x + b * y different number of vertices as given in
+ c = 0; this is a well-known result in Table 1 (K. R. Wijeweera et al, 2015).
coordinate geometry (S. L. Green, 1954).
According to this result, the condition for
eligibility to calculate the distance can be
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

Table 1: The List of Polygons Table 2: The Set of Results


Polygon Coordinates of Vertices Polygon xc yc r
1 (10, 10), (110, 210), (90, 80), (250, 1 69 56 32
60) 2 69 178 50
2 (10, 10), (110, 210), (90, 80), (250, 3 103 148 55
60) 4 120 162 81
3 (140, 90), (200, 240), (40, 190), (70, 5 90 61 38
10), (280, 80), (210, 130) 6 106 168 65
4 (60, 60), (300, 130), (240, 220), 7 129 50 40
(100, 250), (20, 200), (40, 20), (260, 8 207 79 42
70) 9 70 57 37
5 (100, 240), (130, 80), (270, 210), 10 137 171 31
(190, 20), (150, 60), (90, 10), (10,
80), (80, 100) CONCLUSION
6 (150, 40), (50, 10), (10, 190), (120,
250), (230, 220), (130, 90), (240, An algorithm to find the largest circle
160), (240, 30), (50, 120) which can be included inside a given polygon
7 (140, 90), (240, 60), (140, 240), was proposed. The proposed algorithm is
(250, 170), (290, 10), (30, 10), (10, based on generating random points inside
120), (130, 220), (80, 70), (170, 140) the polygon as candidate centers. The
8 (230, 40), (30, 10), (80, 70), (10, 90), variable n was set to 10000 (See section:
(70, 90), (150, 140), (150, 60), (190, Iterative Process). The accurate results can
140), be obtained with smaller values of n as well.
(80, 250), (210, 200), (280, 10) The value of n depends on the area of the
9 (90, 90), (150, 160), (130, 220), polygon since it decides the distribution of
(240, 70), (250, 10), (160, 110), candidate centers. An optimization
(110, 30), (40, 10), technique like genetic algorithms could be
(20, 130), (90, 250), (130, 160), (90, used to improve the efficiency of the
190)
proposed algorithm.
10 (90, 70), (60, 150), (10, 20), (60,
190), (90, 150), (140, 240), (190,
130), (230, 170), REFERENCES
(270, 60), (190, 10), (230, 100),
(140, 30), (160, 150) J. O’Rourke (1998) Computational
Geometry in C: 2nd Edition, Cambridge
The value of n variable was set to 10000 University Press, pp. 1.
and the algorithm was run for each of the S. L. Green (1954), Advanced Level Pure
polygons reported in Table 1. By observing Mathematics, University Tutorial Press
the graphical output, it was confirmed that Ltd, Clifton House, Euston Road,
the final outputs gave accurate results. Those
London, N. W. I.
results are reported in Table 2. There (xc, yc)
is the coordinates of the center of the largest K. R. Wijeweera, S. R. Kodituwakku
circle and r is its radius. (2015), Efficient Triangulation of a
Polygon by Ear Removal; submitted to
Ceylon Journal of Science (Physical
Sciences).

APPENDIX

This section includes the implementation


of the proposed algorithm in C programming
language.
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

#include<stdio.h> double xp;


#include<conio.h> int a,b;
#include<math.h> for(i=0;i<points;i++)
#include"D:/header/grap.h" {
j=(i+1)%points;
double x[]={60,60,90,110,90,110,70,10}; if(pos(i)*pos(j)<0)
double y[]={40,10,10,40,60,90,100,40}; {
int points=8; xp=x[i]+(yc-y[i])*(x[j]-
x[i])/(y[j]-y[i]);
double xc,yc; if(xp>xc)
{
int dpx(double x) p++;
{ }
int p; }
p=(int)(x+0.5);
return p; if((y[i]==yc) && (x[i]>xc))
} {
a=i-1;
int dpy(double y) b=i+1;
{
int p; if(a==-1)
p=(int)(y+0.5); {
p=getmaxy()-p; a=points-1;
return p; }
} if(b==points)
{
void drawPolygon() b=0;
{ }
int i,j;
for(i=0;i<points;i++) if(pos(a)*pos(b)<0)
{ {
j=(i+1)%points; p++;
}
line(dpx(x[i]),dpy(y[i]),dpx(x[j]),dpy(y[j
])); if((y[j]==yc) && (x[j]>xc))
} {
} a=i-1;
b=j+1;
int pos(int k)
{ if(a==-1)
if(y[k]>yc) {
{ a=points-1;
return 1; }
} if(b==points)
else if(y[k]<yc) {
{ b=0;
return -1; }
}
return 0; if(pos(a)*pos(b)<0)
} {
p++;
int isInside() }
{
int i,j; }
int p=0; }
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

} {
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];

double d_e_m() m1=-(1/m);


{ c1=yc-m1*xc;
int i,j;
double de,dem; if((m1*x[i]-
double m,c,m1,c1; y[i]+c1)*(m1*x[j]-y[j]+c1)<0)
int found=0; {
if(found==0)
for(i=0;i<points;i++) {
{ dem=ab(m*xc-
j=(i+1)%points; yc+c)/sqrt(m*m+1);
if(x[i]==x[j]) found=1;
{ }
if((y[i]-yc)*(y[j]-yc)<0) else
Wijeweera and Kodituwakku/Ceylon Journal of Science- Physical Sciences 19 (2015) 45-54

{
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]; }
}
}

You might also like