Tuesday, September 3, 2024

donut.c converted to donut.py (Python)

Here's my version of Andy Sloane's donut.c converted to Python programming language.

#!/usr/bin/env python3
#
# python port of donut.c
#

import sys
import time
import math
import itertools

def donut() -> list:
    z = list(itertools.repeat(0.0, 1760))
    b = list(itertools.repeat(' ', 1760))
    j = 0.0
    while j < 6.28:
        i = 0.0
        while i < 6.28:
            c: float = math.sin(i)
            d: float = math.cos(j)
            e: float = math.sin(A)
            f: float = math.sin(j);
            g: float = math.cos(A);
            h: float = d + 2.0;
            D: float = 1.0 / (c * h * e + f * g + 5.0);
            l: float = math.cos(i);
            m: float = math.cos(B);
            n: float = math.sin(B);
            t: float = c * h * g - f * e;
            x = int(40 + 30 * D * (l * h * m - t * n))
            y = int(12 + 15 * D * (l * h * n + t * m))
            o = int(x + 80 * y)
            N = int(8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n))
            if 22 > y and y > 0 and x > 0 and 80 > x and D > z[o]:
                z[o] = D
                b[o] = ".,-~:;=!*#$@"[N if N > 0 else 0]
            i += 0.02
        j += 0.07
    for k in range(0, 1760, 80):
        b[k] = '\n'
    return b

A: float = 0.0
B: float = 0.0
sys.stdout.write('\x1b[2J')
while True:
    sys.stdout.write('\x1b[H' + ''.join(donut()) + '\n')
    A += 0.04
    B += 0.02
    time.sleep(0.03)



No comments:

Post a Comment