vha3 / Hunter-Adams-RP2040-Demos

Demo code for the Raspberry Pi Pico
210 stars 55 forks source link

fix28 overflow #4

Open rs1729 opened 8 months ago

rs1729 commented 8 months ago

VGA_Graphics/Mandelbrot_Set/mandelbrot_fixvfloat.c https://github.com/vha3/Hunter-Adams-RP2040-Demos/blob/f5f85bafe148e3ee578c67afbdb08fc9f6d4ec0f/VGA_Graphics/Mandelbrot_Set/mandelbrot_fixvfloat.c#L188 The fix28 values Zre_sq + Zim_sq can overflow, resulting in artifacts. You can mitigate the problem a little by

    if ((unsigned)(Zre_sq + Zim_sq) > (unsigned)FOURfix28) break;

(or using fix27, but losing precision). However some points remain for which the iterates will overflow. Probably better to check for potential overflow before the escape check (affects only iterates outside the circle)

    if (Zre < -TWOfix28 || Zre > TWOfix28) break;
    if (Zim < -TWOfix28 || Zim > TWOfix28) break;

    if ((unsigned)(Zre_sq + Zim_sq) > (unsigned)FOURfix28) break;

In principle the rectangle check is an escape check, so it would also work (the rectangle contains the circle), but the contour lines outside the Mandelbrot set would look different.