Narzędzia użytkownika

Narzędzia witryny


narzedzia:fraktale_python

Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Poprzednia rewizja po obu stronachPoprzednia wersja
Nowa wersja
Poprzednia wersja
narzedzia:fraktale_python [2025/05/10 23:54] administratornarzedzia:fraktale_python [2025/05/16 18:39] (aktualna) administrator
Linia 1: Linia 1:
-====== Fraktale w pythonie ======+====== PY: Fraktale w pythonie ======
  
 [[https://en.wikipedia.org/wiki/L-system|Wikipedia: L-systems]] [[https://en.wikipedia.org/wiki/L-system|Wikipedia: L-systems]]
  
 +{{::pythonw_dgrsk9lafw.gif|}}
 +
 +<code python fractal_ardugeek.py>
 +import turtle
 +import time
 +
 +# Configuration
 +FONT = ("Arial", 48, "bold")
 +LETTER_SPACING = 65
 +ANIMATION_DELAY = 0.5  # seconds between drawing each letter
 +FRACTAL_ITER = 2       # reduced iterations for faster drawing
 +FRACTAL_SCALE = 10     # scaling factor for fractal drawing
 +
 +# L-system definitions for each letter (axiom, rules, angle)
 +# 8 distinct fractals for 8 letters in "ArduGeek"
 +L_SYSTEMS = [
 +    ("F", {"F": "F+F--F+F"}, 60),                         # Koch curve
 +    ("FX", {"X": "X+YF+", "Y": "-FX-Y"}, 90),        # Dragon curve
 +    ("F-G-G", {"F": "F-G+F+G-F", "G": "GG"}, 120),    # Sierpinski triangle
 +    ("X", {"X": "F-[[X]+X]+F[+FX]-X", "F": "FF"}, 25),# Fractal plant
 +    ("A", {"A": "B-A-B", "B": "A+B+A"}, 60),         # Arrowhead curve
 +    ("X", {"X": "X+YF++YF-FX--FXFX-YF+", "Y": "-FX+YFYF++YF+FX--FX-Y"}, 90), # Gosper curve
 +    ("F+F+F+F", {"F": "F+F-F-F+F"}, 90),                 # Smaller square Koch variant
 +    ("F", {"F": "F+F-F"}, 120) # Terdragon curve for an intricate, compact design
 +]
 +
 +# Setup screen and turtles
 +def setup():
 +    screen = turtle.Screen()
 +    screen.title("ArduGeek with Persistent Fractals")
 +    screen.bgcolor("white")
 +
 +    pen = turtle.Turtle()
 +    pen.hideturtle()
 +    pen.penup()
 +    pen.speed(1)
 +    pen.color("darkblue")
 +
 +    fractal_t = turtle.Turtle()
 +    fractal_t.hideturtle()
 +    fractal_t.penup()
 +    fractal_t.speed(0)
 +    fractal_t.color("gray")
 +
 +    return screen, pen, fractal_t
 +
 +# Generate L-system string
 +def generate_lsystem(axiom, rules, iterations):
 +    s = axiom
 +    for _ in range(iterations):
 +        s = ''.join(rules.get(ch, ch) for ch in s)
 +    return s
 +
 +# Draw L-system with given turtle
 +def draw_lsystem(t, instructions, angle, scale):
 +    for cmd in instructions:
 +        if cmd in ('F', 'G'):
 +            t.forward(scale)
 +        elif cmd == '+':
 +            t.right(angle)
 +        elif cmd == '-':
 +            t.left(angle)
 +        elif cmd == '[':
 +            stack.append((t.position(), t.heading()))
 +        elif cmd == ']':
 +            pos, head = stack.pop()
 +            t.penup()
 +            t.goto(pos)
 +            t.setheading(head)
 +            t.pendown()
 +
 +# Main animation: draw each fractal once, then write letters on top
 +def animate_text_with_fractals(screen, pen, fractal_t, text):
 +    # center the text on screen
 +    total_width = len(text) * FONT[1] * 0.6 + (len(text)-1) * LETTER_SPACING
 +    start_x = -total_width / 2
 +    baseline_y = 0
 +    pen.goto(start_x, baseline_y)
 +
 +    # draw persistent fractals behind each letter
 +    for i, letter in enumerate(text):
 +        axiom, rules, angle = L_SYSTEMS[i]
 +        inst = generate_lsystem(axiom, rules, FRACTAL_ITER)
 +        fractal_t.penup()
 +        # align each fractal under its letter
 +        fractal_t.goto(start_x + i*(FONT[1]*0.6 + LETTER_SPACING), baseline_y - FONT[1]*0.6)
 +        fractal_t.setheading(90)  # align fern and others upright
 +        fractal_t.pendown()
 +        global stack
 +        stack = []
 +        draw_lsystem(fractal_t, inst, angle, FRACTAL_SCALE)
 +        fractal_t.penup()
 +
 +    # write all letters on top, one by one
 +    pen.goto(start_x, baseline_y)
 +    for letter in text:
 +        pen.write(letter, font=FONT, align="left")
 +        pen.forward(FONT[1]*0.6 + LETTER_SPACING)
 +        time.sleep(ANIMATION_DELAY)
 +
 +if __name__ == '__main__':
 +    
 +    screen, pen, fractal_t = setup()
 +    input()
 +    animate_text_with_fractals(screen, pen, fractal_t, "ArduGeek")
 +    pen.penup()
 +    screen.mainloop()
 +
 +</code>
  
 {{:narzedzia:fractal_plant.jpg?400}} {{:narzedzia:fractal_plant.jpg?400}}
Linia 96: Linia 205:
 {{:narzedzia:pasted:20250508-161954.png?400}} {{:narzedzia:pasted:20250508-161954.png?400}}
  
-<code python >+<code python siepinsky_triangle.py>
 from turtle import * from turtle import *
  
narzedzia/fraktale_python.1746914052.txt.gz · ostatnio zmienione: 2025/05/10 23:54 przez administrator