For standard Python users, here's a version that runs on cPython:
Code:
from math import *
from time import *
from graphics import *
t0 = monotonic() # Save the current clock count for timing program
# Define graphics screen
win=GraphWin("Green Hat", 320, 200)
win.setCoords(0,0,319,199)
win.setBackground("black")
# Start program proper
p=160; q=100
xp=144; xr=1.5*3.1415927
yp=56; yr=1; zp=64
xf=xr/xp; yf=yp/yr; zf=xr/zp
for zi in range(-q,q):
if zi>=-zp and zi<=zp:
zt=zi*xp/zp; zz=zi
xl=int(.5+sqrt(xp*xp-zt*zt))
# Draw one cross-section of figure
for xi in range(-xl,xl+1):
xt=sqrt(xi*xi+zt*zt)*xf; xx=xi
yy=(sin(xt)+.4*sin(3*xt))*yf
x1=round(xx+zz+p)
y1=round(yy-zz+q)
win.plot(x1,y1,"green")
if y1!=0:
lin=Line(Point(x1,y1-1),Point(x1,y1-10)) # Erase points below current point
lin.draw(win)
print(monotonic() - t0,"seconds")
# Wait for mouse click and close window
win.getMouse()
win.close()