Applied Mechanics and Materials ISSN: 1662-7482, Vols. 239-240, pp 1313-1317 doi:10.4028/www.scientific.net/AMM.239-240.1313 © 2013 Trans Tech Publications, Switzerland Online: 2012-12-13 An improved Cohen-Sutherland region encoding algorithm Wei Wei1, a, Peihua Ma1,b and Wei Lin1,c 1 School of Computer, Electronics and Information, Guangxi University, No. 100 Daxue Road Nanning, Guangxi 530004, P.R.CHINA a b c [email protected], [email protected], [email protected] Keywords: computer graphics, Cohen-Sutherland algorithm, line clipping, rectangular window Abstract. Through the arduous study of the famous Cohen-Sutherland algorithm, this paper proposes a novel improved algorithm by analyzing its main flaw in efficiency. The line segment without the clipping window will be rejected in our algorithm. By means of adding auxiliary straight-line, this algorithm efficiently calculates intersection points between line and the boundary of clipping window. Thus, this improved algorithm is simple, improving its clipping efficiency and reaching the desired results. Introduction The line clipping algorithm on a 2D surface was one of the important subjects in computer graphics. Cut a series of shapes (lines, polygons, curves and surfaces) in different crop windows (rectangle, polygon, circle and oval). This clipping algorithm can help us display a part or all of the graphics in the visible window on 2D surfaces. If the graphic is out of a visible window in full, we can cut and discard the parts outside the window, and display parts must be showed in the visible window. Its essence is to find out the “intersection”, “difference” and the “union” between the visible window and the geometric figure. To improve the clipping efficiency, lots of researches have received some valuable results. In this subject, the Cohen-Sutherland region encoding algorithm [1] is one of the well-known algorithms. In addition, there are a lot of algorithms, such as the Midpoint segmentation algorithm, the Liang-Barsky algorithm [2], the Nicholl-Lee-Ncholl algorithm [3], the FLC algorithm [4] and the ELC algorithm [5]. More detailed description can be found in references [6,7]. In this paper, we present an improved Cohen-Sutherland clipping algorithm that can be pre-judged the location of segment with the visible window, so we need to encode the endpoints of the segment only once and reduce the number of counts in calculating intersection points between the visible window and the line segment. Cohen-Sutherland clipping algorithm The Cohen-Sutherland algorithm [1] is the first line clipping algorithm. The basic idea of this algorithm is: For each segment, it was divided into three cases. (1)If the line is completely within the visible window, to display it completely, called “take”; (2) if the line is located outside the visible window obviously, clipping it so that it is not display in the visible window, called “abandon”; (3) if the segment does not belong to the two situations above, to divide it into two parts. Reject one part that outside the visible window completely and repeat the steps with the remaining part. We can judge the position relation of the segment and the visible window by the method of region encoding. The specific process is below: extend each side of the window firstly, so that the plane is divided into nine parts. And then we give each part a four-digit binary code. In this binary code, each digit from right to left represents that the region is located on the left, right, below or top of the visible window in turn (Figure 1). Set it to 1 in the case of the endpoint is inside the region, else set 0. Followed by the basic idea of the algorithm and the region encoding rule, we can work like this: set the line segment endpoint coding for code1 and code2. (1) If code1 | code2==0, inside the visible window, take; (2) If code1 & code2!=0, outside the visible window, abandon; All rights reserved. No part of contents of this paper may be reproduced or transmitted in any form or by any means without the written permission of Trans Tech Publications, www.ttp.net. (#69811230, Pennsylvania State University, University Park, USA-18/09/16,09:02:58) 1314 Measurement Technology and its Application (3) If the segment does not belong to the two situations above, divide it into two parts. Find out the intersection of the line with the edge of the visible window and abandon the part that outside the window completely and repeat the steps with the remaining part. We can learn from the algorithm flow that it is only needs to be judged from the codes in the first two cases. But the third case we need to calculate the intersection. 1001 0001 0101 1000 1010 1001 1000 C D 0000 0010 0001 0100 0110 0101 0000 B A E 0100 1010 0010 0110 F Figure 1. Region code of window on 2D surface. Figure 2. Find the intersections. Obviously learn from Figure 2 that the line does not intersect with the visible window, so it can be abandoned. But the first two steps cannot determine directly. So it must be a waste of time to calculate the intersections of the segment with the top and left borders. The line intersects with the visible window. And we can see from the figure that it only needs to calculate the intersections of the segment with the top and left borders, too. The line is the same as line. We can obtain from the above analysis: (1) The second case cannot be “abandoned” all of segments that outside the visible window completely, and it was a waste of time to calculating the intersections. (2) When the segments have some intersections with the visible window just like segment and Segment, we only need to calculate two valid intersections. If we judge through the third case, it also cost extra time to calculating the invalid intersections for three or four times. Improved Cohen-Sutherland clipping algorithm To solve the problem, we propose an improve algorithm that can improve the efficiency of the algorithm. It was proved to be effective by experiments. Concrete can be classified into the following cases. Clipping algorithm classification Through analysis, the classification of conditions amended as follows (Table 1). In Table 1, classification 1 and 2 can be judged through the original algorithm directly. The following other two cases will be discussed separately. Table 1. Classification of line clipping algorithm. Classification Condition 1 code1 | code2==0 Take 2 code1 & code2!=0 Abandon 3 code1 & code2==0 and do not intersect Abandon 4 code1 & code2==0 and intersect Take a part Improved method of coding judgment cannot be abandoned Treatment Applied Mechanics and Materials Vols. 239-240 1315 For the classification 3, if we can judge that the line segment and the visible window are located on the same side, it will prove that they do not have an intersection. So we can compute the vector product. Definition of the vector product: a × b = a b sin 〈a, b〉 n 〈a, b〉 is the angle between vector and ( 〈a, b〉 ∈ [0, π ] ), it is located on the plane which the two vectors defined. And it is the unit vector that perpendicular to the plane. As can be seen from Figure 3, the lines from an endpoint of the segment to all of the vertices of the visible window were drawn. So it can get four unit vectors in different directions. And then we select the line segment in one direction as a reference. To compute the vector product and judged: if the four groups of vector product with the same sign, it can be concluded in the case that does not have an intersection. So it is judged to be abandoned. B 1001 1001 1000 1000 1010 1010 B D C A 0001 0001 F 0101 0000 0010 0100 0110 0010 0000 E 0100 0110 Figure 3. Draw lines from one endpoint. 0101 V A Figure 4. Draw the auxiliary line. Reduce the number of intersection calculation In this section, we deal with the situation that line segment have some intersections with the visible window. If the line segment has one endpoint inside the visible window and the other is outside, still deal with it according to the original Cohen-Sutherland algorithm. It only describes the special situation, just as we have seen in Figure 2. The line segment has four intersections with the visible window when one endpoint of it is located in the corner regions where the binary code is 1001, 1010, 0101 or 0110. And there are three intersections when one endpoint of the segment on the left, right, up or down regions. Take the line segment in Figure 2 for example. It can easily judge from the figure that we can only calculate the intersections where on the left boundary and the top boundary of the visible window. And the line segment has to calculate the intersections on the right and below boundaries, similarly. The original Cohen-Sutherland algorithm costs extra time to compute one or two intersections in that case. It is a waste of time. So we try to improve it like this. Firstly, mark the two endpoints of the line segment as A ( x0 , y0 ) and B ( x1 , y1 ) .As we mention above, each digit of the binary region code is represent that the region is located on the left, right, below or top of the visible window in turn. So we can find out a reference vertex near the endpoint. Mark it as V ( xi , y j ) , ( xi , y j ) ∈ {( xl , yb ) , ( xl , yt ) , ( xr , yb )( xr , yt )} , and the subscript l , b, r or t is representative of the left, the below, the right and the top. Secondly, according to the equation L ( x, y ) = ( y − y0 )( xi − x0 ) − ( x − x0 ) ( y j − y0 ) , we draw an auxiliary straight line L from one endpoint A to the reference vertex V . In Figure 4 the plane is divided into three parts by the auxiliary line. When L ( x, y ) > 0 , L ( x, y ) = 0 or L ( x, y ) < 0 , it respectively means that the endpoint is above, on or below the auxiliary line. If the other endpoint B ( x1 , y1 ) takes into the equation and L ( x1 , y1 ) > 0 , the intersection is in the left boundary of the window. L ( x1 , y1 ) < 0 , the intersection is in the low boundary. L ( x1 , y1 ) = 0 , result can be calculated in both. Method for calculating the intersection is the same as the Cohen-Sutherland algorithm. All of the instances are listed below. 1316 Measurement Technology and its Application When code1==0101, the reference vertex is V ( xl , yb ) . If L ( x1 , y1 ) ≥ 0 , the line segment is cross the left boundary x = xl . Else, the below boundary y = yb ; When code1==1001, the reference vertex is V ( xl , yt ) . If L ( x1 , y1 ) ≥ 0 , the line segment is cross the top boundary y = yt . Else, the left boundary x = xl ; When code1==1010, the reference vertex is V ( xr , yt ) . If L ( x1 , y1 ) ≥ 0 , the line segment is cross the top boundary y = yt . Else, the right boundary x = xr ; When code1==0110, the reference vertex is V ( xr , yb ) . If L ( x1 , y1 ) ≥ 0 , the line segment is cross the right boundary x = xr . Else, the below boundary y = yb . It is in accordance with the method mentioned above to deal with the endpoints which belong to the angular region in the case of the line segment had three intersections with the window. So it can reduce the times of calculating intersects. Steps of the algorithm and efficiency analysis The improved Cohen-Sutherland clipping algorithm judge the line segment by regional encoding and logical calculation. It can abandon the line segment that outside the visible window completely and reduce the intersection calculated amount from 4 to 2. It is simple and easy to be implemented. The algorithm steps are as follows: Step1: Calculate the binary code of the two endpoints as code1 and code2; Step2: If code1 | code2==0, take all of it and turn to Step8; otherwise turn to Step3; Step3: If code1 & code2! =0, abandon all of it and turn to Step8; otherwise turn to Step4; Step4: Judge through the vector product. If it located on the same side, abandon all of it and turn to Step8; otherwise turn to Step5; Step5: Judge the position of the intersection by the auxiliary equation and determine the crossing boundary. Turn to Step6; Step6: Calculate the intersection coordinates and mark as ( x0 , y0 ) and ( x1 , y1 ) . Turn to Step7; Step7: Calculate the two intersections as the two endpoints of the input, Turn to Step1; Step8: End Below is the theoretical analysis for the improvements in intersecting efficiency. Assume that there are k line segments to cut and the intersections with the visible window mark n ( n = 3 or n = 4 ). So it must do linear operation (n × k ) times and [(n − 1) × k ] times to calculate the symbol by the original algorithm. And it needs only do linear operation (2 × k ) times to calculate the symbol and (2 × k ) times to calculate the intersection with the improved algorithm. Table 3. The experiment results (Cut 10000 times time proportion). Average time (ms) Three intersections Four intersections Original 27.75 31.67 Improved 21.50 22.11 Table 2. The experiment results (Cut 10000 times average time). Time proportion Three intersections Four intersections Theoretical 80.0% 57.1% Experimental 77.4% 69.8% Let k = 10000 and n = 3 , amount of computation in theory is: Original: 3 × 10000 + (3 − 1) × 10000 = 50000 Applied Mechanics and Materials Vols. 239-240 1317 Improved: 2 × 10000 + 2 ×10000 = 40000 Time required by the improved algorithm is about 80% of the original. Let k = 10000 and n = 4 , Original: 4 ×10000 + (4 − 1) × 10000 = 70000 Improved: 2 × 10000 + 2 ×10000 = 40000 Time required by the improved algorithm is about 58% of the original. Efficiency of the improved algorithm to cut the line segment had four intersections with the visible window is much greater than that of the segment with three intersections. Our experiments generated nine sets of data randomly and calculated the average time that clipping the segment 10000 times. Experimental setup: Intel i5-2300 and 32 bits windows 7. The results are showed in Table 2 and Table 3. Conclusions The improved Cohen-Sutherland clipping algorithm judge the line segment by regional encoding and logical calculation. It can abandon the line segment that outside the visible window completely and reduce the intersection The Cohen-Sutherland clipping algorithm is simple and easy to be implemented. However, it requires multiple computing the invalid intersection and other steps to complete the cutting for some special line segment. The algorithm proposed in this paper can largely avoid seeking invalid intersection. So it reduces the computation time and improves the efficiency of the algorithm. Acknowledgements This work is supported by Projects of education department in Guangxi (201106LX020). References [1] Donald Hearn and M. Pauline Baker: Computer Graphics with OpenGL 3rd Edition, Publishing House of Electronics Industry, Beijing (2005), p. 259 [2] Y.D. Liang and B.A. Barsky: Transactions on Graphics, Vol. 3 (1984), p. 1 [3] T.M. Nicholl, D.T. Lee and R.A. Nicholl: Computer Graphic, Vol. 4 (1987), p. 253 [4] Jun Wang, Youdong Liang and Qunsheng Peng: Journal of Computers, Vol. 7 (1991), p. 495 (in Chinese) [5] Haohong Wang, Ruixun Wu and Shijie Cai: Journal of Software, Vol. 9 (1998), p. 728 (in Chinese). [6] Zhulin Li and Gang: Computer Engineering and Applications, (2011), in press (in Chinese) [7] Li Zhong and Yanping Ma: Journal of Engineering Graphics, Vol. 5 (2009), p. 49 (in Chinese).
© Copyright 2025 Paperzz