0% found this document useful (0 votes)
283 views4 pages

Polygon Detection From A Set of Lines

This document proposes an algorithm to detect polygons defined by a set of line segments. The algorithm has four main steps: 1) Detect intersections between line segments using the Bentley-Ottmann algorithm. 2) Remove proper intersections to create a graph where vertices are endpoints and intersections, and edges are non-intersecting line segments. 3) Find the minimum cycle basis of the graph to identify minimal polygons. 4) Construct polygons by transforming each minimum cycle into a polygon. The overall algorithm runs in polynomial time and space of O((N+M)4) and O((N+M)2) respectively, where N is the number of line segments and M is the number of intersections.

Uploaded by

robert179
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
283 views4 pages

Polygon Detection From A Set of Lines

This document proposes an algorithm to detect polygons defined by a set of line segments. The algorithm has four main steps: 1) Detect intersections between line segments using the Bentley-Ottmann algorithm. 2) Remove proper intersections to create a graph where vertices are endpoints and intersections, and edges are non-intersecting line segments. 3) Find the minimum cycle basis of the graph to identify minimal polygons. 4) Construct polygons by transforming each minimum cycle into a polygon. The overall algorithm runs in polynomial time and space of O((N+M)4) and O((N+M)2) respectively, where N is the number of line segments and M is the number of intersections.

Uploaded by

robert179
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Polygon Detection from a Set of Lines

Alfredo Ferreira Manuel J. Fonseca Joaquim A. Jorge


Department of Information Systems and Computer Science
INESC-ID/IST/Technical University of Lisbon
R. Alves Redol, 9, 1000-029 Lisboa, Portugal
[email protected], [email protected], [email protected]

Abstract
Detecting polygons defined by a set of line segments in a plane is an important step in the analysis of vectorial
drawings. This paper presents an approach that combines several algorithms to detect basic polygons from a
set of arbitrary line segments. The resulting algorithm runs in polynomial time and space, with complexities of
O((N + M )4 ) and O((N + M )2 ) respectively, where N is the number of line segments and M is the number of
intersections between line segments. Our choice of algorithms was made to strike a good compromise between
efficiency and ease of implementation. The result is a simple and efficient solution to detect polygons from lines.

Keywords
Polygon Detection, Segment Intersection, Minimum Cycle Basis

1. INTRODUCTION line segments in which any pair of segments share at most


one endpoint.
Unlike image processing, where data consist of raster im-
ages, the proposed algorithm deals with drawings in vector
format, consisting of line segments. This requires com- 2.1. Finding line segment intersections
pletely different approaches, such as described in this pa-
per. The first step of our approach consists in detecting all M
To perform polygon detection from a set of line segments intersections between N line segments in a plane. This is
we divide this task in four major steps. First we detect considered one of the fundamental problems of Computa-
line segment intersections using the Bentley-Ottmann al- tional Geometry and it is known that any algorithm, within
gorithm [13]. Next step creates a graph induced by the the model of algebraic decision tree, have a lower bound
drawing, where vertices represent endpoints or proper in- of Ω(N log N + M ) time to solve it [3, 5].
tesection points of line segments and edges represent max- In [1] Balaban proposes two algorithms for finding inter-
imal relatively open subsegments that contain no vertices. secting segments, a deterministic and asymptotically op-
The third step finds the Minimum Cycle Basis (MCB) [16] timal for both time O(N log N + M ) and space O(N )
of the graph induced in previous step, using the algorithm algorithm and a simpler one that can perform the same
proposed by Horton [12]. Last step constructs a set of poly- task in O(N log2 N + M )-time. Before that, Chazelle
gons based on cycles in the previously found MCB. This and Edelsbrunner [5] reached a time optimal algorithm
is straight-forward if we transform each cycle into a poly- O(N log N + M ) with space requirement of O(N + M ).
gon, where each vertex in the cycle represents a vertex in The randomized approach devised by Clarkson and Shor
the polygon and each edge in the cycle represents an edge [6] produced a algorithm for reporting all intersecting pairs
in the polygon. that requires O(N log N + M ) time and O(N ) space.
In sections 2 and 3 we describe the four steps of our In 1979 Bentley and Ottmann proposed an algorithm that
method. Section 4 presents the whole algorithm, followed solved this problem in O((N +M ) log N ) time and O(N +
by experimental results in section 5. Finally in section 6 M ) space [13]. This algorithm is the well-known Bentley-
we discuss conclusions and future work. Ottmann algorithm and after more than 20 years it is still
widely adopted in practical implementations because it is
2. INTERSECTION REMOVAL
easy to understand and implement [15, 11]. In realiz-
In a vector drawing composed by a set of line segments ing that this is not the most complex part of our aproach,
there might exist many intersections between these seg- we decide to use the Bentley-Ottmann algorithm, since its
ments. To detect polygonal shapes we have to remove complexity is pretty acceptable for our purposes and its
proper segment intersections, thus creating a new set of published implementations are quite simple.
r 2
.
. 3
. 1
.
4
.
.
. 5

Figure 3. A planar graph with a exponential


number of cycles

Figure 1. Set Φ of line segments 3.1. All Cycles of a Graph


2.2. Removing line segment intersections The first known linear-time algorithm for listing all cycles
of a graph was presented by Syslo [16]. This algorithm
The next step of our approach is to remove all proper requires O(V ) space and O(V × C) time, where V is the
intersections between line segments, dividing each inter- number of vertices and C the number of cycles in G. Later
sected segment in sub-segments without proper intersec- Dogrusöz and Krishnamoorthy proposed a vector space al-
tions, only sharing endpoints. In order to find and re- gorithm for enumerating all cycles of a planar graph that
move intersections, performing at once the first two steps runs in O(V 2 × C) time and O(V ) space [8]. Although
of our approach, we use a robust and efficient imple- asymptoticaly slower, this algorithm is much simpler than
mentation of the Bentley-Ottmann algorithm, described by Syslo’s and is amenable to parallelization. Unfortunately,
Bartuschka, Mehlhorn and Naher [2] that computes the the total number of cycles in a planar graph can grow ex-
planar graph induced by a set of line segments. Their ponentially with the number of vertices [14]. An example
implementation, represented in this paper by C OMPUTE - of this situation is the graph presented in Figure 3. In this
I NDUCED -G RAPH, computes the graph G induced by set case, the number of cycles, including the interior region
Φ in O((N +M ) log N ) time. Since this algorithm is quite numbered 1, is O(2r ) with r = k/2 + 1, where k is the
long we choose not to present it here. We refer our readers number of vertices, since one can choose any combination
to [2] for a detailed description. of the remaining regions to define a cycle [8]. This is why
In this implementation the vertices of G represent all end- it is not very feasible to detect all polygons that can be
points and proper intersection points of line segments in constructed from a set of lines. In this paper, we choose
Φ, and the edges of G are the maximal relatively open sub- just to detect the minimal polygons, those that have a min-
segments of lines in Φ that do not contain any vertex of imal number of edges and cannot be constructed by joining
G. The major drawback of this implementation lies in that other minimal polygons.
parallel edges are produced in the graph for overlapping 3.2. Minimum Cycle Basis of a Graph
segments. We assume that Φ contains no such segments.
Considering, for example, the set Φ shown in Figure 1, Considering that we just want to detect the minimal poly-
C OMPUTE -I NDUCED -G RAPH will produce the graph G, gons this can be treated as searching for a Minimum Cycle
depicted in Figure 2, where each edge represents a non- Basis (MCB). So, the second step of our approach con-
intersecting line segment. sists in obtaining a MCB of graph G. A cycle basis is
defined as a basis for the cycle space of G which consists
3. POLYGON DETECTION entirely of elementary cycles. A cycle is called elemen-
tary if it contains no vertex more than once. The dimen-
Detecting polygons is similar to finding cycles on the graph sion of the cycle space is given by the cyclomatic num-
G produced in the previous step. ber ν = E − V + P [9, 4], where E is the number of edges
and V the number of vertices in G and P is the number of
connected components of G.
Horton presented the first known polynomial-time algo-
rithm to find the shortest cycle basis of a graph, which runs
in O(E 3 V ) time [12] or in O(E 4 ) on simple planar graphs
[10], which is the case. While assimptotically better so-
lutions have been published in the literature, the Bentley-
Ottmann algorithm is both simple and usable for our needs.
The pseudo-code of this algorithm is listed in M INIMUM -
C YCLE -BASIS and shortly described bellow. A further de-
tailed description of this algorithm and concepts behind it
can be found in [12].
Figure 2. Graph G induced by Φ The A LL -PAIRS -S HORTEST-PATHS finds the shortest
M INIMUM -C YCLE -BASIS(G)
1 Γ ← empty set P4
2 Π ← A LL -PAIRS -S HORTEST-PATHS(G)
3 for each v in V ERTICES(G)
4 do for each (x, y) in E DGES(G) P3
5 do if Πx,v ∩ Πv,y = {v} P1
P2
6 then C ← Πx,v ∪ Πv,y ∪ (x, y)
7 add C to Γ
8 O RDER -B Y-L ENGTH(Γ)
9 return S ELECT-C YCLES(Γ)
Figure 5. Set Θ of polygons detected from Φ

paths between all pairs of vertices in graph G and can be P OLYGONS -F ROM -C YCLES(Γ)
performed in O(V 3 ) time and O(V 2 ) space using Floyd- 1 Θ ← empty set
Warshall or Dijkstra algorithms [7]. O RDER -B Y-L ENGTH 2 for each C in Γ
orders the cycles by ascending length and can be imple- 3 do P ← new polygon
mented by any efficient sorting algorithm. This is a non- 4 for each v in V ERTICES(V )
critical step because it has a O(V ν log V ) upper bound 5 do add vertex v to P
in time complexity, which is insignificant in comparision 6 add polygon P to Θ
with other steps of this algorithm. 7 return Θ
In S ELECT-C YCLES we use a greedy algorithm to find the
MCB from Γ set of cycles. To do this Horton [12] suggests Figure 5 illustrates the resulting set Θ of polygons gen-
representing the cycles as rows of a 0-1 incidence matrix, erated by applying P OLYGONS -F ROM -C YCLES to Γ de-
in which columns correspond to the edges of the graph picted in Figure 4.
and rows are the incidence vectors of each cycle. Gaus-
4. ALGORITHM OUTLINE
sian elimination using elementary row operations over the
integers modulo two can then be applied to the incidence We can now outline D ETECT-P OLYGONS. This algorithm
matrix, processing each row in turn, in ascending order of is able to detect a set Θ of polygons from a initial set Ψ
the weights of cycles, until enough independent cycles are of line segments. To perform this task we pipeline the
found. algorithms referred in previous sections for line segment
intersection removal, MCB finding and cycle-to-polygon
This step dominates the time complexity from other steps,
conversion.
since it takes O(Eν 2 V ) time. Knowing that G is always
a simple planar graph we can conclude that as a whole the D ETECT-P OLYGONS(Ψ)
M INIMUM -C YCLE -BASIS algorithm has a worst case up- 1 G ← C OMPUTE -I NDUCED -G RAPH(Ψ)
per bound of O(Eν 2 V ) = O(E 3 V ) = O(E 4 ) operations 2 Γ ← M INIMUM -C YCLE -BASIS(G)
and a space requirements of O(V 2 ). 3 Θ ← P OLYGONS -F ROM -C YCLES(Γ)
4 return Θ
Figure 4 shows an example of Γ, the set of cycles resulting
from applying the M INIMUM -C YCLE -BASIS to graph G
shown in Figure 2. As refered in section 2.2, C OMPUTE -I NDUCED -G RAPH
runs in O((N + M ) log N ) time and O(N + M ) space.
3.3. Polygon construction The S HORTEST-C YCLE -BASIS runs in O(V 4 ) operations
The last step of our approach consists in constructing a and has a space requirement of O(V 2 ), making this the
set Θ of polygons from the MCB. An algorithm to per- critical step in the complexity of this algorithm, since the
form this operation can easily run in O(CV ) time, where P OLYGONS -F ROM -C YCLES just needs O(CV ) time.
C is number of cycles in MCB. Such an algorithm is listed Since the number V of vertices in the graph is no greater
in P OLYGONS -F ROM -C YCLES which returns a set Θ of than the sum of line endpoints (2 × N ) with detected in-
polygons. tersections M , we can then conclude that the proposed
algorithm has time and space complexities of O(V 4 ) =
O((N + M )4 ) and O(V 2 ) = O((N + M )2 ), respectively.
5. EXPERIMENTAL RESULTS
The algorithm proposed in this paper was implemented in
C++ and tested in a Intel Pentium III 1GHz 512MB RAM
computer running Windows XP . We tested the algorithm
with sets of line segments created from simple test draw-
ings, technical drawings of mechanical parts and hand-
sketched drawings. Table 1 presents the results obtained
Figure 4. Shortest cycle basis Γ of graph G from these tests.
Lines Intersections Nodes Edges Time (ms) [3] M. Ben-Or. Lower bounds for algebraic computation
6 9 21 24 10 trees. In Proceedings of the 15th Annual ACM Sym-
36 16 58 68 50 posium Theory of Computing, pages 80–86. ACM,
167 9 169 177 3986 1983.
286 47 389 376 8623
[4] Augustin-Louis Cauchy. Recherche sur les
518 85 697 679 36703 polyèdres. J. Ecole Polytechnique, 9(16):68–86,
872 94 1066 10050 128995 1813.
2507 10 2407 2526 1333547
[5] Bernard Chazelle and Herbert Edelsbrunner. An op-
Table 1. Results of algorithm tests timal algorithm for intersecting line segments in the
Based on these results we conclude that performance is plane. Journal of the ACM, 39:1–54, 1992.
acceptable for on-line processing in sets with less than [6] Ken Clarkson and Peter W. Shor. Applications
three-hundred lines like hand-sketches or small-size tech- of random sampling in computational geometry, ii.
nical drawings. If the line set have about 2500 lines the Discrete and Computational Geometry, 4:387–421,
algorithm will take more than twenty minutes to detect the 1989.
polygons. Still this remains a feasible solution for batch
processing of medium-size technical drawings. [7] Thomas Cormen, Charles Leiserson, and Ronald
6. CONCLUSIONS and FUTURE WORK Rivest. Introduction to Algorithms. MIT Press,
McGraw-Hill, 2nd. edition, 1990.
The proposed algorithm is used for polygon detection in
vector drawings to create descriptions based on spatial and [8] U. Dogrusöz and M. Krishnamoorthy. Cycle vector
topological relationships between polygons. Other use is space algorithms for enumerating all cycles of a pla-
detecting planar shapes in sketches. Both applications have nar graph. Technical Report 5, Rensselaer Polytech-
been implemented as working prototypes used for shape nic Institute, Dept. of Computer Science, Troy, New
retrieval and architectural drawing from sketches. York 12180 USA, January 1995.
The algorithm presented here detects in polynomial time [9] Leonhard Euler. Elementa doctrinae solido-
and space, all minimal polygons that can be constructed rum. Novi Commentarii Academiae Scientiarum
from a set of line segments. This approach uses well- Petropolitanae, 4:109–140, 1752.
known and simple to implement algorithms to perform
line segment intersection detection and to find a MCB of [10] David Hartvigsen and Russel Mardon. The all-pairs
a graph, instead of using more efficient but less simpler minimum cut problem and the minimum cycle basis
methods. problem on planar graphs. SIAM Journal on Comput-
ing, 7(3):403–418, August 1994.
Indeed there is considerable room for improvement in the
presented algorithm, namely through the use of more re- [11] John Hobby. Practical segment intersection with fi-
cent, complex and efficient algorithms. Further work may nite precision output. Computational Geometry: The-
be carried out regarding the detection and correction of ory and Applications, 13(4), 1999.
rounding errors resulting from finite precision computa-
tions. [12] J.D.Horton. A polynomial-time algorithm to find the
shortest cycle basis of a graph. SIAM Journal on
7. ACKNOWLEDGMENTS
Computing, 16(2):358–366, April 1987.
We thank to Professor Mukkai S. Krishnamoorthy from
Rensselaer Polytechnic Institute, New York, for his very [13] J.L.Bentley and T.Ottmann. Algorithms for reporting
helpful suggestions. and counting geometric intersections. IEEE Transac-
tions on Computers, pages 643–647, 1979.
This work was funded in part by the Portuguese Founda-
tion for Science and Technology, project 34672/99 and the [14] Prabhaker Mateti and Narsingh Deo. On algorithms
European Commission, project SmartSketches IST-2000- for enumerating all circuits of a graph. SIAM Journal
28169. on Computing, 5(1):90–99, March 1976.
References
[15] Joseph O’Rourke. Computational Geometry in
[1] Ivan J. Balaban. An optimal algorithm for finding
C, chapter Section 7.7 ”Intersection of Segments”,
segment intersections. In Proceedings of the 11th
pages 264–266. Cambridge University Press, 2nd
Annual ACM Symposium Comp. Graph., pages 211–
edition, 1998.
219. ACM, 1995.
[2] Ulrike Bartuschka, Kurt Mehlhorn, and Stefan Naher. [16] Maciej M. Syslo. An efficient cycle vector space al-
A robust and efficient implementation of a sweep line gorithm for listing all cycles of a planar graph. SIAM
algorithm for the straight line segment intersection. Journal on Computing, 10(4):797–808, November
In Proceedings of Workshop on Algorithm Engineer- 1981.
ing, pages 124–135, Venice, Italy, September 1997.

You might also like