Live data from Hacker News

The Beauty of Bresenham's Algorithm

free.pages.at

11–20 of 50 posts

Re: The Beauty of Bresenham's Algorithm

#11
post #7
post #5

Why 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".

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

#12
post #10
post #7

Earlier 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".

FWIW, GCC and Perl generate the same code for both. http://stackoverflow.com/questions/885908/while-1-vs-for-is-...

Interesting. Looks like they'd be the same performance wise, since

for (;;);

is the same as

{ while (true) { ; ; } }

I originally asked this question cos it's used this way in first C program in the link. Bit of a tangent though :P

Re: The Beauty of Bresenham's Algorithm

#13
post #5

Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?

Why do people write while(1) instead of for(;;)? Are you saying that "while(1)" is superior in some way? Why?

I prefer "for(;;)" because it is explicitly defined as an infinite loop, but "while(1)" needs an implicit cast of "1" to "true", and even then there's an implicit test for "true".

Re: The Beauty of Bresenham's Algorithm

#14
post #13
post #5

Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?

Why do people write while(1) instead of for(;;)? Are you saying that "while(1)" is superior in some way? Why? I prefer "for(;;)" because it is explicitly defined as an infinite loop, but "while(1)" needs an implicit cast of "1" to "true", and even then there's an implicit test for "true".

You could just say while(true). And I'm not sure that the test for true is implicit, since that is what while() explicitly does.

Re: The Beauty of Bresenham's Algorithm

#15
post #4

Bresenham's Algorithm is a useful argument when someone suggests that software patents are a good thing. How far back would the progress of computer graphics have been set if Bresenham or his employer had claimed ownership of this idea?

I think you do Bresenham a great disservice, by underestimating how far ahead of its time this really was. This was published in 1962, so any patent would have expired in 1982. So any patent would probably have had almost no effect, because it would have expired before the market advanced to the point where it was generally applicable.

[deleted]

Re: The Beauty of Bresenham's Algorithm

#16
post #13
post #5

Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?

Why do people write while(1) instead of for(;;)? Are you saying that "while(1)" is superior in some way? Why? I prefer "for(;;)" because it is explicitly defined as an infinite loop, but "while(1)" needs an implicit cast of "1" to "true", and even then there's an implicit test for "true".

I guess it's just personal preference. "for(;;)" looks really messy to me, and doesn't seem obvious that it signifies an infinite loop. I think that even the standard format for for loops (for (initialisation; condition; thing done at end of loop)) is quite unintuitive, and I have to think a bit to remember what each part does if one of them is omitted. It's also the only place I can think of where semicolons aren't expected at the end of a line (conventionally). I don't find a problem implicitly casting "1" to "true", I sort of do this automatically after programming in C for a while. Obviously if I were using java or something, I'd use "while(true)". Basically, I don't have to think much to know that it's an infinite loop :)

Re: The Beauty of Bresenham's Algorithm

#17
post #5

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)

Re: The Beauty of Bresenham's Algorithm

#18
post #13
post #5

Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?

Why do people write while(1) instead of for(;;)? Are you saying that "while(1)" is superior in some way? Why? I prefer "for(;;)" because it is explicitly defined as an infinite loop, but "while(1)" needs an implicit cast of "1" to "true", and even then there's an implicit test for "true".

True and false are actually defined as 1 and 0 respectively. I don't have a copy of the standard but this specifies true and false as macros.

http://pubs.opengroup.org/onlinepubs/007904975/basedefs/stdb...

Edit: Of course I'm talking about C here. Also, I prefer for(;;) because it's an easy to recognize idiom that means "infinite loop."

Re: The Beauty of Bresenham's Algorithm

#20

By the way, I had a teacher (J.P Reveilès) who had invented the theoretically fastest drawline algorithm ever: he simply figure out that, for a line, there is a pixel pattern which repeat itself (unless the variation is a transcendental number http://en.wikipedia.org/wiki/Transcendental_number ) so you only have to compute that pattern once and copy/paste it. If I remember well, for example, a line which has a variat…

Wouldn't the exception be for lines whose slope is irrational? (i.e. that can't be represented by a ratio of integers), so the repeating pattern is analogous to the repeating pattern in a decimal representation of a rational number.
Post reply on HN