Here we perform calculations at each step using the results from the preceding step. The characteristic of the DDA algorithm is to take unit steps along one coordinate and compute the corresponding values along the other coordinate. The unit steps are always along the coordinate of greatest change, e. Suppose at step i we have calculated xi, yi to be a point on the line.
|Published (Last):||26 August 2008|
|PDF File Size:||18.65 Mb|
|ePub File Size:||17.38 Mb|
|Price:||Free* [*Free Regsitration Required]|
Multiplication by 2 can be implemented by left-shift. This version limited to slopes in the first octant,. This is an all-integer function, employs left shift for multiplication and eliminates redundant operations by tricky use of the eps variable. A real implementation should do this. As expected, it fails to plot lines with negative slopes try it and see what happens. It also fails to plot lines of positive slope greater than 1 this is an interesting case, try it also and see if you can explain what is happening.
More unusually, we find that the order in which the endpoints are supplied to this routine is significant, it will only work as long as x1 is smaller than x2.
In fact, if we have two line segments with the same endpoints, and the same slope, this routine may draw one of them successfully but fails to draw the other one. Of course, this is not surprising really, when we consider that the function works by incrementing x.
It does emphasise, however, that the routine is plotting vectors, direction is significant. A full implementation of the Bresenham algorithm must, of course, be able to handle all combinations of slope and endpoint order. Some of the regions in the plane, those for which x2 is smaller than x1 can be handled by exchanging the endpoints of the line segment.
It is also clear that we will need a piece of code to handle large slopes by stepping over y instead of x values. However, careful consideration of the diagram will reveal that there is one case which cannot be reduced to a version of the algorithm we have already looked at.
If we want to draw a line having a small negative slope, we will have to consider a modification of the basic Bresenham algorithm to do this. The same point applies to lines of large negative slope as well, but the code for small negative slopes may be adapted to this case by stepping over y instead of x.
Consider a line with negative slope between 0 and 1 i. As usual there will be an error, , associated with y. Notice that the error generated by the above is negative. A little manipulation gives a decision inequality: It is worth comparing this with the decision inequality for the case of positive slope.
The error update rules are also subtly different for this case of negative slope. It is, however, a trivial matter to convert the algorithm into an integer-only form.
Bresenham's line algorithm
Bresenham’s Line Drawing Algorithm in C and C++
Circle Generation Algorithm