import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, FancyArrowPatch, Arc
import matplotlib.transforms as transforms
fig, ax = plt.subplots(figsize=(7, 3))
# Colors
beam_color = '#cccccc'
deformed_color = '#e0e0e0'
arrow_color = '#008fe0'
# Beam dimensions
L = 1.0
beam_height = 0.08
# Draw undeformed beam (dashed outline)
ax.plot([0, L], [-beam_height/2, -beam_height/2], 'k--', linewidth=1, alpha=0.5)
ax.plot([0, L], [beam_height/2, beam_height/2], 'k--', linewidth=1, alpha=0.5)
ax.plot([0, 0], [-beam_height/2, beam_height/2], 'k--', linewidth=1, alpha=0.5)
ax.plot([L, L], [-beam_height/2, beam_height/2], 'k--', linewidth=1, alpha=0.5)
ax.text(0.5, beam_height/2 + 0.03, 'undeformed', fontsize=9, ha='center', style='italic', alpha=0.7)
# Draw deformed beam (shifted and deflected downward with z pointing down)
u_val = 0.08 # axial displacement
w_val = 0.12 # deflection (positive = downward in z-down convention)
# Deformed beam - simple representation with curved axis
x_def = np.linspace(0, L, 50)
# Cubic deflection shape (negative in plot since z points down but plot y points up)
w_def = -w_val * (3*(x_def/L)**2 - 2*(x_def/L)**3)
u_def = u_val * x_def/L
# Draw deformed beam outline (approximate)
ax.fill_between(x_def + u_def, w_def - beam_height/2, w_def + beam_height/2,
facecolor=deformed_color, edgecolor='black', linewidth=1.5, alpha=0.8)
# Draw beam axis (deformed)
ax.plot(x_def + u_def, w_def, 'k-', linewidth=1)
# Mark a specific cross-section location
x_cs = 0.7
idx = int(x_cs/L * 49)
x_cs_def = x_cs + u_def[idx]
w_cs_def = w_def[idx]
# Calculate local rotation at this point (slope in plot coordinates)
dw_dx_plot = -w_val * (6*x_cs/L - 6*(x_cs/L)**2) / L
phi_local = np.arctan(dw_dx_plot)
# Draw the rotated cross-section
cs_length = 0.12
dx = -cs_length/2 * np.sin(phi_local)
dz = cs_length/2 * np.cos(phi_local)
ax.plot([x_cs_def - dx, x_cs_def + dx], [w_cs_def - dz, w_cs_def + dz],
'b-', linewidth=3)
# Draw original cross-section position (vertical line)
ax.plot([x_cs, x_cs], [-cs_length/2, cs_length/2], 'b--', linewidth=2, alpha=0.5)
# Arrow for u (axial displacement)
ax.annotate('', xy=(x_cs_def, 0.08), xytext=(x_cs, 0.08),
arrowprops=dict(arrowstyle='->', color=arrow_color, lw=2))
ax.text((x_cs + x_cs_def)/2, 0.11, r'$u$', fontsize=14, ha='center', color=arrow_color)
# Arrow for w (deflection - pointing down since z is down)
ax.annotate('', xy=(x_cs + 0.12, w_cs_def), xytext=(x_cs + 0.12, 0),
arrowprops=dict(arrowstyle='->', color=arrow_color, lw=2))
ax.text(x_cs + 0.15, w_cs_def/2, r'$w$', fontsize=14, ha='left', color=arrow_color)
# Arc for rotation phi (clockwise rotation for downward deflection)
arc_radius = 0.08
arc = Arc((x_cs_def, w_cs_def), 2*arc_radius, 2*arc_radius,
angle=0, theta1=90 + np.degrees(phi_local), theta2=90,
color=arrow_color, linewidth=2)
ax.add_patch(arc)
ax.text(x_cs_def - 0.02, w_cs_def - arc_radius - 0.02, r'$\varphi$',
fontsize=14, ha='center', va='top', color=arrow_color)
# Coordinate axes
ax.annotate('', xy=(1.25, 0), xytext=(-0.05, 0),
arrowprops=dict(arrowstyle='->', color='black', lw=1.5))
ax.text(1.28, 0, r'$x$', fontsize=12, va='center')
ax.annotate('', xy=(-0.05, -0.22), xytext=(-0.05, 0),
arrowprops=dict(arrowstyle='->', color='black', lw=1.5))
ax.text(-0.03, -0.25, r'$z$', fontsize=12, ha='left')
ax.set_xlim(-0.15, 1.4)
ax.set_ylim(-0.3, 0.18)
ax.set_aspect('equal')
ax.axis('off')
plt.tight_layout()
plt.show()