Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed python turtle example #890

Open
jdegenstein opened this issue Jan 29, 2025 · 1 comment
Open

Proposed python turtle example #890

jdegenstein opened this issue Jan 29, 2025 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@jdegenstein
Copy link
Collaborator

jdegenstein commented Jan 29, 2025

Adapted from me and @snoyer:

Still need to add some explanatory comments.

Inspired by https://mathcurve.com/courbes2d.gb/giration/motifs.shtml

from build123d import *
import tkinter
import turtle
from contextlib import contextmanager
from math import cos, radians

@contextmanager
def b3d_turtle():
    main = tkinter.Tk()
    main.withdraw()
    canv = tkinter.Canvas(master=main)
    trtl = turtle.RawTurtle(canv)
    trtl.screen.tracer(0, 0)
    yield trtl

with b3d_turtle() as trtl:
    def meanderc(i, a=0.96, b=2, n=10, samples=1000):
        resample = 7200/samples
        c = 1 / n / b
        return resample*(a * cos(radians(resample*i / b)) + c)

    trtl.begin_poly()

    iters = 300
    for i in range(0, iters):
        trtl.forward(1)
        trtl.right(meanderc(i, samples=iters))

    trtl.end_poly()

with BuildPart() as p:
    with BuildSketch() as s:
        with BuildLine() as l:
            Spline(trtl.get_poly(), periodic=False)
        make_face()
    extrude(amount=-10)

Image

@snoyer
Copy link
Contributor

snoyer commented Jan 30, 2025

you really went digging for that one!

Is there a use for a context manager with no closing (nothing after the yield)?
For this use-case would it be more straightforward to have something like:

def offscreen_turtle():
    """a turtle that doesn't show a drawing window."""
    tk = tkinter.Tk()
    tk.withdraw()
    trtl = turtle.RawTurtle(tkinter.Canvas(master=tk))
    trtl.screen.tracer(0, 0)  # remove delays by disabling animation
    return trtl

trtl = offscreen_turtle()
trtl.begin_poly()
...

@gumyr gumyr added documentation Improvements or additions to documentation enhancement New feature or request labels Jan 30, 2025
@gumyr gumyr added this to the Not Gating Release 1.0.0 milestone Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants