Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Turtle Window?

I am generating diagrams in Turtle, and as part of my program, I identify certain coordinates from my diagrams. I would like to be able to hide the complete turtle window, since I only care about the coordinates, is that possible?

Edit:

QUESTION 2:

This isn't really an answer, but a few other questions.

I got my program working to some extent, if you run it in IDLE and type "l" it will give you the list with the coordinates.

import Tkinter
import turtle

from turtle import rt, lt, fd   # Right, Left, Forward

size = 10

root = Tkinter.Tk()
root.withdraw()

c = Tkinter.Canvas(master = root)
t = turtle.RawTurtle(c)

t.speed("Fastest")

# List entire coordinates
l = []

def findAndStoreCoords():
    x = t.xcor()
    y = t.ycor()

    x = round(x, 0)     # Round x to the nearest integer
    y = round(y, 0)     # Round y to the nearest integer

    # Integrate coordinates into sub-list
    l.append([x, y])

def hilbert(level, angle):
    if level == 0:
        return

    t.rt(angle)
    hilbert(level - 1, -angle)
    t.fd(size)
    findAndStoreCoords()
    t.lt(angle)
    hilbert(level - 1, angle)
    t.fd(size)
    findAndStoreCoords()
    hilbert(level - 1, angle)
    t.lt(angle)
    t.fd(size)
    findAndStoreCoords()
    hilbert(level - 1, -angle)
    t.rt(angle)

The problem is that Turtle is so SLOW! Is there any package that is just like Turtle but can do commands much faster?

like image 218
ElectroNerd Avatar asked Sep 13 '25 02:09

ElectroNerd


1 Answers

I reimplemented the turtle class as suggested by thirtyseven. It is consistent with the api. (i.e. when you turn right in this class, it is the same as turning right in turtle.

This does not implement all the methods in the api, only common ones. (And the ones you used).

However, it's short and fairly straightforward to extend. Also, it keeps track of all of the points it has been to. It does this by adding an entry to pointsVisited every time you call forward, backward, or setpos (or any of the aliases for those functions).

import math

class UndrawnTurtle():
    def __init__(self):
        self.x, self.y, self.angle = 0.0, 0.0, 0.0
        self.pointsVisited = []
        self._visit()

    def position(self):
        return self.x, self.y

    def xcor(self):
        return self.x

    def ycor(self):
        return self.y

    def forward(self, distance):
        angle_radians = math.radians(self.angle)

        self.x += math.cos(angle_radians) * distance
        self.y += math.sin(angle_radians) * distance

        self._visit()

    def backward(self, distance):
        self.forward(-distance)

    def right(self, angle):
        self.angle -= angle

    def left(self, angle):
        self.angle += angle

    def setpos(self, x, y = None):
        """Can be passed either a tuple or two numbers."""
        if y == None:
            self.x = x[0]
            self.y = y[1]
        else:
            self.x = x
            self.y = y
        self._visit()

    def _visit(self):
        """Add point to the list of points gone to by the turtle."""
        self.pointsVisited.append(self.position())

    # Now for some aliases. Everything that's implemented in this class
    # should be aliased the same way as the actual api.
    fd = forward
    bk = backward
    back = backward
    rt = right
    lt = left
    setposition = setpos
    goto = setpos
    pos = position

ut = UndrawnTurtle()
like image 97
Nick ODell Avatar answered Sep 15 '25 15:09

Nick ODell