Live data from Hacker News

Island Generator

exupero.org

31–33 of 33 posts

Re: Island Generator

#31
post #24
post #21

Earlier quoted context omitted.

It wouldn't have been a problem if it instead used an index variable i ranging from 0 to the number of sides, computing 2 pi i/sides along the way. If you're going to rely on repeated addition of irrational numbers to come out to some exact value, then you've misunderstood the abilities of finite decimal representations. It is something easy to overlook, but shouldn't be too surprising to someone who has computed any…

I fully agree. As a more general strategy, just use integers (preferably unsigned integers) for all critical stuff such as state handling and looping over. Then, perform floating point operations only on top of that, as a last step. Sometime you don't even need that and use fixed point arithmetics instead (i.e. change your unit to some fixed fraction, e.g. in accounting calculate with integer cents instead of floatin…

> Then, perform floating point operations only on top of that, as a last step.

Right. "Once you go float, you never go back."

Any time you need discrete operations like counting and equality, do it in integer space. Avoid going from integer space back to floating point space.

Re: Island Generator

#33
post #12

Great writeup! Notice that range stops just shy of 2π. Floating point math sometimes produces a final angle indistinguishably close to 2π, adding one extra vertex to our n-gon. To see for yourself, change (- tau 0.000001) to tau and set the number of sides to 6. You’ll see an extra point in the output very close to the starting point. This is a "trick" that you find everywhere in graphics programming, and I can't bel…

Final angle? If it's gonna close the path for you, just stop at n-1. No extra side.
Post reply on HN