Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions donut.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

int main() {
float A = 0, B = 0;
float i, j;
int k;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using bzero instead of memset for zeroing out memory. It might be slightly more efficient in this case.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable z is declared as a float array but used as an array of integers. This could lead to unexpected behavior.

float z[1760];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size of z and b should be calculated based on the loop bounds to avoid potential buffer overflows. Consider using sizeof(z) / sizeof(z[0]) and sizeof(b) / sizeof(b[0]) instead of hardcoding the values.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name than z for the depth buffer. Something like depthBuffer or zBuffer would be more clear.

char b[1760];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to use a constant for the terminal escape sequence to enhance readability and maintainability.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable z is declared with a size of 1760, but it is used with an index of o, which can potentially be greater than 1760. This could lead to out-of-bounds access and undefined behavior. Consider using a larger size for z to prevent this issue.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good practice to use a descriptive variable name instead of a single letter for j and i. For example, angleJ and angleI.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, b could be renamed to chars or outputChars for better readability.

printf("\x1b[2J");
for(;;) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment explaining the purpose of the memset calls would improve clarity.

memset(b,32,1760);
memset(z,0,7040);
for(j=0; j < 6.28; j += 0.07) {
for(i=0; i < 6.28; i += 0.02) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant 6.28 is used multiple times. Consider defining a constant for it to improve readability and maintainability.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's recommended to avoid using magic numbers. Consider defining constants for values like 6.28, 0.07, and 0.02. This will enhance code readability and maintainability.

float c = sin(i);
float d = cos(j);
float e = sin(A);
float f = sin(j);
float g = cos(A);
float h = d + 2;
float D = 1 / (c * h * e + f * g + 5);
float l = cos(i);
float m = cos(B);
float n = sin(B);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using > and < for comparisons, using >= and <= would be more robust and prevent potential issues at the boundaries.

float t = c * h * g - f * e;
int x = 40 + 30 * D * (l * h * m - t * n);
int y= 12 + 15 * D * (l * h * n + t * m);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable N is used as an index into the string ".,-~:;=!*#$@", but it can potentially be negative. This could lead to out-of-bounds access and undefined behavior. Consider using abs(N) or clamping the value to the range of the string length to prevent this issue.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable N is calculated but not used directly. This might indicate a potential bug or unused code.

int o = x + 80 * y;
int N = 8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if condition checks for D > z[o] but z[o] is initialized to 0. This could lead to unexpected behavior in the first iteration.

if(22 > y && y > 0 && x > 0 && 80 > x && D > z[o]) {
z[o] = D;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable b is declared as a character array but assigned a string. This could lead to unexpected behavior and potential memory issues.

b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses putchar to print characters, but it doesn't handle newline characters correctly. The k % 80 condition only checks for multiples of 80, which may not align with the desired output format. Consider using a separate loop to print newlines at the end of each line.

}
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using 10 as the newline character, use the constant \n for better readability.

printf("\x1b[H");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to use a more descriptive variable name than k for the loop counter.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses usleep to pause execution for 30 milliseconds, which is a relatively short time. Consider using a longer delay or adjusting the update frequency of the donut animation to make it more visually appealing.

for(k = 0; k < 1761; k++) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usleep function call is used for a delay. Consider using a more platform-independent solution, like nanosleep.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usleep function is non-portable. Consider using nanosleep for better portability.

putchar(k % 80 ? b[k] : 10);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to add a comment explaining why you're using usleep here. It's not immediately obvious from the code.

A += 0.00004;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using usleep, consider using nanosleep for more precise timing control.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name than A and B. Names like rotationAngleA and rotationAngleB would improve code understanding.

B += 0.00002;
}
usleep(30000);
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good practice to add a comment explaining the purpose of the return 0 statement.

return 0;
}