Exemplo n.º 1
0
scale = 0.5
grammar.add_non_terminal('Scale', [
    lambda turtle: turtle.scale(scale),
    lambda turtle: turtle.scale_thickness(0.6),
])
grammar.add_non_terminal('Scale', [])

grammar.add_non_terminal('Tree', [
    lambda turtle: turtle.store(),
    'Forward Left',
    'Scale',
    'Tree',
    lambda turtle: turtle.restore(),
    lambda turtle: turtle.store(),
    'Forward Right',
    'Scale',
    'Tree',
    lambda turtle: turtle.restore(),
], 8)

grammar.add_non_terminal('Tree', [])

#for i in range(0, 5):
backend = SVGBackend()
turtle = ThickTurtle(backend)
turtle.thickness = 0.05

grammar.expand('Root', turtle, 20)
backend.write('flower3.svg')
Exemplo n.º 2
0
    lambda turtle: turtle.forward(0.1), lambda turtle: turtle.turn_right(5)
]

left_segment = [
    lambda turtle: turtle.forward(0.1), lambda turtle: turtle.turn_left(5)
]

#grammar.add_non_terminal('Left', left_segment * 7)
#grammar.add_non_terminal('Left', left_segment * 10)
grammar.add_non_terminal('Left', left_segment * 13)

#grammar.add_non_terminal('Right', right_segment * 7)
#grammar.add_non_terminal('Right', right_segment * 10)
grammar.add_non_terminal('Right', right_segment * 13)

scale = 0.5

grammar.add_non_terminal('Tree', [
    lambda turtle: turtle.store(),
    'Left',
    lambda turtle: turtle.scale(scale),
    'Tree',
    lambda turtle: turtle.restore(),
    'Right',
    lambda turtle: turtle.scale(scale),
    'Tree',
])

grammar.expand('Tree', turtle, 15)
backend.write('tree.svg')
Exemplo n.º 3
0
    lambda turtle: turtle.turn_left(120),
    'Tree',
    lambda turtle: turtle.turn_left(120),
    'Tree',
])

scale = 0.5
grammar.add_non_terminal('Scale', [
    lambda turtle: turtle.scale(scale),
])
grammar.add_non_terminal('Scale', [])

grammar.add_non_terminal('Tree', [
    lambda turtle: turtle.store(),
    'Forward Left',
    'Scale',
    'Tree',
    lambda turtle: turtle.restore(),
    lambda turtle: turtle.store(),
    'Forward Right',
    'Scale',
    'Tree',
    lambda turtle: turtle.restore(),
], 8)

#for i in range(0, 5):
backend = SVGBackend()
turtle = Turtle(backend)
grammar.expand('Root', turtle, 12)
backend.write('flower2.svg')
Exemplo n.º 4
0
from svg_backend import SVGBackend
from turtle import Turtle
from grammar import Grammar

backend = SVGBackend()
turtle = Turtle(backend)

grammar = Grammar()

grammar.add_non_terminal('RS', [
    lambda turtle: turtle.forward(2), 
    lambda turtle: turtle.turn_right(89.9),
    lambda turtle: turtle.scale(1.00001),
    'RS'])

grammar.expand('RS', turtle, 300)
backend.write('rect_spiral.svg')
Exemplo n.º 5
0
from svg_backend import SVGBackend
from turtle import Turtle
from grammar import Grammar

backend = SVGBackend()
turtle = Turtle(backend)

turtle.turn_right(180.0)

grammar = Grammar()

grammar.add_non_terminal('S', [
    lambda turtle: turtle.forward(), lambda turtle: turtle.turn_right(5),
    lambda turtle: turtle.scale(0.99), 'S'
])

grammar.expand('S', turtle, 500)
backend.write('spiral.svg')
Exemplo n.º 6
0
    },
    "$NP": {
        ("$A", "$NP"): Fraction(3, 10),
        ("$N",): Fraction(7, 10),
    },
    "$VP": {
        ("$V", "$NP"): Fraction(3, 4),
        ("$V",): Fraction(1, 4),
    },
    "$N": {
        ('ideas',): .5,
        ('linguists',): .5,
    },
    "$V": {
        ("hate",): .5,
        ("generate",): .5,
    },
    "$A": {
        ("great",): .5,
        ("green",): .5,
    }
}

english = Grammar()
english.add_terminals({'generate', 'hate', 'great', 'green', 'ideas', 'linguists'})
english.add_nonterminals({"$S", '$NP', '$VP', '$N', '$V', '$A'})
for k, v in rules.items():
    english.add_rule(k, v)

print(english.expand("$S"))