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

Criar página sobre L-System (Sistema de Lindenmayer) #108

Closed
villares opened this issue May 2, 2021 · 1 comment
Closed

Criar página sobre L-System (Sistema de Lindenmayer) #108

villares opened this issue May 2, 2021 · 1 comment

Comments

@villares
Copy link
Owner

villares commented May 2, 2021

Referências:

axioma = "X"
regras = {"X": "F+[[X]-X]-F[-FX]+X",
          "F": "FF"
          }          
tamanho = 10
angulo = 25
iteracoes = 4  # repeticoes (voltas na aplicação das regras)
xo, yo = 300, 500

def setup():
    global frase
    size(600, 600)
    frase = gerar_sistema(iteracoes)
    print(len(frase))

def draw():
    background(240, 240, 200)
    translate(xo, yo)
    desenha_sistema(frase)
            
def keyPressed():
    global tamanho, angulo, iteracoes, frase
    if key == 'z':
        tamanho -= 1 # tamanho = tamanho - 1
    if key == 'x':
        tamanho += 1
    if key == 'a':
        angulo -= 1
    if key == 's':
        angulo += 1       
    if key == 'q':
        iteracoes -= 1
        frase = gerar_sistema(iteracoes)
        print(len(frase))
    if key == 'w':
        iteracoes += 1   
        frase = gerar_sistema(iteracoes)
        print(len(frase))

            
def desenha_sistema(simbolos):
    """
    Recebe uma frase e desenha de acordo com
    as "regras de desenho".
    """
    for simbolo in simbolos:
        if simbolo == "F":
            line(0, 0, 0, -tamanho)
            translate(0, -tamanho)
        if simbolo == "+":
            rotate(radians(angulo))
        if simbolo == "-":
            rotate(radians(-angulo))
        if simbolo == "[":
            pushMatrix()
        if simbolo == "]":
            popMatrix()

def gerar_sistema(num):
    """
    Produz a partir da frase na variável globak axioma,
    repetindo `num` iterações, um sistema-L seguindo as
    regras do dicionário global `regras`
    """
    frase = axioma
    for i in range(num):
        frase_nova = ""
        for simbolo in frase:
            substituicao = regras.get(simbolo, simbolo)  
            frase_nova = frase_nova + substituicao
        frase = frase_nova
    return frase
@villares
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant