analitics

Pages

Showing posts with label turtle. Show all posts
Showing posts with label turtle. Show all posts

Thursday, April 3, 2025

Python 3.13.0rc1 : Draw L-System with turtle package.

You can find the turtle python package documentation on the official webpage.
The random python package is default on any python.
import turtle
import random

# Funcții L-System
def generate_lsystem(axiom, rules, iterations):
    """Generarea L-System bazată pe reguli de producție."""
    for _ in range(iterations):
        new_axiom = ""
        for char in axiom:
            new_axiom += rules.get(char, char)
        axiom = new_axiom
    return axiom

def draw_lsystem(axiom, length):
    """Desenarea L-System utilizând turtle."""
    for char in axiom:
        if char == "|":  # Desenează linia
            turtle.forward(length)
        elif char == "+":  # Rotire la dreapta
            angle = random.choice([0,60,120])  # Alegere aleatorie a unghiului
            turtle.right(angle)
            print(f"Rotire dreapta cu {angle} grade.")
        elif char == "-":  # Rotire la stânga
            angle = random.choice([-120,-60])  # Alegere aleatorie a unghiului
            turtle.left(angle)
            print(f"Rotire stânga cu {angle} grade.")
    return angle

# Setări pentru L-System
#axiom = "|---|--+|-+-|+--|+--|+-+|-+-"  # Axioma de bază
axiom = "|---|---|---|---|---+|---+|---" 
rules = {
    "|": "|+|--|-",  # Reguli pentru extensia liniei cu rotații aleatorii
    "+": "+",
    "-": "-"
}

# Generare axiomă nouă după reguli
iterations = 3  # Număr de iterații pentru dezvoltarea L-System
length = 10 # Lungimea fiecărei linii
final_axiom = generate_lsystem(axiom, rules, iterations)


# Inițializare Turtle
turtle.speed(0)
turtle.penup()
turtle.goto(0, 0)  # Poziționare inițială
turtle.pendown()

# Desenare L-System
draw_lsystem(final_axiom, length)

# Finalizare
turtle.hideturtle()
turtle.done()

Sunday, January 14, 2018

The trinket website for learning.

This website comes with this feature:
Trinket lets you run and write code in any browser, on any device.
Trinkets work instantly, with no need to log in, download plugins, or install software.
Easily share or embed the code with your changes when you're done.

  • Just create Your Free Account then use the web interface to play with turtle python module:
  • Trinket lets you run and write code in any browser, on any device.
  • Trinkets work instantly, with no need to log in, download plugins, or install software.
  • Easily share or embed the code with your changes when you're done.