Bresenham's Line Generation Algorithm with Built-in Clipping

Embed Size (px)

Citation preview

  • Volume 14, (1995) number 5 pp. 275-280

    Bresenhams Line Generation Algorithm with Built-in Clipping

    Yevgeny P. Kuzmin

    IBS Graphics Technologies, 46/2 Dmitrovskoje sh., Moscow, 127238, Russia [email protected]

    Abstract One of the most important operations in many graphical systems is the generation of a line segment. This process consists of two stages: clipping and drawing. These two stages are separated in current graphicalapplications. In this paper a new approach to line generation is proposed, which unifies these stages. The proposed algorithm is based on Bresenhams line generation algorithm to include necessary line clipping. The line clipping stage is an operation-reduced, integer arithmetic only algorithm. The notion of correctness of line clipping is introduced and correctness of the proposed algorithm is shown. Complete C-notation of the algorithm is included.

    Keywords: raster graphics, line drawing, clipping.

    1. Introduction

    One of the most important and fundamental opera- tions in many graphical systems is generation of a line segment. The efficiency of raster device usage strongly depends on the the efficiency of this algorithm.

    The process of generation of a line segment consists of two stages. The first is clipping of the segment by a given planar rectangle - the window. At this stage that part of the line segment within the window is determined. In the second stage, a raster representation of the clipped segment is generated.

    There are three main methods for line clipping1 :

    encoding clipping; divide clipping; parametric clipping;

    These methods determine the part of the given segment within the given rectangle, by calculation of intersection points of the segment and the window rectangle.

    The clipped segment must then be represented by a sequence of pixels, which closely approximate this segment. Usually, segments are drawn by using some modification of Bresenhams line generation algorithm2 (BLGA).

    The Eurographics Association 1995. Published by Blackwell Publishers, 108 Cowley Road, Oxford OX4 1JF, UK and 238 Main Street, Cambridge, MA 02142, USA.

    In existing line generation processes these two stages are separated. The first stage produces rounded coordi- nates of the endpoints of the clipped segment. This may produce incorrect results : raster representation of the clipped segment may differ from raster representation of the original unclipped segment within the window.

    This may be demonstrated by drawing several ran- domized unclipped segments, then setting up the clipping area and again drawing these segments in XOR mode. Usually the unerased points will all be seen within the clipping area. This is as a result the inherent error in the line generation process used.

    In this paper we propose an efficient integer- based clipping algorithm based on BLGA with correct output.

    After introducing the necessary definitions and no- tions we briefly review existing methods of clipping and drawing. Then we give our interpretation of BLGA, introduce a notion of correctness and describe our pro- posed method and its implementations. Finally we ana- lyze the proposed method.

    The appendix contains the complete C-notation of the proposed algorithm.

    275

  • 276

    2. Notions and Definitions

    We now introduce some necessary definitions and nota- tions (see Figure 1) .

    Segment - a line segment S defined by its four coordi- nates:

    Bounding Box - a rectangular area B defined by the coordinates of segment

    Window - a rectangular area W defined by its four coordinates:

    Window Regions - 9 regions on the plane, defined by the window;

    Clipped Segment - that part of the line segment inside the window

    We also denote:

    Without loss of generality, we shall assume that the following conditions are valid:

    In this case the segment is called standard. In other cases we can transform our construction to the stan- dard configuration: this process is called standardiza- tion. Below, we propose that segment and window are in standard configuration.

    There exist several trivial cases of segment/window juxtaposition which do not require arithmetical calcula- tion for clipping:

    trivial reject: the whole segment is invisi- ble. trivial accept: the whole segment is visible, we draw it by the standard Bresenham algorithm. box degradation: the given segment is horizon- tal or vertical, and by simple comparisons of segment and window coordinates we determine its visible part.

    Beyond these cases, the segment/window interposition is nontrivial.

    3. Line Clipping

    In this section we briefly review existing line-clipping algorithms. Detailed consideration of this topic may be found in Rogers1 or in Foley, et al.3.

    Encoding clipping is based on subdivision of the plane into nine non-overlapped regions (see Figure 1) and as- signment of binary location codes to the endpoints of

    Figure 1: Segment-window juxtaposition.

    the segment. Using these codes we may determine seg- ment/window juxtaposition and points of intersection of the segment with the window. This technique was proposed by Cohen and Sutherland1, and was devel- oped by Sobkow, Pospisil, and Yang4. Calculation of the endpoints of a clipped segment in this method uses floating point multiplication and division.

    Another approach making use of the location codes was implemented by Sproull and Sutherland5 in the clip- ping divider algorithm. In this algorithm the intersection points are determined by a binary search for the inter- section. This algorithm does not require floating point operation and is used for hardware implementation.

    Yet another method uses the parametric representa- tion of a line segment to determine intersection points. This technique is used in algorithms by Cyrus and Beck6, Liang and Barsky7, and Dorr8. The last algorithm unlike the first two, is an integer-based clipping algorithm.

    Calculation of the coordinates of endpoints of clipped segment in all these previous algorithms includes round- ing of the coordinates. This leads to errors at the drawing stage.

    4. Line Drawing

    Drawing a line segment is the process of determin- ing a sequence of pixels which most closely approxi- mate the segment. There exist several algorithms for line drawing, proposed by Bresenham2, Earnshaw9, Pitteway and Green10. All of them are based on the incremental aproach proposed by Bresenham. The most commonly

    The Eurographics Association 1995

    Yevgeny P. Kuzmin / Bresenhams Line Generation Algorithm with Built-in Clipping

  • Yevgeny P. Kuzmin / Bresenhams Line Generation Algorithm with Built-in Clipping 277

    used method for line drawing is Bresenhams line gen- eration algorithm ( BLGA ).

    4.1. Bresenham Line Generation Algorithm

    For the standard segment in the first octant, BLGA looks like this:

    The original derivation of this algorithm is given in detail in Rogers1. We shall consider another interpreta- tion of BLGA, which will be useful for our purposes.

    4.2. BLGA Interpretation

    Let us consider the following coordinate transformation:

    After this transformation the segment bounding box B becomes square and the segment becomes its diag- onal ( the slope of segment becomes ). Every co- ordinate cell is transformed into rectangle with sides

    We must now choose the cell node which is nearest to the given segment. Initially there are two candidates S and D (figure 2). Let A be the point of intersec- tion of the given segment and segment [D,S], then

    because is equal to Let M be the middle point of segment [DS], then

    Let us denote the y-coordinates of points A and M as yA and yM respectively, and e = yA - yM is the difference of these coordinates. Then if yA yM (or in

    (2 * dy, 2 * dx).

    Figure 2: BLGA interpretation.

    other words e 0) we must select point D as the nearer and point S otherwise.

    Before the first step the value of the difference is equal to the initial value

    of the control variable in BLGA. Further, if we know the current intersection point and the difference e, between the y-coordinates of the point of next intersection with the vertical cell line and the middle point in the current cell we can correctly choose the nearest cell node.

    Let us consider how this variable e changes from step to step. As it follows from the definition of point A ev- ery step of the line generator changes its y-coordinate:

    If we execute horizontal steps the value of yM does not change, but at the diagonal step yM changes to: Therefore, in terms of e, the changes are on every step, and additionally on the diagonal step, This reasoning allows us to derive the normal BLGA nota- tion.

    5. The New Algorithm

    We now consider the new line-generation algorithm based on BLGA with correct output.

    By correctness of output we mean that an image of the clipped line is equal to the image of the original line inside the window. Obviously the drawing is correct in this sense for all trivial cases. We now consider a correct line generation algorithm for nontrivial segment-window interposition.

    The main idea of the proposed algorithm is the unifi- cation of the clipping and drawing stages. For this pur-

    The Eurographics Association 1995

  • 278

    pose we must correctly set up initial values for position variables, the error term and the termination condition for BLGA during the clipping.

    5.1. Window Entry

    If the start point of the segment lies within the clipping window, initialization does not differ from the original BLGA initialization.

    Let us suppose that the start point lies outside the clipping window. Initial setting depends on whether the segment enters the window in horizontal or vertical side.

    Suppose that the segment has vertical entry. We can then calculate the y-coordinate of the vertical entry point yd as follows:

    Note here that the last two operations are executed during one integer assembler division (IDIV) on most processors. Further, we calculate it with one division operation.

    Let us analyze the values of these variables. First, we must determine those segments which are rejected. The corresponding rejection condition is:

    We determine next whether the segment has vertical entry or not. The corresponding vertical entry test is:

    The first parts of these conditions are self evident, and the second parts are derived from our definition of correctness. If the last test fails, our proposition about vertical entry was incorrect, and we proceed with the horizontal entry test.

    We now set up the value of the control variable of BLGA for the segments with vertical entry using the value of the remainder and our previous reasoning (4.2.).

    If the remainder is less than dsx, then the relative coordinates of the middle point M, intersection point A and the value of control variable e will be as follows:

    If the remainder is greater than or equal to dsx, then corresponding values will be as follows :

    position BLGA variables are the same as corresponding values for the unclipped line in BLGA at the vertical entry point.

    The reasoning for horizontal entry is much the same as for vertical but with the following difference : for correct entry of BLGA we must calculate the x-coordinate of the horizontal entry point xd, not as the intersection with the bottom window line, but with the horizontal line y = wy1 - 1/2:

    The analysis of entry point coordinates is the same as before. We reject the entire segment if xd is greater than the coordinate of the right side of the window. Values of BLGA variables are calculated as follows:

    We can now perform the correct entry into the window in both cases. Which of the two cases occurs for a given segment can be discovered by using window regions. If the start point lies in region 4 we have vertical entry. If the start point lies in region 8 we have horizontal entry. If the start point lies in region 7 we first test for horizontal entry as described above.

    Thus control and position BLGA variables are initial- ized and we can begin generating pixels of the segment inside the window. However, we must cancel this pro- cess on the window border or, in other words, set the termination condition for BLGA.

    5.2. Window Exit

    If the end point of the segment lies within the clipping window the termination does not differ from the original BLGA. Otherwise the termination condition depends on whether the segment exits the window through a horizontal or vertical side.

    For vertical exit, the termination value is equal to the x-coordinate of the right side of the window.

    For horizontal exit, the termination value is equal to the x-coordinate of the exit point (point of intersection of the segment and the horizontal line y = wy2 + 1/2).

    Finally, the termination setting looks like this:

    After this operation, the values of the control and

    The Eurographics Association 1995

    Yevgeny P. Kuzmin / Bresenhams Line Generation Algorithm with Built-in Clipping

  • Yevgeny P. Kuzmin / Bresenhams Line Generation Algorithm with Built-in Clipping 279

    The values of all BLGA variables are now initialized, and drawing of the clipped line is produced by the usual BLGA.

    Another way to execute window exit is to check whether the current pixel generated by BLGA is inside the window or not. This allows us to exclude termi- nation setting, but complicates the step processing for software implementation. This method is good for hard- ware implementation, because the internal test consists of independent comparisons, which may be parallelized.

    A complete C-notation of the proposed algorithm with standardization is presented in the Appendix.

    6. Discussion of the Algorithm

    The proposed algorithm yields correct output in the sense we defined, because :

    the initial values of all variables at the entry point are the same in the proposed algorithm and Bresenhams algorithm. the set up of the termination condition allows us to execute the correct exit from the window.

    In the proposed algorithm, coordinate rounding is absent since it is substituted by in line calculation of the control variables for BLGA.

    Note that we use only integer operations and their number is equal to or less than those in the referenced clipping algorithms. In the worst case we have three division and three multiplication operations.

    The operations executed with standardization are also necessary for BLGA, so the total number of operations for both clipping and drawing in sum is also reduced.

    It follows from the arguments described in the paper that the number of required operations depends on the order of passing of end points of a segment. This is conditioned by different numbers of operations required for initialization and termination setup for centrally- symmetrical situations. By analysis of segment endpoints and window regions interposition we may choose the order of segment passing to reduce the number of op- erations. This preprocessing of the segment may be exe- cuted before clipping, during standardization of segment position.

    Also we may reduce the number of operations by careful examination of segment/window interposition as in the algorithm of Nicholl et al.11.

    7. Conclusion

    A new approach to the line drawing process is pro- posed, which unifies the clipping and drawing stages. The proposed line generation algorithm is based on Bresenhams line drawing algorithm. Line clipping is an operation-reduced, integer arithmetic only pro- cess.

    References

    1.

    2.

    3.

    4.

    5.

    6.

    7.

    8.

    9.

    10.

    11.

    D. F. Rogers, Procedural Elements for Computer Graphics, McGraw-Hill, New York (1985).

    J. E. Bresenham, Algorithm for computer control of a digital plotter, IBM Systems Journal, vol. 4, n.

    J. D. Foley, A. van Dam, Steven K. Feiner, and John F. Hughes, Computer Graphics: Principles and Prac- tice, Addison-Wesley Publishing Company, second edition, (1990)

    M. S. Sobkow, P. Pospisil, Y. H. Yang, A fast two-dimensional line clipping algorithm via line en- coding, Computers and Graphics, vol. 11, n.4, pp. 459-467 (1987).

    R. F. Sproull, I. E. Sutherland, A clipping divider, In Fall Joint Computer Conference 1968, Thompson Book, Washington DC, pp. 765-775 (1968).

    M. Cyrus, J. Beck, Generalized two- and three- dimensional clipping, Computers and Graphics, vol.3, n. 1, pp. 23-28 (1978).

    Y. P. Liang, B. A. Barsky, A new concept and method for line clipping, ACM Transaction on Graphics, vol.3, n.1, pp. 1-22 (1984).

    M. Dorr, A new approach to parametric line clip- ping, Computers and Graphics, vol.14, n.3/4, pp. 449-464 (1990).

    R. A. Earnshaw, Line tracking with incremental plotters, Computer Journal, vol. 23, n. 1 (1980).

    M. L. V. Pitteway, A. Green, Bresenhams al- gorithm with run-line coding shortcut, Computer Journal, vol.25, n.1, pp. 114-115 (1982).

    T. M. Nicholl, P. T. Lee, R. A. Nicholl, An efficient new algorithm for 2D line clipping: Its development and analysis, Computer Graphics, vol.21, n.4, pp.

    1, pp. 25-30 (1965).

    253-262 (1987).

    The Eurographics Association 1995

  • 280

    Appendix A: Listing of algorithm

    The Eurographics Association 1995

    Yevgeny P. Kuzmin / Bresenhams Line Generation Algorithm with Built-in Clipping

    1. Introduction 2. Notions and Definitions 3. Line Clipping 4. Line Drawing 4.1. Bresenham Line Generation Algorithm 4.2. BLGA Interpretation

    5. The New Algorithm 5.1. Window Entry 5.2. Window Exit

    6. Discussion of the Algorithm 7. Conclusion References Appendix A: Listing of algorithm