Set theoretic operations on polygons using the scan-grid approach L M Patnaik*, R S Shenoyt and D Krishnan* This paper describes an algorithm to compute the union, intersection and difference of two polygons using a scan~grid approach. Basically, in this method, the screen is divided into cells and the algorithm is applied to each cell in turn. The output from all the cells is integrated to yield a repre- sentation of the output polygon. In most cells, no computa- tion is required and thus the algorithm is a fast one. The algorithm has been implemented for polygons but can be extended to polyhedra as well. The algorithm is shown to take O(N) time in the average case where N is the total number of edges of the two input polygons. algorithm, scan-grid approach, polygons The problem of computing the union, intersection and difference of polygons and polyhedra finds an important application in CAD work 1-4 . Examples of algorithms to carry out these operations can be found in the literature. For example see Sutherland 4 and Barton and Buchanan I . Bentley and Wood s describe an algorithm to report inter- sections of rectangles. Nievergelt and Preparata 2 discuss a plane sweep algorithm which computes s intersections of two convex polygons in O(nlogn+s) time, where n is the total number of edges of the two polygons. A grid cell approach has been employed by Tilove 6 to compute the wireframe representations of solids represented using con- structive solid geometry (CSG). In this paper, we use this approach to compute the result of performing a set theoretic operation on two polygons. As the algorithm is implemented on a line drawing display, it is assumed in the following that: the input is a set of edges and vertices of the input poly- gons the output from the algorithm is a set of edges and vertices of the output polygon An edge is defined as a line joining its starting and ending points where the points are referred in a clockwise manner. Thus, viewing the edge from the starting point, the polygon's inside is to the right of the edge. BASIC IDEAS BEHIND THE ALGORITHM The algorithm operates in the image space. Initially, the screen is divided into a number of cells by overlaying the scene with an n x n grid. Computations are performed on each cell to decide the output edge segments from the cell and finally all these outputs are integrated to produce the *Indian Institute of Science, Bangalore 560012, India. tComputer Aided Management Services, NGEF Ltd, Bangalore 560038, India complete output polygon. The algorithm scans the grid on a cell-by-cell basis to determine the output and hence the name scan-grid. The input polygons to the algorithm can be convex, concave or self-intersecting in nature. Consider Figure I which shows two intersecting polygons A and B overlaid by a 4 x 4 grid. When computing A fq B, we need consider only the cells where both the polygons are present namely, 6, 7, 10 and I 1. Thus, the other cells need not be considered at all. Similarly for A-B we need consider only the cells where A is present (ie 2, 3, 6, 7, 10 and 11 ) whereas for A U B, all cells excepting I, 4, 5, 8, 13 and 16 (ie the nonempty cells) are considered. Note that empty cells are not considered regardless of what the opera- tion is. There are mainly two situations encountered in each cell. These are: only one polygon exists in the cell both polygons exist in the cell Situation 1 Whenever only one polygon exists in the cell, the segments of the edges of the polygon, which are in the cell, are all either a part of the output or are not so. For example, in cell 2 only polygon A is present. The part of A that is in the cell is part of the output when computing A LJ B or A-B, When computing A N B or B-A, nothing within the cell is part of the output. ,< 13 I 2 6 J 14 I 3 / 4 I J 8 J J 16 .~- Poly~ A .~ Pol~n B Figure 1. Scene overlaM by a 4 x 4 grid volume 18 number 5 june 1986 0010-4485/86/050275-05 $03.00 © 1986 Butterworth & Co (Publishers) Ltd 275