Visibility Line in a Direction for a Set of Parallel Line Segments Radu Litiu Department of Electrical Engineering and Computer Science, The University of Michigan Ann Arbor, MI 48109-2122, email: [email protected] Dionysios I. Kountanis Department of Computer Science, Western Michigan University Kalamazoo, MI 49008, email: [email protected] Abstract In this paper we present a sequential algorithm that computes the part of a set of parallel line segments visible in a direction. The algorithm performs in O(n log n) time and O(n) time with preprocessing-sorting, where n is the number of line segments. The worst possible conguration of the line segments is determined to be a staircase arrangement with a garbage collection segment. keywords: visibility, staircase, garbage collector 1 Introduction Visibility and related problems have been the subject of intense study in computational geometry [1, 3, 7]. These problems can be classied into two wide categories: visibility from a point and visibility in a direction. The two categories are further divided into sub-categories depending on the objects in the domain of the problem, for example, intersecting or non-intersecting line segments, simple polygons, planes, etc. Applications of the visibility problems include the art gallery and illumination [1], minimum distance between Steiner trees [5], the skyline problem [6], etc. Two algorithms performing in linear time are described in [4] for a domain of segments in the plane that form a single simple polygon. In [9] various special types of visibility problems are explored through the denition of set-theoretic operations on visibility polygons. An algorithm that nds the visibility polygon of a point inside a polygon is used to compute the portion of a set of polygons that are visible in a given direction is described in [2]. Finally, in [6] a simple divide and conquer strategy is used to develop an O(n log n) algorithm that solves the skyline problem. This problem is easily reduced to the problem we are concerned in this paper. Our algorithm obtains solutions in O(n log n) time by using a simple data structure. The O(n log n) time is due to sorting of the line segments according to their coordinates. If the sorting has been previously performed the algorithm is linear, i.e. has time complexity O(n), where n is the number of line segments. The problem we are concerned in this paper is the following: Given a set of parallel line segments on the plane, nd their visibility part relatively to the perpendicular direction. 1 2 The Algorithm In the development of the algorithm below we assume that the parallel line segments are vertical and the direction horizontal. The dual case is solved similarly. If the parallel line segments have a random direction the same algorithm can be used to solve the problem by appropriately adjusting the coordinates of their endpoints. Let P denote a set of vertical segments, nd the visibility part of P in the horizontal direction. The algorithm is subject to the following additional assumptions: The n segments are viewed from right to left. Each segment s 2 P is specied by the Cartesian coordinates of its endpoints i.e. s = (x ; (y 1; y 2S)). Y = 1 fy 1; y 2 g is the array of points to be processed, we call them the signicant points. Two successive points of Y together with the corresponding x-coordinate dene a visibility segment. X is the array of the x-coordinates of the points in the visibility line. L is an array of forward links. The visibility segments are linked to form the visibility line. The semantics of the above data structures are as follows: the visibility line consists of all line segments (X[i], Y[i], Y[i+1]) at the end of the algorithm; at any point during the running of the algorithm, if L[i] 6= 0 then the visibility segments have already been found from Y[i] to L[i]. We have also introduced line segments of length 0 (null segments or points). We keep track of these segments through the following arrays: self - Boolean; xself - if self[i] = 1, then (xself[i], y[i]) is a null segment. The arrays Y, X, L, self, xself have all the same size m, equal with the number of signicant (break) points. i i i i i i n i i 1. Sort the segments in decreasing order of their x-coordinates. Let S be the sorted array of segments. 2. Sort the array Y in increasing order and eliminate duplicate points. 3. Fill the data structures Initialization for i = 0 to m-1 X[i] = L[i] = self[i] = 0 Main loop for i = 0 to n-1 //every segment in S (a) Test for null segments if y 1 = y 2 Find index k of y 1 in the array Y if self[k] = 0 self[k] = 1 i i i 2 xself[k] = x continue // go to the next step of the for loop Find indexes k, l of y 1 , y 2 in the array Y. (b) Find the new link for y 1 j=k while j < l if L[j] 6= 0 then j = L[j] else j = j + 1 while L[j] 6= 0 j = L[j] //go as far as possible j is now the new link (c) Update X[k] and the link L for all the points we went through in Step (b) r=k while r < l if L[r] = j then break p=r if L[r] 6= 0 then // already lled r = L[r] else // need to update x[r] x[r] = x r=r+1 L[p] = j while (L[r] 6= j) and (r 6= j) // update links p=r r = L[r] L[p] = j i i i i i 4. Construct the visibility line i=0 while i < m-1 if (self[i] = 1) and (xself[i] > x[i]) and ((i = 0 or (i > 0) and xself[i] > x[i-1])) new segment(xself[i], y[i], y[i]) j=i while (x[j] = x[j+1]) and not((self[j+1] = 1) and (xself[j+1] > x[j]) and (xself[j+1] > x[j+1])) j=j+1 new segment(x[i], y[i], y[j+1]) i=j+1 3 The following example illustrates the algorithm. Let n=9 parallel line segments given by their Cartesian coordinates (x ; (y 1; y 2)). The segments have already been sorted in decreasing order of their x-coordinates: (6, (1, 3)), (6, (13, 16)), (5, (9, 12)), (4, (1, 4)), (4, (10, 16)), (3, (13, 18)), (2, (3, 6)), (2, (12, 16)), (1, (3, 10)) Figure 1 illustrates the parallel line segments and the perpendicular direction d. Note that Figure 1 has been rotated. i i i x 6 5 d 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 y Figure 1: The visibility area of a set of segments The array Y of signicant points contains: Y = f1, 3, 4, 6, 9, 10, 12, 13, 16, 18g At the beginning of the main loop X[i] = L[i] = self[i] = 0, 8 i = 0,..,m-1. After processing the rst two segments, (6, (1, 3)) and (6, (13, 16)), the arrays Y, X and L have the following values: Y 1 3 4 6 9 10 12 13 16 18 X 6 0 0 0 0 0 0 6 0 0 L 3 0 0 0 0 0 0 16 0 0 The subsequent step by step congurations of the arrays are listed in order below: Y 1 3 4 6 9 10 12 13 16 18 X 6 0 0 0 5 5 0 6 0 0 L 3 0 0 0 12 12 0 16 0 0 Y 1 3 4 6 9 10 12 13 16 18 X 6 4 0 0 5 5 0 6 0 0 L 4 4 0 0 12 12 0 16 0 0 Y X L Y X L Y X L 1 6 4 1 6 4 1 6 4 3 4 4 3 4 4 3 4 6 4 0 0 4 0 0 4 2 6 6 9 10 12 13 16 18 0 5 5 4 6 0 0 0 12 16 16 16 0 0 6 9 10 12 13 16 18 0 5 5 4 6 3 0 0 12 16 16 18 18 0 6 9 10 12 13 16 18 0 5 5 4 6 3 0 0 12 16 16 18 18 0 4 Y 1 3 4 6 9 10 12 13 16 18 X 6 4 2 0 5 5 4 6 3 0 L 4 6 6 0 12 16 16 18 18 0 Y 1 3 4 6 9 10 12 13 16 18 X 6 4 2 1 5 5 4 6 3 0 L 4 12 6 12 12 16 16 18 18 0 From the last line of the nal table we obtain the visibility line indicated by the shaded line segments in Figure 1. The coordinates that dene the visibility line computed by the algorithm are: (6, (1, 3)), (4, (3, 4)), (2, (4, 6)), (1, (6, 9)), (5, (9, 12)), (4, (12, 13)), (6, (13, 16)), (3, (16, 18)) Theorem 1 The above algorithm has time complexity O(n log n), where n is the total number of segments, i.e. n = jP j. Proof The sorting in step 1 can be performed in O(n log n) time. The array Y has at most 2n elements. Therefore, the sorting in step 2 can be performed in O(2n lg 2n) = O(n lg n) time. The elimination of the duplicate points in step 2 can be performed in O(n) time and the remaining number of points in the array Y is O(n). The initialization in step 3 can be performed in O(n) time. There are n steps performed in loop (a). Both loops (b) and (c) in step 3 need to consider the same points, thus the number of steps in each one of these loops is the same. Sometimes (when the test L[r] 6= j in loop (c) fails, the link for the last signicant point considered has already been updated) loop (b) has one more step than loop (c). Therefore, we consider only the loop (b) in the computation of the complexity. An inductive proof can show that the time complexity of the main loop is O(n), being bounded by 11n, where n is the number of segments. Extensive runs of the algorithm experimentaly veries this bound. Increase on the number of signicant points increases the number of link updates. Therefore, the worst case conguration of the segments should have distinct y-coordinates of their 2n endpoints. It can be determined that the worst case is the one in which the segments have the distribution illustrated in Figure 2. The arrow represents the direction d of viewing the set of segments. The top (n-1) segments - the staircase - have their y 1 (the leftmost y-coordinates) in strictly increasing order and all of them are less than y12. A similar relationship is true for the y 2 's (the rightmost y-coordinates) of the top (n-1) segments, namely the y 2 coordinates are in strictly decreasing order. The last segment - the garbage collector - has y 1 > y12, y 1 < y 2 , 8 i = 2..n-1 and y 2 > y ?1 2 . The garbage collector is designed to introduce the maximum number of link updates. It is obvious that the cost for a perfect staircase (no garbage collector) is 4n-4 = 4(n-1). i i i n n n ; 5 n i d Figure 2: Worst case conguration - staircase plus garbage collector As illustrated in Figure 3, the costs C(2)/C(3) for 2/3 segments are C(2) = 4 = 42-4 and C(3) = 3+2+3 = 8 = 43-4 The arcs above the segments indicate the steps performed in the loop of the algorithm, i.e. the encountered signicant points. d Figure 3: The cost for 2/3 segments - staircase A1 A2 A3 An B1 B2 B3 Bn 1 2 3 d n Figure 4: The cost for n segments - staircase Figure 4 presents the general case. The points A , B denote the leftmost and rightmost points of the segments respectively . Notice that A1 A2 ...A B1 B2 ...B (denition of a staircase). Let C(n) be the cost of the algorithm for a n segment staircase, then C(n) = n+2+3(n-2) = 4n-4 The general case for the staircase plus a garbage collector is illustrated in Figure 5. If C0(n) is the cost for this conguration then C0 (n) = (n-1)+3(n-2)+(n-1) = 5n-8 In the latest case the last segment (garbage collector) adds a higher cost because the algorithm goes through all the rightmost points of the segments and establishes links from all these points to the end. If we continue to add more segments, the cost added by them will be low, since most of the links have already been established to the rightmost point by the garbage collector. We can prove inductively that this is the worst case with cost C0(n) = 5n-8. Base case: the cost for a perfect staircase with n segments is given by C(n) = 4n-4. i i n 6 n A1 A2 A3 B1 An B2 B3 Bn 1 2 3 d n Figure 5: The cost for n segments - staircase plus garbage collector The cost for n segments arranged in a staircase with a garbage collector is given by C0(n) = 5n-8. This represents the worst case conguration for n segments for n<4, as it can be determined by exaustive calculations. We now extend the perfect staircase of n segments by adding a n+1 segment. We consider all possible cases for the location of the new segment relatively to the previous n segments. Using the notation in Figure 4 the following are all possible cases: A +1 , B +1 < A1 The segment A +1 B +1 does not interfere with the others, therefore it introduces the additional cost of 1. A +1 < A1 , A < B +1 < A B +1 introduces an additional cost of 1 when we process A1 B1 . The processing of the segment A +1 B +1 introduces an additional cost of 2. Thus, the total additional cost introduced in this case is 3. A +1 < A1 , A < B +1 < B1 This case is the same as the previous one . A +1 < A1 , B1 < B +1 < B or more precisely B < B +1 < B +1 B +1 adds cost of 1 when processing A +1 B +1 (one more step at the end). A +1 , A1 , B1 are the points we will go through when we process A +1 B +1 . This adds 3 to the cost Thus, the total additional cost in this case is 4. A +1 < A1 , B < B +1 In this case there are no interferences with any other segment. A +1 , A1 , B1 , B are the points considered when we process A +1 B +1 . Thus the added cost for this case is 4. A < A +1 < A +1 < A < B +1 < B1 A +1 , B +1 adds a cost of 2 when processing A +1 B +1 . The cost to process A +1 B +1 is 1. Thus the additional cost is 3. A < A +1 < A +1 , B < B +1 < B +1 A +1 adds a cost of 1 when processing A1 B1 . B +1 adds also a cost of 1 when we process A +1 B +1 . A +1 , B1 adds a cost of 2 when processing A +1 B +1 . The total cost added in this case is therefore 4. A < A +1 < A +1 , B < B +1 A +1 adds a cost of 1 when processing A1 B1 . A +1 , B1 , B add a cost of 3 when we process A +1 B +1 . Thus in this case the additional cost is 4. A < A +1 < B +1 < B1 A +1 , B +1 adds a cost of 2 when processing A1 B1 . The cost to process A +1 B +1 is 1. Thus the total additional cost for this case is 3. th n n n n n i n j n n n n n n n n n i n i n n n n n i i n n n n i n n i j n n i i n i j n n n i n i n n i n n n n n i j n i n n n n n n n n n n n 7 n n A +1 < B1 , B < B +1 < B +1 A +1 adds a cost of 1 when processing A1 B1 . B +1 adds a cost of 1 when processing A +1 B +1 . A +1 , B1 add a cost of 2 when processing A +1 B +1 . Thus, the total added cost is 4. A < A +1 < B1 , B < B +1 A +1 adds a cost of 1 when processing A1 B1 . B +1 does not interfere with the other segments. The cost to process A +1 B +1 is 3. Thus, the total added cost is 4. B < A +1 < B +1 < B < B +1 < B +1 A +1 adds a cost of 1 when processing A +1 B +1 . B +1 adds a cost of 1 when processing A +1 B +1 . A +1 , B +1 , B +2 , ..., B are passed over when evaluating A +1 B +1 . In the worst case, i+1=2, j=n-1, which results in (n-1) additional cost. Thus, the total cost added in this case is (n+1). In all the previous cases the additional cost was at most 4. Thus, the total cost for n+1 segments is at most C(n+1) = 4(n+1)-4 = 4n. In the worst case the cost increases up to C0(n+1) = (4n-4)+n+1 = 5n-3, i.e. C0 (n) = 5n-8. B < A +1 < B +1 , B < B +1 A +1 adds a cost of 1 when processing A +1 B +1 . B +1 does not interfere in the processing. The additional cost for processing A +1 B +1 is at most n. Thus, the maximum total additional cost is n+1. This case, together with the previous one, represents what we call a staircase with a garbage collector, which is the worst possible case. B < A +1 < B +1 A +1 B +1 does not interfere with any other segment. The total additional cost is 1 in this case. Notice that after the addition of a garbage collector to a staircase, the points B +1 , ..., B will all be linked to B +1 . If we add a new segment with the properties of the garbage collector, the new cost will be much smaller, because the new segment will take advantage of the previously updated links. The same happens when we have a staircase with two garbage collectors: the rst garbage collector adds the cost for updating the links for all the segments preceding it, say k, and the second one adds the cost for updating the links for the remaining m-k segments, such that the total cost is bounded by 5m-8 for all the m segments. Therefore, we have proven by induction on the number of segments that the staircase with a garbage collector is the worst conguration for n line segments. The bound on the processing cost was also calculated to be C0 (n) = 5n-8. The loops (a), (b), (c) are bounded by costs n, 5n, 5n respectively. Thus the loops have time complexity O(n). Therefore, the whole algorithm has time complexity O(n log n). 2 A n < n i n i n i n i n n n n n n n n n i n i j n n j n j n i i n j n i i i n j i n n n n i n n n n n i n n n n i j j Corollary 1 Given a set of vertical line segments, sorted according to their x-coordinates and the sorted array that contains the y-coordinates of the endpoints of the segments, the visibility line of the set of segments relatively to the horizontal direction can be found in time O(n). 8 Proof The main loop has time complexity O(n). The O(n log n) time is due to the initial sorting. 3 Conclusion and future work We have proven that the visibility line of a set of parallel segments according to a direction can be obtained in O(n log n) time and in O(n) time with preprocessing. It would be interesting to develop an ecient algorithm when the given segments are not parallel. A related optimization problem is to nd the direction that maximizes/minimizes the length of the visibility line of a given set of line segments. References [1] S. G. Akl and Kelly A. Lyons, \Parallel Computational Geometry", Prentice Hall, Englewood Clis, N.J., 1993 [2] T. Asano and H. Umeo, \Systolic Algorithms for Computing the Visibility Polygon and Triangulation of a Polygon Region", Parallel Computing, Vol. 6, p.p. 209-216, 1988 [3] F. Dehne, \Solving Visibility and Separability Problems in a Mesh-of-Processors", The Visual Computer, Vol. 3, p.p. 356-370, 1988 [4] H. ElGindy and D. Aris, \ A Linear Algorithm for Computing the Visibility Polygon from a Point", J. Algorithms 2(2), p.p. 186-197, 1981 [5] D. Kountanis and K. Kokkinos, \A Balanced Approach to the Rectilinear Steiner Tree Problem", Congressum Numerantium, Vol. 8, p.p. 205-221, 1995 [6] V. Manber, \Introduction to Algorithms, a Creative Approach", Addison-Wesley Publishing Company, 1989 [7] K. Mulmuley, \Computational Geometry", Prentice Hall, 1994 [8] F.P. Preparata and M.I. Shamos, \Computational Geometry an Introduction", Springer-Verlag, N.Y., 1985 [9] G.T. Toussaint, \Pattern Recognition and Geometrical Complexity", Proceedings 5th International Conference on Pattern Recognition, p.p. 1324-1347, 1980 9
© Copyright 2026 Paperzz