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.
|Published (Last):||26 February 2012|
|PDF File Size:||10.37 Mb|
|ePub File Size:||6.51 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.
Midpoint circle algorithm
So during the while loop, y increments by 1 each iteration, and x decrements by 1 on occasion, never exceeding 1 in one iteration. The second part of the problem, the determinant, is far trickier. This determines when to decrement x. It usually comes after drawing the pixels in each iteration, because it never goes below the radius on the first pixel. Because in a continuous function, the function for a sphere is the function for a circle with the radius dependent on z or whatever the third variable is , it stands to reason that the algorithm for a discrete voxel sphere would also rely on this Midpoint circle algorithm. But when looking at a sphere, the integer radius of some adjacent circles is the same, but it is not expected to have the same exact circle adjacent to itself in the same hemisphere.
Bresenham's Line and Circle Algorithms
Bresenham’s circle drawing algorithm
Bresenham's Line Algorithm