Someone mentioned the other day that they'd never done a rotating cube in any
programming language, and to encourage them, I spent half an hour writing the
simplest rotating cube demo I could think of.  People seem to like it!  The
code, that is.  The visuals are a bit dull.

#!/usr/bin/python
"Very simple rotating cube in ASCII art."
import sys, time

w, h, out = 80, 24, sys.stdout
cube = [(x, y, z) for x in -1, 1 for y in -1, 1 for z in -1, 1]
s = 0.1                                 # sine
c = (1 - s**2)**0.5                     # cosine
ym = h/3                                # Y magnification
xm = 2*ym                               # X magnification

while True:
    cube = [(c*x + s*z, y, -s*x + c*z) for x, y, z in cube] # Rotate by theta.
    proj = [(round(w/2+xm*x/(z+2)), round(h/2+ym*y/(z+2))) for x, y, z in cube]
    out.write('\033[H' + '\n'.join(
            ''.join(('*' if (x, y) in proj else ' ') for x in range(w))
            for y in range(h)))
    out.flush()
    time.sleep(1/15.0)
-- 
To unsubscribe: http://lists.canonical.org/mailman/listinfo/kragen-hacks

Reply via email to