104 lines
2.4 KiB
Python
Executable File
104 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Tue Mar 3 10:11:04 2020
|
|
|
|
@author: suwako
|
|
"""
|
|
"""
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.image as mpimg
|
|
import numpy as np
|
|
import copy as c
|
|
M=np.array([[1,0,1],[0,1,1],[1,0,1],[0,1,1]])
|
|
|
|
## ajoute une ligne tout autour de la matrice M
|
|
def bord(M):
|
|
p=np.size(M[:,0])
|
|
q=np.size(M[0,:])
|
|
N=np.zeros((p+2,q+2))
|
|
c=0.3
|
|
N[:,0]=c
|
|
N[:,q+1]=c
|
|
N[0,:]=c
|
|
N[p+1,:]=c
|
|
N[1:p+1,1:q+1]=M[0:p,0:q]
|
|
return(N)
|
|
##agrandissement de la matrice et création de l'image
|
|
def lab(M):
|
|
p=np.size(M[:,0])
|
|
q=np.size(M[0,:])
|
|
r=min(512//p,512//q)
|
|
img=np.zeros((r*p,r*q))
|
|
for i in range(p):
|
|
for j in range(q):
|
|
img[i*r:i*r+r,j*r:j*r+r]=int(M[i,j]*255)
|
|
return(img)
|
|
|
|
img=lab(bord(M))
|
|
plt.imshow(img,cmap=plt.cm.gray)
|
|
plt.show()
|
|
|
|
##passage à la matrice des triplets et retour
|
|
def tr1(M):
|
|
p=np.size(M[:,0])
|
|
q=np.size(M[0,:])
|
|
M1=np.array([[[M[i,j],0,0] for j in range(q)] for i in range(p)])
|
|
return M1
|
|
|
|
def tr(M):
|
|
p=np.size(M[:,0])
|
|
q=np.size(M[0,:])
|
|
M1=np.array([[M[i,j][0] for j in range(q//3)] for i in range(p//3)])
|
|
return M1
|
|
|
|
print(tr(tr1(M)))"""
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import random
|
|
|
|
def bord(M):
|
|
return np.pad(np.array(M, dtype='float64'), pad_width=1, mode='constant', constant_values=0.3)
|
|
|
|
def img(M):
|
|
return np.kron(M, np.full((512, 512), 255))
|
|
|
|
|
|
def tr(M):
|
|
return np.array([[j[0] for j in i] for i in M])
|
|
|
|
def tr1(M):
|
|
return np.array([[(j, 0, 0) for j in i] for i in M])
|
|
|
|
def voisin(M, i,j):
|
|
cases=[case for case in [(i,j-1), (i-1,j), (i+1,j), (i,j+1)] if M[case[0]][case[1]][0]==1]
|
|
for case in cases:
|
|
M[case[0]][case[1]]=[0.5, i, j]
|
|
return cases
|
|
|
|
def suivant(M, chemin):
|
|
return [vois for case in chemin for vois in voisin(M, *case)]
|
|
|
|
def chemin(M, depart, fin):
|
|
voisins=voisin(M, *depart)
|
|
i=0
|
|
while fin not in voisins:
|
|
voisins = suivant(M, voisins)
|
|
i+=1
|
|
if i>1000:
|
|
raise
|
|
chemin = [fin]
|
|
while depart != chemin[-1]:
|
|
i,j=chemin[-1]
|
|
chemin.append(tuple(map(int,M[i][j]))[1:])
|
|
return chemin[::-1]
|
|
def val(p):
|
|
if random.random()<p:
|
|
return(0)
|
|
else:
|
|
return(1)
|
|
p=0.35
|
|
m=15
|
|
M=tr1(bord(np.array([[val(p) for j in range(m)] for i in range(m)])))
|
|
print(chemin(M, (1,3), (4,2)))
|
|
plt.imshow(img(tr(M)), cmap=plt.cm.gray) |