import matplotlib.pyplot as plt
import numpy as np

r, t, k = 2.0, 1.5, 0.33  # inside radius, thickness, K-factor (drawing units)
flange = 5.0
neutral = r + k * t
sweep = np.linspace(np.pi, 1.5 * np.pi, 100)  # the bend turns the sheet through 90 degrees
sheet_colour, edge_colour, neutral_colour, allowance_colour = "#d9dde3", "#3c4650", "#2f6fb5", "#d9822b"

fig, ax = plt.subplots(figsize=(6.4, 5.2))
# The sheet: a horizontal flange, the bend, and a vertical flange; the bend's centre is the origin.
outline = np.vstack(
    [
        [[flange, -r - t], [0.0, -r - t]],
        np.column_stack([(r + t) * np.cos(sweep[::-1]), (r + t) * np.sin(sweep[::-1])]),
        [[-r - t, 0.0], [-r - t, flange], [-r, flange], [-r, 0.0]],
        np.column_stack([r * np.cos(sweep), r * np.sin(sweep)]),
        [[0.0, -r], [flange, -r]],
    ]
)
ax.fill(outline[:, 0], outline[:, 1], color=sheet_colour, ec=edge_colour, lw=1.4)
# From the bend's centre to where the bend begins and ends.
for x0, y0, x1, y1 in ((0, 0, 0, -r - t), (0, 0, -r - t, 0)):
    ax.plot([x0, x1], [y0, y1], color=edge_colour, lw=0.8, ls=":")
# The neutral layer through the flanges, and its arc across the bend: the bend allowance.
ax.plot([0, flange], [-neutral, -neutral], color=neutral_colour, lw=1.2, ls="--")
ax.plot([-neutral, -neutral], [0, flange], color=neutral_colour, lw=1.2, ls="--")
ax.plot(neutral * np.cos(sweep), neutral * np.sin(sweep), color=allowance_colour, lw=3.0)
ax.annotate(
    "bend allowance\n$BA = \\theta\\,(r + K\\,t)$",
    xy=(neutral * np.cos(1.25 * np.pi), neutral * np.sin(1.25 * np.pi)),
    xytext=(-r - t - 1.6, -r - t - 1.4),
    color=allowance_colour,
    ha="center",
    arrowprops={"arrowstyle": "->", "color": allowance_colour},
)
ax.text(flange + 0.15, -neutral, "neutral layer", color=neutral_colour, va="center")
# Inside radius and bend angle, at the bend's centre.
angle = 1.4 * np.pi
ax.annotate("", xy=(r * np.cos(angle), r * np.sin(angle)), xytext=(0, 0),
            arrowprops={"arrowstyle": "->", "color": edge_colour, "shrinkA": 0, "shrinkB": 0})
ax.text(0.55 * r * np.cos(angle) - 0.45, 0.55 * r * np.sin(angle), "$r$", color=edge_colour)
arc = np.linspace(np.pi, 1.5 * np.pi, 40)
ax.plot(0.7 * np.cos(arc), 0.7 * np.sin(arc), color=edge_colour, lw=0.8)
ax.text(1.0 * np.cos(1.17 * np.pi) - 0.1, 1.0 * np.sin(1.17 * np.pi) - 0.1, "$\\theta$", color=edge_colour)
# Thickness, and the neutral layer's distance from the inside face, on the horizontal flange.
x = 0.7 * flange
ax.annotate("", xy=(x, -r), xytext=(x, -r - t), arrowprops={"arrowstyle": "<->", "color": edge_colour, "shrinkA": 0, "shrinkB": 0})
ax.text(x + 0.12, -r - 0.78 * t, "$t$", color=edge_colour)
x = 0.4 * flange
ax.annotate("", xy=(x, -r), xytext=(x, -neutral), arrowprops={"arrowstyle": "<->", "color": neutral_colour, "shrinkA": 0, "shrinkB": 0})
ax.text(x + 0.2, -r - 0.22 * t, "$K\\,t$", color=neutral_colour, va="center")
ax.text(1.0, -r + 0.35, "inside face", color=edge_colour, fontsize=9)
ax.text(1.0, -r - t - 0.55, "outside face", color=edge_colour, fontsize=9)
ax.set_aspect("equal")
ax.set_xlim(-r - t - 3.4, flange + 2.9)
ax.set_ylim(-r - t - 2.0, flange + 0.2)
ax.axis("off")
fig.tight_layout()