Skip to content

Instantly share code, notes, and snippets.

@icavalheiro
Created June 4, 2024 21:27
Show Gist options
  • Select an option

  • Save icavalheiro/7eae2ec73dc258bfad6fb2e97b6f4b21 to your computer and use it in GitHub Desktop.

Select an option

Save icavalheiro/7eae2ec73dc258bfad6fb2e97b6f4b21 to your computer and use it in GitHub Desktop.

Revisions

  1. icavalheiro created this gist Jun 4, 2024.
    65 changes: 65 additions & 0 deletions donut.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    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());
    }