Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?
According to the C99 standard [0], an omitted optional expression-2 in a for loop is replaced by a non-zero constant. Thus, for(;;) is equivalent to for(;1;) which is effectively equivalent to while(1) as there are no other declarations or expressions. And, for those who are counting, it also saves a character. [0] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf (See 6.8.5.3)
The Beauty of Bresenham's Algorithm
31–40 of 50 posts
Re: The Beauty of Bresenham's Algorithm
#32Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?
Re: The Beauty of Bresenham's Algorithm
#33Earlier quoted context omitted.
How does a computer distinguish an irrational number from a good, (necessarily rational) floating-point approximation of one?
The algorithm only works with points with integer coordinates, so the slope is always rational.
just:
{ x++ and y+=slope } or { y++ and x+= 1./slope }
Then slopes are always rational in line drawing algorithms.
Re: The Beauty of Bresenham's Algorithm
#34Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?
Because while(1) still has a condition that is checked on every loop iteration when optimizations are disabled. for(;;) can be read as "forever".
Re: The Beauty of Bresenham's Algorithm
#35Earlier quoted context omitted.
According to the C99 standard [0], an omitted optional expression-2 in a for loop is replaced by a non-zero constant. Thus, for(;;) is equivalent to for(;1;) which is effectively equivalent to while(1) as there are no other declarations or expressions. And, for those who are counting, it also saves a character. [0] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf (See 6.8.5.3)
Why would anyone wreck legibility to save a character?
Re: The Beauty of Bresenham's Algorithm
#36About a week ago I came to know Bresenham's Algorithm is also important in mobile robotics. Basically, you model your map using a grid, after firing a sensor such as a LIDAR or sonar and finding something blocks its cone of sight you use Bresenham's to see what cells in your map the new reading provides information about. (i.e. something bouncing 2 meters from where your robot is not only tells you about a block at 2…
Re: The Beauty of Bresenham's Algorithm
#37it's very funny how this sort of stuff comes back at opportune times to magically solve really hard problems for you with a single pass. Sort of like having Knuth on your bookshelf.
Re: The Beauty of Bresenham's Algorithm
#38Earlier quoted context omitted.
According to the C99 standard [0], an omitted optional expression-2 in a for loop is replaced by a non-zero constant. Thus, for(;;) is equivalent to for(;1;) which is effectively equivalent to while(1) as there are no other declarations or expressions. And, for those who are counting, it also saves a character. [0] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf (See 6.8.5.3)
Why would anyone wreck legibility to save a character?
Re: The Beauty of Bresenham's Algorithm
#39Earlier quoted context omitted.
Because while(1) still has a condition that is checked on every loop iteration when optimizations are disabled. for(;;) can be read as "forever".
I do doubt that even for a for(;;), a jmp (x86) is use instead of a conditional jump like jz.
Re: The Beauty of Bresenham's Algorithm
#40About a week ago I came to know Bresenham's Algorithm is also important in mobile robotics. Basically, you model your map using a grid, after firing a sensor such as a LIDAR or sonar and finding something blocks its cone of sight you use Bresenham's to see what cells in your map the new reading provides information about. (i.e. something bouncing 2 meters from where your robot is not only tells you about a block at 2…
Could you please elaborate on how this works or point to some resources on this application of the algorithm (if you know of any)?
This is done because a LIDAR/sonar/whatever scanning range sensor returns the angle and range to a reflective object. In most cases, there's an implicit additional piece of information - namely that there's nothing in between the sensor and the object, since the EM radiation was able to get there and back. Bresenham's algorithm is used to tell you the grid cells in which you can assume free space.