Turtle Graphics are incompatible with gmpy

Mensanator mensanator at aol.com
Wed Aug 5 23:03:05 CEST 2009


On Aug 5, 12:56 pm, Gregor Lingl <gregor.li... at aon.at> wrote:
> Mensanator schrieb:
>
>
>
>
>
> >> It didn't form 2.5 to 2.6 (at least not intentionally). But with the
> >> indroduction of the TurtleScreen class and the Screen class/object
> >> (singleton) a few of the turtle methods were also implemented as screen
> >> methods and as turtle methods declared deprecated (see docs of Python
> >> 2.6). These deprecated turtle methods do not occur as turtle methods any
> >> more in Python 3.x.
>
> > More info.
>
> > Yes, there is no tracer attribute...when the object is created.
>
> > But watch this (Python 3.1):
>
> >>>> import turtle
> >>>> tooter = turtle.Turtle()
> >>>> tooter.tracer
> > Traceback (most recent call last):
> >   File "<pyshell#2>", line 1, in <module>
> >     tooter.tracer
> > AttributeError: 'Turtle' object has no attribute 'tracer'
> >>>> tooter.hideturtle()
> >>>> tooter.speed('fast')
> >>>> turtle.update()
> >>>> turtle.tracer
> > <function tracer at 0x013E0ED0>
>
> > Now, after setting hide, speed, update, a tracer exists.
>
> No,

Yeah, I mistyped my example, sorry about that. I thought sure
it was the math error that lead me to the tracer issue, but that
might have been a mistake.

>
> Nevertheless I'd like to see a working Python 2.5 version of your script.

I've commented out the parts that v2.5 doesn't have.
So the pen is still down and I have no .dot function,
but the gotos appear to still work as evidenced by
the large black square it now draws.

With tracing on in 2.5, I see the turtle move around
the square. With tracing off, I still the the square
outined, but without a turtle and much faster.

With 3.1, the screen is completely blank for 3-4 seconds and appears
all at once when it's done.

import gmpy

##   (even) hi----|
##                |
##                lo (odd)
## or
##
##   (even) lo
##           |
##           |
##           ----hi (odd)
##
##
##
##

import turtle

tooter = turtle.Turtle()

#tooter.hideturtle()
tooter.speed('fast')
#turtle.update()
tooter.tracer(False)
#tooter.penup()
tooter.color('black')

s = ['1','0']
while len(s[0])<10000:
    s = [''.join(s), s[0]]


origin = [0,0]
if s[0] == '0':
  tooter.goto(origin)
  #tooter.dot(1)
if s[1] == '0':
  tooter.goto([1,0])
  #tooter.dot(1)

print len(s[0])

for i,j in enumerate(s[0]):
  the_coord=[]
  cur_root = gmpy.sqrt(i)
  lo__root = gmpy.sqrt(i)**2
  hi__root = (gmpy.sqrt(i)+1)**2

  if hi__root%2==0:
    side = 'northeast'
  else:
    side = 'southwest'

  elbow = (hi__root - lo__root)/2 + lo__root + 1

  if i>= elbow:
    side_len = i - elbow
    elbow_plus = True
  else:
    side_len = elbow - i
    elbow_plus = False

  if side == 'northeast':
    elbow_offset = [(gmpy.sqrt(elbow)-1)/2 +1,-((gmpy.sqrt(elbow)-1)/2
+1)]
  else:
    elbow_offset = [-((gmpy.sqrt(elbow)-1)/2 +1),((gmpy.sqrt(elbow)-1)/
2 +1)]

  elbow_coord = [origin[0]+elbow_offset[0],origin[1]+elbow_offset[1]]

  if i != hi__root and i != lo__root:
    if i == elbow:
      the_coord = elbow_coord
    else:
      if elbow_plus:
        if side == 'northeast':
          the_coord = [elbow_coord[0]-side_len,elbow_coord[1]]
        else:
          the_coord = [elbow_coord[0]+side_len,elbow_coord[1]]
      else:
        if side == 'northeast':
          the_coord = [elbow_coord[0],elbow_coord[1]+side_len]
        else:
          the_coord = [elbow_coord[0],elbow_coord[1]-side_len]
  else:
    if i % 2 == 0:  # even square
      n = gmpy.sqrt(i)/2 - 1
      the_coord = [-n, -n-1]
    else:
      n = (gmpy.sqrt(i)-1)/2 - 1
      the_coord = [1+n, 1+n]
  if j == '0':
    tooter.goto(the_coord)
    #tooter.dot(2)
print 'done'

#turtle.update()
turtle.done()
print 'done'


>
> Regards,
> Gregor



More information about the Python-list mailing list