Line rasterization algorithms that satisfy the subset line property

  • Published on
    21-Jun-2016

  • View
    214

  • Download
    0

Transcript

COMPUTER VISION, GRAPHICS, AND IMAGE PROCFSING 41,210-228 (1988) Line Rasterization Algorithms That Satisfy the Subset Line Property* M. L. P. VAN LIEROP, C. W. A. M. VAN OVERVELD, AND H. M. M. VAN DE WETERING Department of Mathematics and Computing Science, Eidhoven University of Technology, P. 0. Box 513, 5600 MB Eindhoven, The Netherlands Received October 10.1986 In this paper two subset line properties are introduced; the difference concerns the domain of the end points of the line segments under consideration, viz. Q* and Z* respectively. For both properties, a recursive and a nonrecursive algorithm to generate raster line segments are derived. All algorithms use integer arithmetic only. The accuracy of the algorithms in representing lines will be discussed, as well as their time complexity. 0 1988 Academic PXSS, IX 1. INTRODUCTION In raster graphics, where pictures consist of discrete elements (calkd pixels), objects in so-called world space have to be converted into rasterized analogs in screen space. In this paper we deal with the conversion of line segments into pixel sets representing these segments. A well-known algorithm for rasterizing line segments is that of Bresenham II]. Some of the properties of lines do not hold for their raster&d analogs. For instance, in R* two different nonparallel lines have exactly one point in common, whereas their rasterizations may have none, one, or more than one pixel in common. Also, in R* exactly one line exists through two given points, whereas two d&rent pixels may be elements of various raster&d lines. Another property that holds for line segments in R* is the subset line property, which may be formulated as follows. G iven a line segment L( p, q), where its end points p and q are points in R*, then (Ar,s E R: r,s E L(p,q): L(V) c L(p,q)). (A is used to denote the universal quarttiGer; (Ad E D: R(d): P(d)) means that for all d in domain D that satisfy restriction R(d), predicate P(d) holds. In the same way E denotes the existential quarttier.) The subset line property is of importance for operations such as clipping and hidden line elimination. Suppose we have a rasterization algorithm A that raster&s the line segment L( p, q) for any p and q in Z*. We will denote the resulting set of pixels, which is a subset of Z*, as A( p,q). In [2], the following analog of the subset line property for rasterized lines is given. *The investigations were partly supported by the Netherlands Technology Foundation (SW). 210 0734-189X/88 $3.00 Copyright 0 1988 by Academic Press, Inc. All rights of reproduction in any form reserved. SUBSET LINE PROPERTY 211 -- 0 4 8 12 16 FIG. 1. The Bresenham rasterization of the line segment connecting (0,O) and (16,5), indicated by 0, and of the segment connecting (8,2) and (14,4), indicated by 0. DEFINITION (of SPl). Rasterization algorithm A satisfies subset line property SPl if @P, 4, r, s E 2*: r, s E A(p, q): A(r, s) c A(p, q)). This property is applicable in situations where computations for updating the screen are performed in screen space. For example, if one wants to erase parts of rasterized line segments by overpainting the parts in question with the background color, one needs a rasterization algorithm that satisfies the above property. However, if the computations to update the screen are performed in world space, the above definition of SPl is of little use. For example, in programs, the intersec- tion point r of Up,, ql) ami Up2, q2), where pl, ql, p2, ad q2 E z*, is, in general, not an element of 2 *. Indeed, since Q is closed under all operations necessary to compute the intersection point of two line segments with end points in Q*, one would like to have a rasteriz.ation algorithm that allows elements of Q* as end points. We therefore arrive at the following definition. DEFINITION (of SP2). Algorithm A satisfies subset line property SP2 if (AP, 4, r, s E Q*: r, s E L( P, 4): A(r, s) C A( P, 4)). The Bresenham algorithm does not satisfy SPl; in Fig. 1, for example, two Bresenham rasterizations are shown: the first one of the line segment correcting (0,O) and (16,5), the second one of the line segment connecting (8,2) and (14,4). Although (8,2) and (14,4) are elements of the first rasterization, the second raster-ix&on is not a subset of the first one. An extended Bresenham algorithm that converts end points in Q* to the nearest elements of Z2 and then applies the original algorithm, does not satisfy SP2, as is illustrated by the line segments of Fig. 1 and r = (8, $) and s = (14, !$), which are converted to (8,2) and (14,4), respectively. Notions. Throughout this paper, elements of Q* are referred to as points. If p is a point and p = (x, y), then p.x = x and p.y = y. div b stands for integer division by b, defined by a = b*(adivb)+amodb,forallaEZ,where amod b E {O,l,. . . , b - l}. For denoting intervals we use [a, b] for the interval in R between (and including) u and b, and [a - - - b] for the interval in Z. Parentheses are used for denoting open intervals. We consider the screen to have size N, where N = 2, and n E N. N is supposed to contain 0. Furthermore, we consider lines with slopes between 0 and 1 only. 212 VAN LIEROP, VAN OVERVELD, AND VAN DE WETERING A line rasterization algorithm A is said to have a deviation of at most i pixels, if for all points p and q E 2 holds that (As, s: s E A(p, q) A s E L( p, q) A s.x = s.x: 1d.y - s.yl I i). In other words, A has a deviation of at most i pixels if the vertical distance of all points s E A( p, q) to L( p, q) is at most i pixels. The Bresenham algorithm has a deviation of at most $ [3]. Furthermore, a line rasterization algorithm A is said to be translation invariant if for all points p, q, and I E Z2 holds that A(p + t, q + t> = A(P, 4) @ t, where the operator d is defined by S@?= {s+tlsES}, for all S E Z* and t E Z*. Translation invariance implies that the rasterization depends on the relative positions of the end points only (the differences in the X- and y-coordinates), not on the absolute positions. In [2], Franklin suggests that no translation invariant algorithm exists that satisfies SPl and has a deviation of at most :. The algorithms presented in this paper satisfying SPl are translation invariant in the ~-direction only, i.e., A( p + ev, q + e,,) = A( p, q) CB e,,, where eu = (0,l). Besides presenting algorithms that satisfy SPl or SP2, and use integer arithmetic only, we want to distinguish between recursive and nonrecursive algorithms. Since recursive algorithms are attractive when parallelism is considered, this latter distinc- tion may be important for hardware implementations. In Section 2 algorithms satisfying property SPl are presented: from the recursive algorithm given in Section 2.1, a nonrecursive algorithm is derived in Section 2.2. The algorithms satisfying property SP2 are presented in Section 3: the nonrecursive one in 3.1, the recursive one in 3.2. Finally, in Section 4 the accuracy and time complexity of the algorithms presented in this paper are discussed. 2. ALGORITHMS SATISFYING SPl In this section all points are supposed to be elements of Z2. The algorithms presented in this section are based on the recursive definition of raster line segments with end points (0,O) and (N, 6), where 6 E N and 6 I N. This d&n&ion is then extended to raster line segments with end points (0, ry) and (N, 6 + ty), where ty E Z. These raster segments are called BEFLINES, and are used to define the raster line segment with end points p, q E Z2, where 0 5 p.x 5 q.x s N and 0 I q.y - p.y I q.x - p-x: it is the part between p and q of a REFLINE contain- ing both p and q. More formally, the raster line segment with end points p and q is the intersection of a REFLINE containing p and q, and [p.x . . . q.x] x N. Before this defunition is given, we prove that for all points p and q a BEFLINE exists that contains both p and q, and that for two d&rent REFLINES that both contain p and q, the intersection with [ p.x . . . q.x] X Z is the same. Any algorithm gener- ating such a segment for any n E N, p, q E Z2, will satisfy SPl. SUBSET LINE PROPERTY 213 FIG. 2. The rasterization of the line segment connecting (0,O) and (2, 6) is the union of the rasterizations of the line segment connecting (0,O) and m = (2-l, 6 div2) and the line segment connecting m and (2, 6). The raster line segment with end points (0,O) and (N, 6) will be defined by prescribing the y-value as a function of x, where x E [0 . . . N]. Hence, the set of pixels constituting this raster line segment contains N + 1 points. The definition of this function is inspired by the following property of line segments in R: (Ap, q, m E R: m E L(p, 4): L(P, q) = L(P, m ) U L(m, 4)). Similarly, we consider a raster line segment as a concatenation of two smaller segments, one with end points (0,O) and a well-chosen point m E Z*, the other one with end points m and (ZV, 6). Here, m is delined to have x-coordinate Ndiv 2 and y-coordinate 6 div2, thus approximating the m idpoint of the line segment connect- ing (0,O) and (iV, 8). See Fig. 2. Consequently, the function prescribing the y-values for the original segment will be expressed in terms of the functions prescribing the y-values for the two constituting segments. Since, at this point, only raster segments are considered with one end point in the origin and the other one at x = 2k, we have to translate the segment with end points m = (Ndiv2,S div 2) and (ZV, S) over the vector - m . In the following definition, L(n, 6) stands for the raster line segment with end points (0,O) and (2n, 8). DEFINITION (of L and Y). For n, 6 E N, with 6 _< 2*, the set L(n, S) c Z* is defined by ~(n, 8) := {(x,Y(n, 6; x))Ix E N A x zz 2}, where the integer function Y is defined by 0 ifx=O Y(n - 1, Sdiv2; x) if n > 0 A 0 < x 5 2- Y(n, 6; x) := Y(n - 1,s - 6 div2; x - 29 + sdiv2 if n > 0 A 2- < x < 2 6 if x = 2. As an example L(4,6) is shown in Fig. 3. By mathematical induction on n, the following properties of Y can be proven. Here, n, S, and x are considered to be elements of N, and 6 I 2, x I 2. 214 VANLIJZROP,VANOVJ2RVELD,ANDVANDEWETERINCi 0 4 8 12 16 FIG. 3. The line segment connecting (0,O) and (16,6), and its rasterization L(4,6). Properties. l for 6 = 0, the y-value of any point equals 0: Y(n,O; x) = 0 (2-l) l for S = 2, the y-value of any point equals its x-value: Y(n,2; x) = x (2.2) l Y is nondecreasing in x and increases with steps of at most 1: Y(n, 8; x) - Y(n, 8; x - 1) E {OJ}, where x > 0 (2.3) l Y is nondecreasing in 6 and increases with steps of at most 1: Y(nJ; x) - Y(n,S - 1; x) E {OJ}, where S > 0 (2.4) l the difference in y-coordinates of two subsequent points is nondecreasing in 6 and increases with steps at most 1: (Y(n, s; x) - Y(n, 8; x - 1)) -(Y(nJ - 1; x) - Y(n,6 - 1; x - 1)) E {O,l}~ where x > 0 A S > 0. (2.5) Property (2.5) is very strong: it implies that if the y-values for x and x - 1 differ for 6 = d, they differ for every S > d. The definition of L is easily extended to support raster line segments with end points (0, Q) and (2n, 8 f ry), by translating L(n, 6) vertically over ty pixels. DEFINITION (of REFLINE). For n, S E N, and ty E Z, where 6 I 2, the set REFLINE(n, 6, ty) C_ Z2 is defined by REFLINE(n, 6, q) = L(n, 6) CB (0, ty). Note that the above definition implies translation invariance in the y-direction. We shall now present some theorems for n E N and points p, q E Z2 with 0 I p.x I q.x I: N = 2 and 0 I q.y - p.y 5 q.x -p-x. The following theorem states that for any two points p and q a REFLINE exists that contaks both p and 4. SUBSET LINE PROPERTY 215 THEOREM 1. There exist numbers ty E Z and 6 E N, such that 6 I 2 and {p, q} c WFLIWn, 4 ty). Proof: For 6 E N, S I 2, we define the function h as h(S) = Y(n, 8; q-x) - Y(n, 6; p.x>. We shall show that the range of h equals [0 . . . q.x - p.x]: (a) h(0) = 0, because of property (2.1), (b) h(2) = 4.x - p.x, because of property (2.2), (c) h(6) - h(6 - 1) E (0, l} for 6 > 0, because of properties (2.4) and (2.5). Hence, the range of h equals [0 . . * q.x -p.x]. Since q.y -p.y I q.x -p.x, a d exists such that h(d) = q.y - p.y. With v defined as p.y - Y(n, d; p.x), it follows that Y(n, d; p.x) + ty = Y(n, d; p.x) +p.y - Y(n, d; P.X) = P.Y, Y(n, d; 4.x) + ty = Y(n, d; 4.x) + p.y - Y(n, d; P.X) = P-Y + h(d) = p.y + q.y - p.y = q.y. Hence, {p, q} c REFLINE(n, d, ty). 0 The following theorem states that for any two REFLINES that contain points p and q, the part between p and q is exactly the same. THEOREM 2. For all tyl, tyz E Z, and d,, d, E N, such that p and q are both elements of REFLINE(n, d,, vi) and REFLINE(n, d,, tyz), the following holak REFLINE(n,d,,ty,) f~ ([p.x 0.. q.x] X Z) = REFLINE(n, d,, ty*) Cl ([p.x *a* q.x] X Z). Proof. Let tyi, d,, tyz, and d, be as stated in the theorem. Notice that we only need to prove (Ax E N: p-x I x I q.x: Y(n, d,; x) + tyl = Y(n, d,; x) + tyz). For x E N, x Z+Z 2, we define the function f by f(x) = Y(n, d,; x) + tyl - (Y(n, d,; x) + 0~~). We shall show that f(x) = 0 for all x with p.x 5 x < 4.x. (a) f(p.x) = 0, because p E REFLINE(n, d,, zjQ (I REFLINE(n, d,, ty*). (b) f(q.x) = 0, because q E BEFLINE(n, d,, tyJ fl REFLINE(n, d,, &). (c) f(x) - f(x - 1) E (O,l}, f or x P- 0, because of property (2.5) and f(x) - f(x - 1) = (Y(n, d,; x) - Y(n, d,; x)) - (Y(n, d,; x - 1) - Y(n, d,; x - 1)). Because of (c), f is nondecreasing in x, and thus (Ax E N: p.x I x I q.x: f(x) = 0). 0 216 VAN LIEROP, VAN OVERVFXD, AND VAN DE WETERING 0 4 8 12 16 FIG. 4. REFLINE(4,6,0) (indicated by 0) and REFLINE(4,5,1) (indicated by 0): both contain p = (6,2) and q = (14,5); the parts between p and q are the same. As an illustration, in Fig. 4 two different REFLINES are shown that both contain p = (6,2) and q = (14,5): the part between p and q of both RFFLINES is the same. Theorems 1 and 2 give rise to the following definition of the raster line segment connecting points p and q. Informally, it is the part between p and q of a REFLINE containing both p and q. DEFINITION (of Line). The set Line(n, p, q) c Z2 is defined by Line(n,p,q) = REFLINE(n,a,fy) n ([p.x a.0 q.x] X Z), where t and 6 are such that {p, q} c REFLINE(n, 6, ty). In Fig. 5, Line(4, p, q) is shown for p = (6,2) and q = (14,5) (see also Fig. 4). With the above definition it is clear that the following theorem holds. THEOREM 3. Let A be an algorithm that generates Line(n, p, q) for any n E N, P, 4 E z2, where 0 I p.x -C q.x I 2 and 0 I q.y - p.y < q.x - p.x. Then A satisjies SPl, and A is translation invariant in the y-direction. An algorithm based on the definition of Line will consist of two parts: (1) the search for a S such that Y(n, 6; q.x) - Y(n, 6; p.x) = q.y - p.y, and (2) the generation of REFLINE(n, 6, ty) n ([ p.x . . . q.x] x Z), where ty = p.y - Y(n, 6; p.x). For given n, 6, and x, the calculation of Y( n, S; x) may be achieved by a recursive function with time complexity O(n), which equals O(210g N). This function is easily derived from the definition of Y. From the proof of Theorem 1 we know that Y(n, 6; 4.x) - Y(n, 6; p.x) 0 4 8 12 16 FIG. 5. Line(4, (6,2), (14,s)). SUBSET LINE PROPERTY 217 is nondecreasing in 6, and therefore a binary search in the range [0 . . . N] can be performed to find S such that Y(n, 6; 4.x) - Y(n, 6; p.x) = q.y - p.y. Hence, part 1 of the algorithm has time complexity O(( log N)2). We shall now concentrate on part 2. For the following two subsections we assume that we have found an appropriate 6, and that we have to generate REFLINE(n, 6, ty) n ([p.x -0. q.x] x Z), where ty = p.y - Y(n, 6; x). 2.1. A Recursive Algorithm To start with, we present an algorithm that generates REFLINE(n, 6, ty): function refine(n, 8, ty: integer): set of point; var x: integer; R: set of point; begIn x := 0; R := {(x, q)}; {P holds} do x Z 2 + x := x + 1; R := R U {(x,Y(n, 6; x) + ty)} {P holds} od; {P A x = 2 holds, hence R = REFLINE(n, 6, ty)) refine := R end where invariant P is defined by P: x E [O..-2] AR = {(i,Y(n,&; i) + ty)li E [O... xl}. Readers that are not familiar with the guarded command language and the notion of invariants, are referred to [4]. However, the above function has time complexity O(iV log N), since for each x the value Y(n, 6; x) has to be computed. By exploiting coherence and by making refrine recursive, we may improve the algorithm. For this purpose we recall the formerly expressed idea that a raster line segment is the concatenation of two smaller raster line segments. In fact, REFLINE( n + 1,6,~) = REFLINE(n, S div2, ty) U(REFLINE(n,G - Sdiv2, ty) 8 (2,Sdiv2)) (2.6) which indicates how the recursion may be introduced. In Fig. 6, a binary tree is shown, illustrating the recursive construction of REFLINE(3,S, q): each node is associated with a segment and is labeled with the x-interval of that segment. This tree corresponds with the segment tree T(O,8) as described in [5]. Equation (2.6) suggests using n, S, and ty, in addition with a translation vector, as parameters for the recursive function. However, we shall use n and the end points rl, r, of the segment to be generated, as parameters. Since we are interested in the part of REFLINE(n, S, q) between p and q only, we extend the parameter list of 218 VAN LIEROP, VAN OVERWD, AND VAN DE WETERING the recursive function with two parameters, px and qx, indicating this interval. The recursive function rline(k, px, qx, r,, r2) wilI generate the set (REFLINE(k, r,.y - r,.y, r,.y) CB (r,.x,O)) n ([px ... qx] x Z). The x-interval spanned by rI and r2 will always be associated with a node of the segment tree. Within the function rline, only three different configurations of px and qx with respect to m.x = ( rl.x + r2.x) div 2 may occur, as shown in Fig. 7. In situations (a) and (c), only one recursive call is needed; in situation (b), two calls result: function line(n, 6: integer; p, q: point): set of point; var ty: integer; rI, r2: pomt; besin ty := p.y - Y(n, 6; p.x); rl := (0, ty); r, := (2, ty + 6); line := rZine(n, p.x, q.x, r,, r2) end function rline(k, px, qx: integer; rl, r2: point): set of point; {precondition: k 2 0 A 0 I rl.x I px -c qx I r2.x I 2k A rl.x mod2k = 0 A r2.x - rl.x = 2k A 0 I r,.y - r,.y I 2k > vzu m: point; R: set of point; besin if k = 0 + R := { rI, r2 > flk>O-+ m := ( rI + r2)div 2; if qx < m.x --a R := rline(k - 1, px, qx, rl, m) UpxSUBSET LINE PROPERTY 219 [O.ll (I.21 L2.31 0.41 i4.51 b.61 16.71 [7.8] FIG. 6. The segment tree T(O,8). PX 41 P q P v FIG. 7. Three different configurations of px and qx with respect to m.x. FIG. 8. The nodes of the segment tree T(O,8) that are associated with the calls of function rline, resulting from the call rline(3,1,5, r,, r2). In general, a0 = qx - p x, and ak I a,-,div2 + 1 for 0 -C k I n. Since a,-#iv2 + 1 I $ak-i + 1, it can be seen that Igai I a, + ~~($aiWl + 1) = (a, + n) + f +clai I 2(a, + n). Hence the time complexity of dine is 0( qx - px) + O(n). (Recall that n = 210g N.) Since the search for 6 requires O((*log A)*) time, the generation of Line(n, p, q) by means of the above recursive function requires 0(( 210g N)*) + O(M), where A4 is the number of elements of Line(n, p, q). Finally, we would like to remark that parameter k may be omitted from the parameter list; the guards k = 0 and k > 0 should then be replaced by r2.x - r,.x = 1 and r2.x - rl.x > 1, respectively. 220 VAN LIEROP, VAN OVERVELD, AND VAN DE WETERING 2.2. A Nonrecursive Algorithm In this section we shall transform the recursive function of Section 2.1 into an iterative one, which still generates BEFLINE(n, 6, p.y - Y(n, 6; x)) n ([ p.x . . . q.x] x Z) = {(x,Y(n,G;x) +p.y-Y(n,6;p.x))(x~ [p.x -** 4.x1). For this purpose we shall use the following observations. From Property (2.3) we know that Y(n,igx) -Y(nJ;x- 1) E {OJ}. For given n E N and 6 E [0 . . .2], we shah say that a jump occurs at x if Y(n, 8; x) - Y(n, 6; x - 1) = 1. Now consider, for given n E N and x E [l . . * 2n], the set JMP( n; x) of all values of S for which a jump occurs at x. Because of Property 2.2, Y(n,2; x) - Y(n,2; x - 1) = x - (x - 1) = 1, and therefore 2 E JMP(n; x). Now suppose that 6 E JMP(n; x), hence, Y(n,S; x) - Y(n, 6; x - 1) = 1. From Property 2.5 it then follows that Y(n,6 + 1; x) - Y(n, 6 + 1; x - 1) E {1,2}. Hence, using Property (2.3), it follows that Y(n,6 + 1; x) - Y(n,6 + 1; x - 1) = 1, which implies that 6 E JMP(n; x) * 6 + 1 E JMP(n; x). (2.7) Now we define for n, x E N, where 0 < x < 2 = N, the function T: N X [l . . . N] --) [l ... N] by T(n; x) = min(JMP(n; x)). (2.8) Then it follows from (2.7) that JMP( n; x) = [T( n; x) * . . 2]. SUBSET LINE PROPERTY 221 Furthermore, for n E N and x E [l * . . 27, we may derive Y(n, 6; x) = Y(n, 6; x - 1) + Y(n, 6; x) - Y(n, 8; x - 1) = Y(n, s; x - 1) + 1 if T(n; x) I 6 0 if T(n; x) > 6. (2.9) Recursively applying (2.9) and using that Y(n, 8; 0) = 0, we come to the expression for Y(n, 6; x), Y(n,S;x)= #{i~[l... x]lT(n;i)~?}, (2.10) and hence Y(n, s; x) + p.y - Y(n, 6; p.x) =p.y + #{i ~[p.x + 10.. x]lT(n; i) I S}. If we are able to calculate T for a fixed n and store its values in an array ql . * . 2], then the set {(x,Y(n,G; x) +p.y - Y(n,6; p.x))lx E [p.x ... q.x]} may be generated using invariant P, which is defined by P: x E [p.x -0. q.x] my =p.y + #{i ~[p.x + 1 ..a x]lT(n; i) I S} AR = {(i,Y(nJ; i) +p.y - Y(n,S; p.x))li E [p.x 0.. x]}: function line(b: integer; p, q: point): set of point; varx, y: integer; R: set of point; begin x := p.x; y := p.y; R := {p}; {P holds} do x # q.x + x := x + 1; if TX] 5 S + y := y + 1 fi; R := R u {(x, y)} {P holds} 04 {P A x = 4.x holds, hence R = REFLINE!(n, 6, p.y - Y(n, 8; x)) fl ([p.x . . . q.x] X Z) 1 line := R end The above function is linear in the number of generated pixels, just as we would like it to be, and of storage complexity O(2) = O(N). The only problem left is the computation of T for hxed n. Since this a preprocessing step, its complexity m ight be considered irrelevant. A naive way to compute T is to generate all REFLINES, keeping track of the places where the new jumps occur when S is increased with 1. This requires O(N*) time. However, we shall give an expression for T such that the array may be calculated in O(N) time. 222 VAN LIEROP, VAN OVJZRVELD, AND VAN DE WETERING LEMMA. (a) T(0; 1) = 1 (b) T(n + 1; x) = 2T( n; x) ifOSUBSET LINE PROPERTY 223 DEFINITION (of C). The class C consists of rasterization algorithms A with the following property: A rasterization function R exists such that for all p and q in Q2 with p.x < q.x A(p, q) = A(q,p) = RS(R, L, P.X, q.x) where L is the line through p and q. A subclass of algorithms from C can be selected by specifying R, e.g., (1) R(L) = 0, for all lines L. (2) R(L) = Z*, for all lines L. (3) R(L) = L n Z*, for all lines L. None of these examples would result in a subclass with satisfactory line algorithms but all of them result in algorithms satisfying SP2 as is proved by the following theorem. WORBM. Any algorithm A E C satisfies SP2. Proof: Let A E C with rasterization function R. Let p, q E Q* with p.x < q.x and let L be the line through p and q. Let r, s E Q* rl LS(L, p.x, 4.x) with r.x -c S.X. Hence, p.x zs r.x < s.x I q.x and A(r, s) = RS(R, L, r.x, s.x) c RS(R, L, p.x, 4.x) = A(p, q). q A definition of R which results in a class of algorithms that have deviation of at most $, is DEFINITION (of R). For a line L, R(L) is the smallest set in Z* satisfying (AZ E L: 1.x E Z: (Er E R(L): r.x = 1.x: -$ < 1.y - r.y I $)). The reader can check that an equivalent definition of R is R(L) := ((1.x, [I.y + il)li.x E Z A 1 E L], for all lines L. In the Subsections 3.1 and 3.2 we present algorithms that, for a given line segment LS(L, px, qx), generate the set RS(R, L, px, qx). These algorithms are based on the following recipe. Compute for every I E LS(L, px, qx) with 1.x E Z, an r E RS(R, L, px, qx) with r.x = I.xandcallectthesersinaset R. The algorithms differ in the order the ls are treated and, hence, the rs are generated. In 3.1 the Zs are treated from left to right. In 3.2 they are treated in the recursive manner as introduced in Subsection 2.1. That is, given two points I, and I,, both in L, the next 1 to be treated is (I, + 1,)/2. 3.1. A Nonrecursive Algorithm In this subsection we give a nonrecursive algorithm that, for given (a, b, c) E Z3, with 0 s -a 5 b, and px and qx in Z with px < qx, generates the set 224 VAN LIEROP, VAN OVERVELD, AND VAN DE WETERING RS(R, L, px, qx) with L = LJa, b, c). For the loop in the algorithm we use the following invariant. P: R = RS(R, L( a, b, c), px, r.x) Ar E Z2 A px I r.x < qx AI E L(a, b,c) A 1.x = r.x A - $ < 1.y - r.y I i. We use the types rational and qpoint for an element of Q and Q2, respectively: function line(u, b, c: integer; px, qx: rational): set of point; {precondition: px -C qx A 0 I - u I b } var r: point; 1: qpoint; begin px := [px]; qx := [qx J; {(see note 0)) r.x := px; r.y := (-a * r.x - c)div b; 1.x := px; 1.y := (-a * 1.x - c)/b; (0 5 1.y - r.y < 1 (see note 1)) if 1.y - r.y > i--f r.y := r.y + 1 fi; R := {r}; {P} do r.x < qx + r.x := r.x + 1; 1.x := 1.x + 1; 1.y := 1.y - u/b; { - $ -c 1.y - r.y I $ (see note 2)) if 1.y - r.y > i + r.y := r.y + 1 fi; R := R U {r} {P} od; {P A r.x = qx} line := R end {postcondition: line = RS(R, L(u, b, c), px, qx)} with note 0: From the definition of RS follows that RS(R, L, px, qx) = RS(R, L, 1 pxl, lqxl), for d px, qx E R. note 1: Let h = -a * r.x - c then 1.y = h/b, r.y = h div b, and 1.y - r.y = h/b - (h/b - (h mod b)/b) = (h mod b)/b E [0, l), according to the definitions of div and mod. note 2: At the beginning of the loop we can account for - $ < 1.y - r.y I : because of P. From this and from 0 5 -a 5 b, we can deduce that - $ < (1.~ - u/b) - r.y I $. In the Introduction we promised to give algorithms which use integer arithmetic only. We can obtain this by transforming the function line. This transformation consists of the following two steps. (0) Add a new integer variable e and keep e = 2 * b * (1.~ - r.y) invariant by adjusting e after every adjustment of 1.y or r.y. Replace tests on 1.y - r.y by tests on e. (1) Leave out all the statements referring to 1. SUBSET LINE PROPERTY 225 This transformation results in the following program: function line( a, b, c: integer; px, qx: rational): set of point; var r: point; e: integer; begin px := [px]; qx := lqxJ; r.x := px; r.y := (-a * r.x - c)div b; e:= 2*((-a*r.x - c) - b*r.y); if e > b + r.y := r.y + 1; e := e - 2* b fi; R := {r}; do r.x < qx --) r.x := r.x + 1; e:=e-2*a- if e > b + r.; := r.y + 1; e := e - 2* b fi; R := R U {r} od; line := R end The above algorithm is very similar to the line generating algorithm of Bresenham. In fact, the only fundamental difference is the initialization of the error term e. This difference is due to the fact that Bresenham considers line segments between points in 2 whereas we consider line segments between points in Q2. The time complexity of this algorithm is O(M) with M = qx - px. 3.2. A Recursive Algorithm In this subsection we present a recursive algorithm that, for given (a, b, c) E Z3, with 0 I -a I b, and px and qx with 0 -< px -C qx I 2, generates the set RS(R, L, px, qx) with L = L(a, b, c). We use the recursive subdivision of the screen as introduced in 2.1. We can make use of this subdivision because RS(R, L, PX, 4x1 = RS(R, L, px, mx) U RS(R, L, mx, qx) for all mx E [px, qx]. Below we give two functions, calkd line and dine. The function line does some preprocessing and the first call to the recursive function rline: function line(n, a, b, c: integer; px, qx: rational): set of point; {precondition: 0 5 px -C qx I 2 A 0 I - a I b } var rI, r2: point; I,, 1,: qpoint; begin rl.x := 0; r,.y := (-a * r,.x - c)div b; 1,.x := 0; l,.y := (-a * 1,.x - c)/b; (0 I l,.y - r,.y < 1 (see note 1)) if l,.y - r,.y > $+ r,.y := r,.y + 1 fi; rz.x := 2; r2.y := (-a * r2.x - c)div b; 1,.x := 2; l,.y := (-a * 1,.x - c)/b; 226 VAN LIEROP, VAN OVERmD, AND VAN DE WETERING (0 I l,.y - r,.y < 1 (see note 1)) if l,.y - r*.y > 5 + r*.y := r*.y + 1 fi; {precondition of rline holds} line := rline(n, rl, r2, I,, I,, [px], [qx J) end {postcondition: line = RS(R, L(a, b, c), px, qx)} functionrline(k: integer; rI, r2: point; I,, 1,: qpoint; px, qx: integer): setofpoint; {precondition: 0 I rl.x I px < qx I r2.x I 2 A rl.x mod2k = 0 A r2.x - rl.x = 2k A 0 5 r,.y - r,.y 5 2k Al,ELAl,EL A 1,.x = rl.x A - t-c l,.y - r,.y I : A 1,.x = r2.x A - $ < l,.y - r,.y I + var m: point; 1: point; R: set of point; begin if k = 0 + R := { rl, r, } Ok %O + m := (rI + r2)div 2; I := (1, + l,)/2; { - $ c l.y - m.y 5 1 (see note 3)) if l.y - m.y > $ + m.y := m-y + 1 fi; if qx I m.x -+ R := rline(k - 1, rl, m, l,, I, px, qx) 0 px < m.x < qx-+ R := rline(k - 1, rI, m, I,, 1, px, m.x) U 0 rline(k - 1, m, r2, 1, I,, m.x, qx) m.x I px + R := rline(k - 1, m, r2, 1, l,, px, qx) fi fi; rline := R end {postcondition: rline = RS(R, L, px, qx)} with note 3: This can be deduced from - l.y - m.y = (l,.y - rl.y)/2 + (l,.y - r,.y)/2 + Wd + r2.YWW/2 -- i < l,.y - rI.y I + A - f < l,.y - r..y < 4 - (r,.y + r,.y)mod2 E (0, l} Again we use a transformation to get functions which use integer arithmetic only. The transformation of rline consists of the following steps. (0) Add the integers e, and e2 to the parameter list of rline and add el = 2*b*(ll.y - rl.y) A e2 = 2*b*(12.y - r2.y) to the precondition of rline. SUBSET LINE PROPERTY 227 (1) Add a new integer variable e and keep e = 2 * b * (1.~ - m .y ) invariant by adjusting e after every adjustment of 1.y or m .y. After the statements m := ( rI + r,)div2; I := (I1 + l&2; e must be adjusted by e := (e, + e,)/2 + b*((r,.y + r,.y)mod2) as can be seen by the following argument, using note 1: e = 2*b*(l.y - m.y) = eJ2 + e,/2 + b*((r,.y + r,.y)mod2). Notice that et/2 = b*(lI.y - rI.y) E Z because I, E L(a, b, c) and hence b * I,.y = - a * I,.x - c E Z. Similarly, e,/2 E Z and hence the value of the ex- pression assigned to e is an integer. Replace the tests on I.y - m.y by tests on e. (2) Leave out all references to I,, l,, and 1. The transformation of line is similar but even simpler. These transformations result in the following program: function line(n, a, b, c: integer; px, qx: rational): set of point; var rl, r2: point; e,, e,: integer; b&n rl.x := 0; r,.y := (-a * rl.x - c)div b; e1 := 2*((-a * rl.x - c) - b * rI.x)); ife,>b-+r,.y:=r,.y+l; e,:=e,-2*bfi; r2.x := 0; r,.y := (-a * r2.x - c)div b; e2 := 2*((-a * r2.x - c) - b * r2.x)); if e2 > b + r,.y := r,.y + 1; e2 := e2 - 2* b fi; line := rline(n, r,, r2, e,, e,, 1~x1, 14x1) end function rline(k: integer; rI, r,: point; e,, e,, px, qx: integer): set of point; var m: point; e: integer; R: set of point; begin if k = 0 + R := {rI, r2} nk>O-, m := (rl + r,)div2; e := (el + e,)/2; if (rI.y + r,.y)mod2 = 1 + e := e + b fi; if e > b + m.y := m.y + 1; e := e - 2* b fi; if qx I m.x -a R := rline(k - 1, rl, m, e,, e, px, qx) 0 px < m.x < qx + R := rline(k - 1, rl, m, el, e, px, m.x) U cl rline( k - 1, m, r2, e, e2, m.x, qx) m.x 5 px + R := rline(k - 1, m, r2, e, e2, px, qx) fi 6; rline := R end 228 VAN LIEROP, VAN OVERVELD, AND VAN DE WETERING The time complexity of this program is O(M) + 0( 210g N), where M = px - qx and N = 2 is the screen size. 4. CONCLUDING REMARKS First we will discuss the accuracy in representing line segments of the algorithms presented in Sections 2 and 3. For the algorithms of Section 2, satisfying property SPl, the possible accumulation of rounding errors may cause a rather large deviation. The following lemma gives an upper bound for the deviation of the algorithms. LEMMA. For all n, 6 E N, where S I 2, Ax E N: x I 2: IYO(n,b) -a*;/ I ;(n+ 5)). This means that for n = 10, for example, the deviation is at most 34 pixels. Compared to Bresenham lines, whose deviation is at most 5, this seems quite bad. From the derivation of the algorithms satisfying the weaker property SP2, it is obvious that the deviation of these algorithms is at most $. We will now compare the time complexities of the various algorithms. The recursive algorithm satisfying SPl is of time complexity 0(( 210g N)2) + O(M), where N is the screen size and M is the number of pixels of the raster&d segment. Its nonrecursive equivalent is O(M), but requires preprocessing time and storage of O(N). The nonrecursive algorithm of Section 3.1, satisfying SP2, is O(M), and its recursive equivalent 0( 210g N) + O(M). Remark. In Section 2.2 we have introduced the function T, which depends on the previously introduced function YO, which in turn is used to derive an algorithm that satisfies property SPl. However, it is also possible to work the other way around: given a function T, define a function YO using expression (2.8), and then derive an algorithm in the same way as is done in Section 2.1. In [6] it is proven that the algorithm thus derived, satisfies SPl if and only if T is a bijective function. Hence, all permutations induce an algorithm satisfying SPl. Probably most of those algorithms generate pixel sets that do not quite resemble straight lines. What permutations induce good line algorithms, i.e., with a small deviation, will be the subject of further research. REFERENCES 1. J. E. Bresenham, Algorithm for computer control of digital plotters, IBM Systems J. 4, No. 1, 1965, 25-30. 2. W. R. Franklin, Problems with raster graphics algorithms, in Data Structures for Raster Graphic.?; Proceedings of a Workshop (F. J. Peters, L. R. A. Kessener, and M. L. P. van Lierop, Eds.), pp. 1-7, Springer-Verlag, Berlin/New York, 1986. 3. J. D. Foley and A. Van Dam, Fundamentals of Interactive Computer Graphics, Addison-Wesley, Reading, MA, 1982. 4. E. W. Dijkstra, A Discipline of Programming, Prentice-Hall, Englewood Cliffs, NJ, 1976. 5. F. P. Preparata and M. I. Shamos, Computational Geometry: An Introduction, Springer-Verlag, New York/Berlin, 1985. 6. M. L. P. van Lierop, Digitisation functions in computer graphics, Ph.D. Thesis, Eindhoven: University of Technology, 1987.

Recommended

View more >