Wondering about learning Python

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

  1. Chris

    Chris Guest

    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
    #1
    1. Advertising

  2. Chris

    Duncan Booth Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. 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
    #4
  5. Chris

    Duncan Booth Guest

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

    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
    #6
  7. Chris

    Lee Harr Guest

    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
    #7
  8. 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
    #8
  9. 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
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. scorpion53061

    I am wondering how you would handle this.....

    scorpion53061, Apr 11, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    299
    Roger Abell [MVP]
    Apr 12, 2004
  2. suresh
    Replies:
    1
    Views:
    363
    Karl Seguin
    Apr 14, 2005
  3. =?Utf-8?B?cm9kY2hhcg==?=

    wondering what the difference is

    =?Utf-8?B?cm9kY2hhcg==?=, Aug 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    354
    Eliyahu Goldin
    Aug 21, 2005
  4. =?Utf-8?B?cm9kY2hhcg==?=

    wondering if ok to ask here...

    =?Utf-8?B?cm9kY2hhcg==?=, Sep 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    354
    Alvin Bruney - ASP.NET MVP
    Sep 14, 2005
  5. ray
    Replies:
    2
    Views:
    178
    Terry Reedy
    Jul 30, 2011
Loading...

Share This Page