import matplotlib.pyplot as plt
import matplotlib.patches as patches
def create_enhanced_stability_plot():
fig, axs = plt.subplots(2, 2, figsize=(14, 12))
fig.suptitle(r'Effet du déplacement de poids sur le centre de gravité $g$', fontsize=18)
# Paramètres du navire
ship_w, ship_h = 40, 20
g_x, g_y = 20, 10
exaggeration = 5
def draw_base_ship(ax):
ax.set_xlim(-5, 45)
ax.set_ylim(-5, 25)
ax.set_aspect('equal')
ax.grid(True, linestyle=':', alpha=0.6)
# Navire
ship_rect = patches.Rectangle((0, 0), ship_w, ship_h, linewidth=2, edgecolor='black', facecolor='lightgrey', alpha=0.3)
ax.add_patch(ship_rect)
# Centre G original
ax.plot(g_x, g_y, 'ko', markersize=6)
ax.text(g_x - 1.5, g_y + 0.5, r'$g$', fontsize=14)
def draw_weight(ax, x_start, y_start, x_end, y_end):
# Position initiale (pointillés)
w_orig = patches.Rectangle((x_start, y_start), 1, 1, linewidth=1, edgecolor='blue', facecolor='none', linestyle='--')
# Position finale (trait plein)
w_final = patches.Rectangle((x_end, y_end), 1, 1, linewidth=1.5, edgecolor='blue', facecolor='blue', alpha=0.6)
ax.add_patch(w_orig)
ax.add_patch(w_final)
# Flèche de mouvement du poids (centrée sur les carrés)
ax.annotate('', xy=(x_end + 0.5, y_end + 0.5), xytext=(x_start + 0.5, y_start + 0.5),
arrowprops=dict(arrowstyle='->', color='blue', lw=1.5))
def draw_g_shift(ax, dx, dy, label_text):
# Déplacement exagéré
target_x = g_x + (dx * exaggeration)
target_y = g_y + (dy * exaggeration)
ax.plot(target_x, target_y, 'ro', markersize=6)
ax.text(target_x - 1, target_y + 0.5, r"$g'$", fontsize=14, color='red')
ax.annotate('', xy=(target_x, target_y), xytext=(g_x, g_y),
arrowprops=dict(arrowstyle='->', color='red', lw=2, alpha=0.8))
ax.set_title(label_text, fontsize=12)
# --- PANEL 1: Latéral ---
ax = axs[0, 0]
draw_base_ship(ax)
draw_weight(ax, 15, 19, 25, 19) # Déplacement de 10m (de 15 à 25)
draw_g_shift(ax, 1.1111, 0, "1. Déplacement latéral")
# --- PANEL 2: Vertical ---
ax = axs[0, 1]
draw_base_ship(ax)
draw_weight(ax, 10.5, 4, 10.5, 14) # Hauteur 5m vers 15m
draw_g_shift(ax, 0, 1.1111, "2. Déplacement vertical")
# --- PANEL 3: Inversé ---
ax = axs[1, 0]
draw_base_ship(ax)
# Déplacement droite vers gauche. Si g se déplace de 0.556 vers la gauche (correction physique)
draw_weight(ax, 25, 19, 15, 19)
draw_g_shift(ax, -0.556, 0, "3. Déplacement de droite à gauche")
# --- PANEL 4: Combiné ---
ax = axs[1, 1]
draw_base_ship(ax)
draw_weight(ax, 15, 9, 25, 19) # 10m droite + 10m haut
draw_g_shift(ax, 1.1111, 1.1111, "4. Déplacement combiné")
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.show()
create_enhanced_stability_plot()