commit
891dc6ae34
4 changed files with 266 additions and 0 deletions
@ -0,0 +1,121 @@ |
|||
|
|||
# Created by https://www.gitignore.io/api/vim,python |
|||
|
|||
### Python ### |
|||
# Byte-compiled / optimized / DLL files |
|||
__pycache__/ |
|||
*.py[cod] |
|||
*$py.class |
|||
|
|||
# C extensions |
|||
*.so |
|||
|
|||
# Distribution / packaging |
|||
.Python |
|||
build/ |
|||
develop-eggs/ |
|||
dist/ |
|||
downloads/ |
|||
eggs/ |
|||
.eggs/ |
|||
lib/ |
|||
lib64/ |
|||
parts/ |
|||
sdist/ |
|||
var/ |
|||
wheels/ |
|||
*.egg-info/ |
|||
.installed.cfg |
|||
*.egg |
|||
|
|||
# PyInstaller |
|||
# Usually these files are written by a python script from a template |
|||
# before PyInstaller builds the exe, so as to inject date/other infos into it. |
|||
*.manifest |
|||
*.spec |
|||
|
|||
# Installer logs |
|||
pip-log.txt |
|||
pip-delete-this-directory.txt |
|||
|
|||
# Unit test / coverage reports |
|||
htmlcov/ |
|||
.tox/ |
|||
.coverage |
|||
.coverage.* |
|||
.cache |
|||
nosetests.xml |
|||
coverage.xml |
|||
*.cover |
|||
.hypothesis/ |
|||
|
|||
# Translations |
|||
*.mo |
|||
*.pot |
|||
|
|||
# Django stuff: |
|||
*.log |
|||
local_settings.py |
|||
|
|||
# Flask stuff: |
|||
instance/ |
|||
.webassets-cache |
|||
|
|||
# Scrapy stuff: |
|||
.scrapy |
|||
|
|||
# Sphinx documentation |
|||
docs/_build/ |
|||
|
|||
# PyBuilder |
|||
target/ |
|||
|
|||
# Jupyter Notebook |
|||
.ipynb_checkpoints |
|||
|
|||
# pyenv |
|||
.python-version |
|||
|
|||
# celery beat schedule file |
|||
celerybeat-schedule |
|||
|
|||
# SageMath parsed files |
|||
*.sage.py |
|||
|
|||
# Environments |
|||
.env |
|||
.venv |
|||
env/ |
|||
venv/ |
|||
ENV/ |
|||
env.bak/ |
|||
venv.bak/ |
|||
|
|||
# Spyder project settings |
|||
.spyderproject |
|||
.spyproject |
|||
|
|||
# Rope project settings |
|||
.ropeproject |
|||
|
|||
# mkdocs documentation |
|||
/site |
|||
|
|||
# mypy |
|||
.mypy_cache/ |
|||
|
|||
### Vim ### |
|||
# swap |
|||
[._]*.s[a-v][a-z] |
|||
[._]*.sw[a-p] |
|||
[._]s[a-v][a-z] |
|||
[._]sw[a-p] |
|||
# session |
|||
Session.vim |
|||
# temporary |
|||
.netrwhist |
|||
*~ |
|||
# auto-generated tag files |
|||
tags |
|||
|
|||
# End of https://www.gitignore.io/api/vim,python |
|||
@ -0,0 +1 @@ |
|||
#TD de SDA : découverte de Python |
|||
@ -0,0 +1,89 @@ |
|||
import matplotlib.pyplot as plt |
|||
import numpy as np |
|||
import random as rd |
|||
|
|||
## Question 1 |
|||
def genere(regles, L0, n): |
|||
L = L0 |
|||
for i in range(n): |
|||
new_res = "" |
|||
for j in L: |
|||
new_res += regles.get(j, j) |
|||
L = new_res |
|||
return L |
|||
|
|||
## Question 2 |
|||
class Tortue: |
|||
def __init__(self, angle, i=1): |
|||
""" Initialise la tortue en (0,0) avec un angle de `angle`.""" |
|||
plt.figure(i) |
|||
plt.axis('equal') |
|||
self.angle = angle |
|||
self.pos = (0,0) |
|||
|
|||
self.stack = [] |
|||
|
|||
def avance(self, r): |
|||
""" Avance la torue de `r` unités. """ |
|||
x,y = self.pos |
|||
self.pos = ( |
|||
x + r*np.cos(self.angle), |
|||
y + r*np.sin(self.angle) |
|||
) |
|||
plt.plot([x, self.pos[0]], [y, self.pos[1]], 'r') |
|||
|
|||
def tourne(self, a): |
|||
""" Tourne la tortue d'un angle a. """ |
|||
self.angle += a |
|||
|
|||
def push(self): |
|||
""" Mémorise l'état courrant. """ |
|||
self.stack.append((self.pos, self.angle)) |
|||
|
|||
def pop(self): |
|||
""" Retourne au précédent état mémorisé. """ |
|||
self.pos, self.angle = self.stack.pop(-1) |
|||
|
|||
def trace(self, s, alpha): |
|||
""" Trace la chaîne de caractères s. """ |
|||
plt.autoscale() |
|||
for c in s: |
|||
if c == '+': |
|||
self.tourne(alpha) |
|||
elif c == '-': |
|||
self.tourne(-alpha) |
|||
elif c == '[': |
|||
self.push() |
|||
elif c == ']': |
|||
self.pop() |
|||
else: |
|||
self.avance(1) |
|||
|
|||
|
|||
## Question 5 |
|||
def genere2(regles, L0, n): |
|||
L = L0 |
|||
for i in range(n): |
|||
new_res = "" |
|||
for j in L: |
|||
regle = regles.get(j, [j]) |
|||
new_res += regle[rd.randint(0,len(regle)-1)] |
|||
L = new_res |
|||
return L |
|||
|
|||
## Question 5 |
|||
def genere3(regles, L0, n): |
|||
L = L0 |
|||
for i in range(n): |
|||
new_res = "" |
|||
for j in L: |
|||
regle = regles.get(j, [(1,j)]) |
|||
poids_total = sum(map(lambda x:x[0], regle)) |
|||
tirage = rd.uniform(0,poids_total-1) |
|||
k = 0 |
|||
while tirage > 0: |
|||
tirage -= regle[k][0] |
|||
k += 1 |
|||
new_res += regle[k-1][1] |
|||
L = new_res |
|||
return L |
|||
@ -0,0 +1,55 @@ |
|||
import numpy as np |
|||
import matplotlib.pyplot as plt |
|||
import lsystem as ls |
|||
|
|||
L1 = ls.genere({"A": "A−B+A+B−A", "B": "BB"}, "A−B−B", 1) |
|||
print("L1 = " + L1) |
|||
assert(L1 == "A−B+A+B−A−BB−BB") |
|||
L2 = ls.genere({"A": "A−B+A+B−A", "B": "BB"}, "A−B−B", 2) |
|||
assert(L2 == "A−B+A+B−A−BB+A−B+A+B−A+BB−A−B+A+B−A−BBBB−BBBB") |
|||
|
|||
plt.close('all') |
|||
t = ls.Tortue(0) |
|||
for i in range(4): |
|||
t.avance(20) |
|||
t.tourne(np.pi/2) |
|||
|
|||
plt.show() |
|||
|
|||
plt.close('all') |
|||
t = ls.Tortue(0) |
|||
s = ls.genere({"A":"A-B+A+B-A", "B":"BB"},"A−B−B", 5) |
|||
t.trace(s,2*np.pi/3) |
|||
plt.show() |
|||
|
|||
plt.close('all') |
|||
t = ls.Tortue(0) |
|||
s = ls.genere({"A":"A-A++A-A", "B":"BB"},"A++A++A", 4) |
|||
t.trace(s,np.pi/3) |
|||
plt.show() |
|||
|
|||
plt.close('all') |
|||
t = ls.Tortue(0) |
|||
s = ls.genere({"A":"A[+A]A[-A]A"},"A", 3) |
|||
t.trace(s,22*np.pi/180) |
|||
plt.show() |
|||
|
|||
plt.close('all') |
|||
t = ls.Tortue(0) |
|||
s = ls.genere({"A":"B[+A][-A]BA", "B":"BB"},"A", 4) |
|||
t.trace(s,25*np.pi/180) |
|||
plt.show() |
|||
|
|||
plt.close('all') |
|||
t = ls.Tortue(0) |
|||
s = ls.genere({"A":"A+b-AA+A+AA+Ab+AA-B+AA-A-AA-Ab-AAA", "b":"bbb"},"A", 2) |
|||
t.trace(s,np.pi/2) |
|||
plt.show() |
|||
|
|||
plt.close('all') |
|||
for i in range(5): |
|||
t = ls.Tortue(0, i) |
|||
s = ls.genere3({"A":[(2,"A[+A]A[-A]A"), (1,"A[+A]A"), (1,"A[-A]A")]},"A", 5) |
|||
t.trace(s,np.pi/6) |
|||
plt.show() |
|||
|
|||
Loading…
Reference in new issue