◂ 18 décembre 2023 ▸
Lavaduct Lagoon : Calculer l'aire délimitée par un parcours
L 6 (#250012)
U 2 (#66d6a3)
L 4 (#0a6532)
⋮
D 5 (#72f323)
⋮
Comme au problème 10, on peut résoudre ça efficacement avec formule de laçage + théorème de Pick. Il faut juste s'assurer que la courbe ne se coupe pas.
- code.py
- fonction_dessiner.py
""" Ce bout de code permet de visualier le circuit dans le terminal, ou à l'aide de matplotlib. """
def dessiner(moves: list[Move], terminal=False, vraies_positions=False) -> None:
RESET = '\033[0m'
BOLD = '\033[01m'
RED = '\033[31m'
allX = set([0])
allY = set([0])
pos = [0,0]
for dir, nb in moves:
pos[0] += nb*dir[0]
pos[1] += nb*dir[1]
allX.add(pos[0])
allY.add(pos[1])
allX = sorted(allX)
allY = sorted(allY)
print(f"x de {allX[0]} à {allX[-1]}, y de {allY[0]} à {allY[-1]}")
W = len(allX)*2 - 1
H = len(allY)*2 - 1
# indice 0 => 0, indice 1 => 2, etc.
pos = [0,0]
pos_grid = [2*allX.index(0), 2*allY.index(0)]
if terminal:
positions = set()
else:
positions = [pos_grid]
real_positions = [(0,0)]
last_pos_grid = None
for dir, nb in moves:
pos[0] += nb*dir[0]
pos[1] += nb*dir[1]
last_pos_grid = pos_grid
pos_grid = (2*allX.index(pos[0]), 2*allY.index(pos[1]))
if terminal:
if dir[0] == 0:
positions.update([ (pos_grid[0], y) for y in range(last_pos_grid[1], pos_grid[1], dir[1])])
else:
positions.update([ (x, pos_grid[1]) for x in range(last_pos_grid[0], pos_grid[0], dir[0])])
else:
positions.append(pos_grid)
real_positions.append(pos[:])
if terminal:
for y in range(H):
for x in range(W):
print(BOLD + RED + '#' + RESET if (x,y) in positions else '.',end='')
print()
else:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(*zip(*list(real_positions if vraies_positions else positions)))
plt.show()