How was the decision variable in Bresenham Line Algorithm figured out?
Every articles I studied about Bresenham Line Algorithm, they talk about a decision variable
Pi = dx * (d1 - d2)
Where does the idea of this mathematical term
come from?
I mean, what was the idea behind taking dx * (d1 - d2)
as decision variable?
Why was not 开发者_开发百科d1 - d2
taken only?
Here is one of the articles.
Because the only information we care about in d1-d2
is the sign, we can multiply it by any positive value and not lose anything. We dont actually know the value of d1
and d2
immediately durning the algorithm, and have to calculate their values from things we do know. However, this calculation involves dividing by dX
, and division is slow and to be avoided if possible. As it happens, dX
is always positive, so we can just multiply both d1
and d2
by dX
to remove the division operation without losing the information we were looking for.
Bresenham's algorithm works by focusing on a single portion of a circle cut into 45 degree sections and solves only one of those. The circle is formed by considering all lines that could be drawn from a point x1,y1 (i.e. the first parameter passed in). However, since it's focused on just a single section (i.e. say x rising faster than y in both positive direction) in order to generalize the solution to all lines that could be formed it has to convert all other sections of that circle transformed into the one way it knows how to draw the circle. Normally you'll see some initialization code that the top of the algorithm that makes sure x1,y1 < x2,y2 and swaps them if this doesn't hold. This effectively cutting the circle in half in terms of the number of lines it has to handle. So now only 4 different slopes has to be handled by the next part of the code. The decision variable is an optimization to figure out which of the 4 sections the algorithm falls into. We're always going in the positive X direction, but the question is are we going up/down faster than X (sections 1 & 4 -starting clockwise position) or is X moving faster than Y (sections 2 & 3).
The decision variable keeps us from having to do the if statement on each iteration of the loop.
精彩评论