Skip to content

Instantly share code, notes, and snippets.

@icavalheiro
Created June 4, 2024 21:27
Show Gist options
  • Save icavalheiro/7eae2ec73dc258bfad6fb2e97b6f4b21 to your computer and use it in GitHub Desktop.
Save icavalheiro/7eae2ec73dc258bfad6fb2e97b6f4b21 to your computer and use it in GitHub Desktop.
the donut.c formatted and converted to C# for learning
using System.Text;
float sin(float x)
{
return MathF.Sin(x);
}
float cos(float x)
{
return MathF.Cos(x);
}
int k;
float A = 0, B = 0, i, j;
float[] z = new float[1760];
char[] b = new char[1760];
Console.WriteLine("\x1b[2J");
StringBuilder sb;
for (; ; )
{
Thread.Sleep((int)(1000 * (1/24f)));//force to run in 24 FPS
sb = new();
Array.Fill(b, ' ');
Array.Fill(z, 0.0f);
for (j = 0; 6.28f > j; j += 0.07f)
{
for (i = 0; 6.28f > i; i += 0.02f)
{
float c = sin(i),
d = cos(j),
e = sin(A),
f = sin(j),
g = cos(A),
h = d + 2,
D = 1 / (c * h * e + f * g + 5),
l = cos(i),
m = cos(B),
n = sin(B),
t = c * h * g - f * e;
int x = (int)(40 + 30 * D * (l * h * m - t * n)),
y = (int)(12 + 15 * D * (l * h * n + t * m)),
o = x + 80 * y,
N = (int)(8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n));
if (22 > y && y > 0 && x > 0 && 80 > x && D > z[o])
{
z[o] = D;
b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
}
}
}
sb.AppendLine("\x1b[H");
for (k = 0; k < 1761; k++)
{
if (k % 80 == 0 && k != 0)
sb.AppendLine("");
else if (k < 1760) // Ensure we don't access out of bounds
sb.Append(b[k]);
}
A += 0.04f;
B += 0.02f;
Console.Write(sb.ToString());
}
@icavalheiro
Copy link
Author

a nice page explaining the math behind it:
https://www.a1k0n.net/2011/07/20/donut-math.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment