INFO-MPx-2021/labyrinthe.py

42 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
def affiche(segments, n, p, anim=False):
"""affiche les segments de la liste segments d'une grille n x p
avec éventuelle animation"""
# graphe orthonormé sans axe
plt.axis('off')
plt.axis('equal')
plt.axis([-1, n + 1, -1, p + 1])
# Affichage du fond
plt.fill([-1, -1, n, n], [-1, p, p, -1], 'gray', lw=0)
# Affichage du labyrinthe
affiche_couleur(segments, n, p, 'w', anim)
def affiche_couleur(segments, n, p, couleur, anim=False):
"""affichage des segments avec couleur"""
largeur_trait = 150 / max(n, p)
# affichage d'un départ
plt.plot([-1, 0], [0, 0], color=couleur, lw=largeur_trait)
# affichage des segments
for P1, P2 in segments:
x1, y1 = P1
x2, y2 = P2
plt.plot([x1, x2], [y1, y2], color=couleur, lw=largeur_trait)
if anim:
plt.pause(1e-6)
# affichage d'une arrivée
plt.plot([n, n - 1], [p - 1, p - 1], color=couleur, lw=largeur_trait)
# pause à la place de show permet d'éventuelles animations
plt.pause(1e-6)
# Enregistrement dans un fichier
plt.savefig('laby_{}.png'.format(couleur))