Polygon Detection From A Set of Lines
Polygon Detection From A Set of Lines
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
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.