import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle, Arc, Circle
def rotate_point(x, y, angle_deg):
"""Applique une rotation à un point (x, y) autour de l'origine."""
angle_rad = np.radians(angle_deg)
nx = x * np.cos(angle_rad) - y * np.sin(angle_rad)
ny = x * np.sin(angle_rad) + y * np.cos(angle_rad)
return nx, ny
def plot_vessel_diagram():
fig, ax = plt.subplots(figsize=(8, 8))
rotation = 20
# --- Vessel (Rectangle) ---
# TikZ: (-2.5,4) -- (-2.5,0) -- (2.5, 0) -- (2.5,4) -- cycle
vessel_pts = np.array([[-2.5, 4], [-2.5, 0], [2.5, 0], [2.5, 4]])
v_rot = np.array([rotate_point(p[0], p[1], rotation) for p in vessel_pts])
vessel = plt.Polygon(v_rot, closed=True, fill=False, edgecolor='black', linewidth=2)
ax.add_patch(vessel)
# --- Axes ---
# x1 axis: (-4,0) to (4,0)
ax_x1_start = rotate_point(-4, 0, rotation)
ax_x1_end = rotate_point(4, 0, rotation)
ax.annotate('', xy=ax_x1_end, xytext=ax_x1_start, arrowprops=dict(arrowstyle='<->', linestyle=':', color='black'))
ax.text(*rotate_point(4, -0.3, rotation), '$x_1$', ha='center')
# x2 axis: (0,0) to (0,6)
ax_x2_start = rotate_point(0, 0, rotation)
ax_x2_end = rotate_point(0, 6, rotation)
ax.annotate('', xy=ax_x2_end, xytext=ax_x2_start, arrowprops=dict(arrowstyle='->', linestyle=':', color='black'))
ax.text(*rotate_point(0.2, 6, rotation), '$x_2$', va='center')
# --- Angles (theta) ---
# Arc 1: center (1, 2), radius 1, angle 0 to -20
# Matplotlib Arc uses degrees and theta1, theta2
# Note: Arc is drawn in the unrotated space, so we must rotate the center and handle the arc carefully
# Alternatively, rotate the view: we'll rotate the center and draw the arc.
c1_x, c1_y = rotate_point(1, 2, rotation)
theta_arc1 = Arc((c1_x, c1_y), 1, 2, angle=rotation, theta1=-50, theta2=0, color='black')
ax.add_patch(theta_arc1)
# Label theta for arc 1
t1_angle = np.radians(-10 + rotation)
ax.text(c1_x + 0.7*np.cos(t1_angle), c1_y - 0.50*np.sin(t1_angle), r'$\theta$', ha='center')
# Arc 2: center (0, 2.96), radius 0.45, angle -90 to -110
c2_x, c2_y = rotate_point(0, 2.96, rotation)
theta_arc2 = Arc((c2_x, c2_y), 0.9, 0.9, angle=rotation, theta1=-125, theta2=-90, color='black')
ax.add_patch(theta_arc2)
t2_angle = np.radians(-100 + rotation)
ax.text(c2_x + 0.4*np.cos(t2_angle), c2_y + 0.7*np.sin(t2_angle), r'$\theta$', ha='center')
# --- Waterline ---
# Blue dashed: (0, 2) to (4, 2)
wl_start = rotate_point(0, 2, rotation)
wl_end = rotate_point(4, 2, rotation)
ax.plot([wl_start[0], wl_end[0]], [wl_start[1], wl_end[1]], color='blue', linestyle='--', linewidth=0.7)
# Blue lines: (0, 2) with angles -20 and -20 (with r=-4 for the second)
# Line 1: (0, 2) + 4cm at -20 deg
# Line 2: (0, 2) + 4cm at -20 deg (relative to axis, but TikZ uses ++(-20:-4cm) which is essentially opposite)
p_base = (0, 2)
p_line1 = (p_base[0] + 4*np.cos(np.radians(-20)), p_base[1] + 4*np.sin(np.radians(-20)))
p_line2 = (p_base[0] - 4*np.cos(np.radians(-20)), p_base[1] - 4*np.sin(np.radians(-20)))
ax.plot([rotate_point(*p_base, rotation)[0], rotate_point(*p_line1, rotation)[0]],
[rotate_point(*p_base, rotation)[1], rotate_point(*p_line1, rotation)[1]], color='blue', linewidth=0.7)
ax.plot([rotate_point(*p_base, rotation)[0], rotate_point(*p_line2, rotation)[0]],
[rotate_point(*p_base, rotation)[1], rotate_point(*p_line2, rotation)[1]], color='blue',linewidth=0.7)
# --- Gravity (Fg) ---
fg_center = (0, 1.6)
fg_bullet = rotate_point(*fg_center, rotation)
ax.scatter(*fg_bullet, color='black', s=20)
# Arrow: from (0,1.6) angle 250, length 0.7
fg_end = (fg_center[0] + 0.7*np.cos(np.radians(250)), fg_center[1] + 0.7*np.sin(np.radians(250)))
ax.annotate('', xy=rotate_point(*fg_end, rotation), xytext=fg_bullet,
arrowprops=dict(arrowstyle='->', color='black'))
ax.text(*rotate_point(fg_end[0]-0.3, fg_end[1]+0.2, rotation), '$F_g$', va='center')
ax.text(*rotate_point(fg_end[0]+0.3, fg_end[1]+0.5, rotation), '$G$', va='center')
# --- Buoyancy (Fb) and Metacentre (M) ---
# B point: (-1, 0.6)
b_center = (-1, 0.6)
b_bullet = rotate_point(*b_center, rotation)
ax.scatter(*b_bullet, color='black', s=20)
ax.text(*rotate_point(-1.2, 0.6, rotation), '$B$', ha='right')
# Fb Arrow: angle 70, length 0.7
fb_end = (b_center[0] + 0.7*np.cos(np.radians(70)), b_center[1] + 0.7*np.sin(np.radians(70)))
ax.annotate('', xy=rotate_point(*fb_end, rotation), xytext=b_bullet,
arrowprops=dict(arrowstyle='->', color='black'))
ax.text(*rotate_point(fb_end[0]-0.15, fb_end[1]+0.1, rotation), '$F_b$', ha='center')
# M point and line: dotted from B to M
m_dist = 2.96
m_point = (b_center[0] + m_dist*np.cos(np.radians(70)), b_center[1] + m_dist*np.sin(np.radians(70)))
m_bullet = rotate_point(*m_point, rotation)
ax.plot([b_bullet[0], m_bullet[0]], [b_bullet[1], m_bullet[1]], color='black', linestyle=':')
ax.scatter(*m_bullet, color='black', s=20)
ax.text(*m_bullet, ' $M$', va='bottom')
# --- Creux sur quille (K) ---
ax.text(*rotate_point(0.15, 0.15, rotation), '$K$', ha='center')
ax.scatter(0,0, color='black', s=20)
# --- Center of Rotation (H) ---
h_center = (0, 0.4)
h_circ = rotate_point(*h_center, rotation)
#ax.scatter(*h_circ, marker='o', facecolors='none', edgecolors='black', s=40)
#ax.text(*rotate_point(0.1, 0.4, rotation), '$H$', va='center')
#ax.scatter(*rotate_point(0, 0.6, rotation), color='black', s=10) # node at (0,0.6)
#ax.text(*rotate_point(0, 0.6, rotation), '', ha='center', va='center') # Placeholder to keep dot
# --- Points D, C, E ---
# Orange line: (0, 1.6) to (-0.65, 1.6)
#e_p1 = rotate_point(0, 1.6, rotation)
#e_p2 = rotate_point(-0.65, 1.6, rotation)
#ax.plot([e_p1[0], e_p2[0]], [e_p1[1], e_p2[1]], color='orange', linewidth=2)
#ax.text(*e_p1, ' $E$', va='bottom')
#ax.text(*e_p2, ' $D$', ha='right', va='center')
# Red line: (0, 1.6) angle 160, length 0.6
#c_p1 = rotate_point(0, 1.6, rotation)
#c_p2 = rotate_point(0.6*np.cos(np.radians(160)), 1.6 + 0.6*np.sin(np.radians(160)), rotation)
#ax.plot([c_p1[0], c_p2[0]], [c_p1[1], c_p2[1]], color='red', linewidth=2)
#ax.text(*rotate_point(-0.5, 1.9, rotation), '$C$', ha='right')
# --- Purple Triangle ---
# (0, 0.6), (-1, 0.6), and M_point
#tri_pts = np.array([[0, 0.6], [-1, 0.6], m_point])
#tri_rot = np.array([rotate_point(p[0], p[1], rotation) for p in tri_pts])
#triangle = plt.Polygon(tri_rot, closed=True, fill=False, edgecolor='purple', linestyle='--', linewidth=1)
#ax.add_patch(triangle)
# Final touches
ax.set_aspect('equal')
ax.axis('off')
plt.xlim(-5, 5)
plt.ylim(-2, 7)
#plt.title("Torque moment and metacentre", pad=20)
plt.show()
if __name__ == "__main__":
plot_vessel_diagram()