Drawing Line in 2 Dimenion Drawing a traight line (or an arc) between two end point when one i limited to dicrete pixel require a bit of thought. Conider the following line uperimpoed on a 2 dimenional 7x7 pixel grid The pixel are indexed with the uual creen convention of x running from left to right and y running from top to bottom. Which pixel hould be et in order to repreent the line? If we increment our way vertically downward through the y value, etting the pixel in the appropriate x column a we go, we get The pixellated line ha gap in it. If, on the other hand, we increment our way horizontally acro through the x value, etting the pixel in the appropriate y can-line a we go, we get Thi i clearly a much more atifactory olution. We note, however, that had the line been longer in y than in x then the firt olution would have delivered the better reult. The magnitude of the lope of the line i the ey here. If the x range exceed the y range then the magnitude of the lope i le than 1 and we hould increment our way through x (and vice vera). - 1 -
In fact, there are 4 cae to conider when electing which pixel hould be ued to contruct a continuou line 1. Poitive lope le than 1 2. Poitive lope greater than or equal to 1 3. Negative lope le than or equal to -1 4. Negative lope greater than -1 If we now the end point of the line to be drawn we can alway calculate the lope y e y m = x e x A poitive lope (in creen co-ordinate) mean that the line i monotonic increaing (a x increae o doe y) whilt a negative lope indicate the oppoite (a x increae y decreae). The intercept, where the line meet (or would meet if long enough) the y axi i alo readily obtained c= y m x Of coure, thi relationhip hold for all pair of x and y value on the line o we can determine a y value for any x value and vice vera or, y = c + m x x = = y c y c m m m Now, if we conider only change in x and y we don t need to worry about the intercept except when determining the firt pixel to be plotted and, y = m x x = y m We hall conider two algorithm for drawing a 2D line on a diplay creen baed on the above principle; the Digital Differential Analyer and Breenham algorithm. - 2 -
The Digital Differential Analyer (DDA) Algorithm The DDA i a can-converion algorithm which ample a line at unit interval along one axi and determine integer value nearet the line for the other axi. Conider the line we have been looing at. The DDA will ample along the x axi in thi cae determining y value a it goe y y m + 1 = + If our line had a lope whoe magnitude wa greater than 1 (45º) then the DDA would need to ample along the y axi determining x value uing x = + 1 x + 1 m Thee equation will wor whether the lope i poitive or negative. When the lope i negative the x and y value will vary inverely but m will have the oppoite ign o all will be well Note that m i not necearily an integer, o the DDA require floating point arithmetic and rounding of the reult. Thi will low up the drawing proce even if implemented in hardware. - 3 -
Breenham Algorithm Breenham algorithm can-convert line more efficiently than the DDA by uing only integer arithmetic. It can alo be adapted to diplay curve. Conider, once again, a line with poitive lope le than 1 o we ample along x determining which y to plot. If we have jut plotted the pixel (x, y ) we now need to decide whether to plot (x +1, y ) or (x +1, y +1 ) a the next pixel on the line. Note that x +1 = x +1 and y +1 = y +1. Let the vertical diparitie of the pixel poition from the actual mathematical line be given by d 1 and d 2. Then, and, ( ) d y y m x c y 1 = = + 1 + ( ) ( ) d2 = y + 1 y = y + 1 m x + 1 c Conider now the difference between thee two value ( ) d1 d2 = 2m x + 1 2y + 2c 1 If we let the difference between the x and y co-ordinate of the end point of the line be given by and, x = x x e y = y y e and if we init that thee endpoint co-ordinate are integer (which they will be if they are pecified a pixel) then we can write the lope of the line a a ratio of two integer m y = x If we now ubtitute thi ratio for m above we get and o, y d1 d 2 = 2 ( x + 1) 2 y + 2c 1 x - 4 -
where the term in quare bracet i contant and independent of the pixel poition. We hall call thi term b from now on. If we now define a deciion parameter a follow Then when p i poitive we hould chooe (x +1, y +1) a the pixel to plot and when it i negative we hould chooe (x +1, y ). Everything apart from the contant, b, in thi deciion can be calculated uing integer arithmetic o if we cam eliminate the need for b we have a method for plotting line uing integer arithmetic only. Conider the difference ( ) ( ) x d1 d2 = 2 y x 2 x y + 2 y + x 2c 1 ( ) p = x d d 1 2 = 2 y x 2 x y + b We have already noted that x +1 = x +1 o we can re-write thi a and where (y +1 -y ) i either 0 or 1 depending on the ign of p. So we have a recurrence equation which involve only integer calculation and all that i left i to determine a tarting value p 0 for it. Uing the tarting point of the line (x, y ) we can determine the intercept ( ) ( ) p p = 2 y x x 2 x y y + 1 + 1 + 1 ( ) p = p + 2 y 2 x y y + 1 + 1 y c = y mx = y x x and ubtituting thi into the equation for p we obtain p0 = 2 y x ============== - 5 -
Aliaing and Anti-aliaing When we produce line with Breenham algorithm, whilt we might be quite impreed with the peed at which we can generate them, we will not be overly enamoured with the way they loo. In general they will appear jagged, tepped, rateried. Thi effect i nown a aliaing, a term derived from ampling ignal at dicrete point in time and thu loing any information conveyed between thoe dicrete time. Our mathematically ideal line decribe a function whoe range i continuou but which our pixellated rater creen require to become dicrete. In effect we are ampling at dicrete point along the line and plotting the mot appropriate pixel for each ample. We have no choice of coure; the creen reolution contrain our ampling rate o aliaing i inevitable. Furthermore, our ideal mathematical line ha zero thicne but the line we wih to diplay mut have a thicne to be een. Indeed, we mae a virtue of thi requirement and put line of varying thicne to good ue. In computer graphic a traight line i, in reality, a thin rectangle and we mut treat it a uch. Conider the following line uperimpoed on a 2 dimenional 7x7 pixel grid Thi line ha a very clear thicne and i quite obviouly a filled rectangle. If we pixellate it, a we did with our mathematically ideal line previouly, we get The pixellated line diplay clear evidence of aliaing. Can we mae it more aethetically appealing? Of coure, the anwer i ye. We can anti-alia it. Anti-aliaing algorithm wor by partially illuminating pixel which are adjacent to thoe plotted by the line drawing algorithm (e.g. the DDA or Breenham algorithm). Thi mean that anti-aliaed line will be wider than wa probably intended. The two mot common method for anti-aliaing are filtering and uperampling. - 6 -
Filtering Thi technique i baed on the concept that each pixel ha an area of influence around it. If our rectangular-haped line overlap the area of influence of a pixel then that pixel hould be illuminated by an amount proportional to the degree of overlap. Thi amount i determined by a filter function. One popular filtering cheme i baed on circular area of influence which are one pixel in radiu. I.e. the area of influence of each pixel extend a far a the centre of it four nearet neighbour The filter function i defined to be a cone with it bae being the area of influence and it apex lying directly above the pixel centre, at a height which produce an overall volume for the cone of unity. The interection of a pixel area of influence with the rectangular line i then determined and the volume of the cone directly above that interection yield the amount of illumination to be applied to that pixel. Clearly the amount of illumination applied to any pixel will range from 0 (pixel area of influence totally outwith the line) up to 1 (pixel totally within the line). Thi i clearly a computationally expenive proce. In order to eep the computation time down it i normal to pre-calculate table of value for the filter function. The downide to thi i that the reulting algorithm are not very flexible the filter function, area of influence and palette ize are all fixed in a given table and any change to thee value will require a new table to be produced. - 7 -
Superampling Another technique for anti-aliaing ue the concept of ub-pixel. Each pixel i divided into, ay nine, ub-pixel We then count the number of ub-pixel that the line overlap. We need a reference point within each ub-pixel to ue in thi counting proce. It doen t matter where it i a long a we ue it conitently for all ub-pixel. A corner, uch a the top-left, i a popular choice. Centre would produce a more aethetically pleaing reult but at the cot of computational efficiency. The proportion of ub-pixel which the line overlap i then ued to determine the illumination, in dicrete tep from 0 through to 1. Clearly the larger the number of ub-pixel which the pixel i divided into, the finer the reolution that can be accomplihed in the illumination. Once again we have to trie a balance between aethetic and computation time. Thi will be determined by the need of the particular application. - 8 -