DEV Community

Jaipal001
Jaipal001

Posted on

Explaining donut like 5 years old Part-3

First fill zBuffer with 0s and buffer with spaces (32 in ascii)

For C, We use string.h library
As float takes 4 bytes, 4 * 1760 = 7040
In java, import java.util.Arrays and use Arrays.fill() function

In C

memset(buffer, ' ', 1760);
memset(zBuffer, 0, 7040);
Enter fullscreen mode Exit fullscreen mode

In Java

Arrays.fill(zBuffer, 0);
Arrays.fill(buffer, ' ');
Enter fullscreen mode Exit fullscreen mode

put 2 nested for loops

for (float theta = 0; theta < 6.28; theta += 0.07) {
    for (float phi = 0; phi < 6.28; phi += 0.02) {
    }
}
Enter fullscreen mode Exit fullscreen mode

First we will understand remaining code after outer for-loop

print "\x1b[H" which clears screen

create for-loop to print all characters in buffer-array

for (int i = 0; i <1761; i++) {
     putchar(i % 80 ? buffer[i] : 10);
     A += 0.00004;
     B += 0.00002;
}
Enter fullscreen mode Exit fullscreen mode

if i is not divisible by 80 i.e. there is some remainder then print the buffer[i] character else newline (\n or 10 in ascii)

then increment A and B so they can rotate in X and Z axis but make them small so that rotation looks smooth

In C, add usleep(10000); because spinning will be very fast so we have to slow it a bit
add unistd.h for accessing this function

Top comments (0)