Discussion in 'Python' started by Chris, Jul 22, 2004.

1. ### ChrisGuest

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.

I'm wondering about learning Python - just for fun - and it would be
interesting to see how easy it is in comparison with qbasic.
--
Chris

Chris, Jul 22, 2004

2. ### Duncan BoothGuest

Chris <nospam@[127.0.0.1]> wrote in news:bKe5VVARw5\$AFwRs@[127.0.0.1]:

> Anyway, I wonder if anyone would be so kind as to convert it to Python.
>
> I'm wondering about learning Python - just for fun - and it would be
> interesting to see how easy it is in comparison with qbasic.

Python version (put it in a file 'spiral.py' then just run it):

from turtle import *

def spiral():
reset()
degrees()
tracer(0)
for i in range(40000):
t = i/100.
x = .5 * t * cos(t)
y = .5 * t * sin(t)
goto(x, y)

if __name__=='__main__':
spiral()
# The code below lets the screen update and then waits until you
# close the window.
import turtle
turtle._root.mainloop()

Duncan Booth, Jul 22, 2004

3. ### Josef DalcolmoGuest

on 22 Jul 2004 11:50:10 GMT
Duncan Booth <> wrote:

> Chris <nospam@[127.0.0.1]> wrote in news:bKe5VVARw5\$AFwRs@[127.0.0.1]:
>

....

sorry, your example doesn't work for me: the turtle sits there in the middle of the window pointing to the right and the program hangs.

- Josef

Josef Dalcolmo, Jul 22, 2004
4. ### Josef DalcolmoGuest

on Thu, 22 Jul 2004 14:35:57 +0200
Josef Dalcolmo <> wrote:

> sorry, your example doesn't work for me: the turtle sits there in the middle of the window pointing to the right and the program hangs.

Sorry, I put my foot in the mouth: I just didn't wait long enough for the spiral to appear.

- Josef

Josef Dalcolmo, Jul 22, 2004
5. ### Duncan BoothGuest

Josef Dalcolmo <> wrote in
news:20040722144601.00004df5@titan:

> on Thu, 22 Jul 2004 14:35:57 +0200
> Josef Dalcolmo <> wrote:
>
>> sorry, your example doesn't work for me: the turtle sits there in the
>> middle of the window pointing to the right and the program hangs.

>
> Sorry, I put my foot in the mouth: I just didn't wait long enough for
> the spiral to appear.
>

Don't worry about it, it fooled me as well. Change the 'trace(0)' to
'trace(1)' (or just delete it entirely: trace(1) is the default) and you
get to see the turtle doing the drawing.

Duncan Booth, Jul 22, 2004
6. ### Grant EdwardsGuest

On 2004-07-22, Chris <nospam@[> wrote:

> 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.

I suspect the patterns to which you refer are the moire
patterns generated when you superimpose the pattern you are
drawing and the two "grids" imposed by the rasterizaion done by
your frame buffer and the grid formed by the phosphor dots and

http://www.mathematik.com/Moire/
http://www.exploratorium.edu/snacks/moire_patterns.html
http://eluzions.com/Illusions/Moire/

--
Grant Edwards grante Yow! Vote for ME
at -- I'm well-tapered,
visi.com half-cocked, ill-conceived
and TAX-DEFERRED!

Grant Edwards, Jul 22, 2004
7. ### Lee HarrGuest

On 2004-07-22, Chris <nospam@[> wrote:
> 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.
>

It is not a direct translation, but here is how I made a spiral
method for my Penguin object in pygsear
(http://www.nongnu.org/pygsear/)

class Penguin(Turtle):
def spiral(self, turns=1, rPerT=100, stepsPerT=60, connect=0):
'''Make a spiral shape

@param turns: number of times to go around the spiral.
@param rPerT: amount spiral grows per turn.
@param stepsPerT: number of line segments in each spiral turn.
(more segments makes a smoother curve)
@param connect: if True, draw a line from the center to each
generated point on the curve.

'''

center = self.get_position()
for c in range(turns * stepsPerT):
f = (PIx2 * rPerT/stepsPerT) * (float(c) / stepsPerT)
self.forward(f)
self.right(360.0 / stepsPerT)
if connect:
p = self.get_position()
self.lineTo(center)
self.lineTo(p)

Lee Harr, Jul 22, 2004
8. ### Patrick EllisGuest

Duncan Booth wrote:
> from turtle import *
>
> def spiral():
> reset()
> degrees()
> tracer(0)
> for i in range(40000):
> t = i/100.
> x = .5 * t * cos(t)
> y = .5 * t * sin(t)
> goto(x, y)
>
> if __name__=='__main__':
> spiral()
> # The code below lets the screen update and then waits until you
> # close the window.
> import turtle
> turtle._root.mainloop()

Change
> for i in range(40000):
> t = i/100.

into
> for t in range(200):

and you get something fairly cool looking, assuming spirals are cool.

Patrick Ellis, Jul 23, 2004
9. ### Pete ShinnersGuest

Chris wrote:
> 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

Someone has already posted a Python Turtle example, you can also do a
pretty direct translation using Pygame. It turns out I already have this
exact same algorithm on my drive. Someone else must have been using the
same example. See attached.

Pete Shinners, Jul 23, 2004