Wondering about learning Python

C

Chris

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

Duncan Booth

Chris said:
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()
 
J

Josef Dalcolmo

....

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
 
J

Josef Dalcolmo

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
 
D

Duncan Booth

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

Grant Edwards

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
shadow mask of your CRT.

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

Lee Harr

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)
 
P

Patrick Ellis

Duncan said:
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.
 
P

Pete Shinners

Chris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top