Spiral

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu May 30 09:37:58 EDT 2002


Chris <nospam@[127.0.0.1]> wrote in news:gXPDTYGWch98Ew1w@[127.0.0.1]:

> I've written a small QBASIC program which draws a spiral. Here is the 
> code:
> 
> SCREEN 12
> CLS
> FOR t = 1 TO 400 STEP .01
> x = .5 * t * COS(t)
> y = .5 * t * SIN(t)
> PSET (x + 320, y + 240)
> NEXT t
> 
> I noticed that it generated some interesting patterns, probably as a 
> result of rounding errors.  These can be explored further by making the 
> spiral tighter.
> 
> Anyway, I wonder if anyone would be so kind as to convert it to Python
> because then I can try and decide whether Python is easy enough for me 
> to learn!

How about:
>>> from turtle import *
>>> reset()
>>> tracer(0)
>>> for t1 in range(1, 40000):
	t = t1/100.
	x, y = .5 * t * sin(t/180.*pi), .5 * t * cos(t/180.*pi)
	goto(x, y)

	
Notes: Python uses radians for cos and sin, so you have to convert your 
degrees to radians.
Use 'tracer(1)' to see what is happening (but it is much slower).

You get pretty much the same output with:
>>> from turtle import *
>>> reset()
>>> tracer(0)
>>> for t in range(1, 400):
	x, y = .5 * t * sin(t/180.*pi), .5 * t * cos(t/180.*pi)
	goto(x, y)

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list