codex/cardiac-offset-slice-foreshortening.py

cardiac-offset-slice-foreshortening.py
"""Central versus parallel offset sections of an ideal ellipsoid.
Python 3.14 / Matplotlib 3.10. Run from _media; output PNG in cwd.
The ellipsoid is (x*x+y*y)/b**2+z*z/a**2=1, a=5 cm, b=2 cm.
At y=0.6*b, both section semiaxes shrink by sqrt(1-0.6**2)=0.8.
"""
from pathlib import Path
from math import sqrt
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

a, b, offset_fraction = 5., 2., .6
scale = sqrt(1-offset_fraction**2)
fig, axes = plt.subplots(1, 2, figsize=(6.8, 4.4), dpi=100, facecolor='white')
for ax in axes:
    ax.set(xlim=(-2.8, 3.2), ylim=(-6.1, 6.2), aspect='equal')
    ax.axis('off')
axes[0].add_patch(Ellipse((0, 0), 2*b, 2*a, facecolor='#eeeeee', edgecolor='#333333', lw=2))
axes[0].annotate('', xy=(2.35, 5), xytext=(2.35, -5), arrowprops=dict(arrowstyle='<->', color='#333333'))
axes[0].text(2.48, 0, '10 cm', rotation=90, va='center', fontsize=10)
axes[0].text(0, 5.65, 'Central section: y = 0', ha='center', fontsize=10)
axes[0].plot([0, 0], [-5, 5], 'o', color='#333333', ms=4)
axes[1].add_patch(Ellipse((0, 0), 2*b, 2*a, facecolor='none', edgecolor='#aaaaaa', ls='--', lw=1.3))
axes[1].add_patch(Ellipse((0, 0), 2*b*scale, 2*a*scale, facecolor='#d9e9f5', edgecolor='#1766a5', lw=2))
axes[1].annotate('', xy=(2.0, 4), xytext=(2.0, -4), arrowprops=dict(arrowstyle='<->', color='#1766a5'))
axes[1].text(2.13, 0, '8 cm', rotation=90, va='center', fontsize=10, color='#1766a5')
axes[1].text(0, 5.65, 'Offset section: y = 0.6 b', ha='center', fontsize=10)
axes[1].plot([0, 0], [-4, 4], 'o', color='#1766a5', ms=4)
axes[1].annotate('Apparent tip', xy=(0, 4), xytext=(-2.5, 4.7), fontsize=9,
                 arrowprops=dict(arrowstyle='->', color='#1766a5'))
fig.suptitle('A parallel slice can miss the true apex', fontsize=13)
fig.text(.5, .035, 'Ideal ellipsoid sections; synthetic geometry, not patient images', ha='center', fontsize=10)
fig.subplots_adjust(left=.02, right=.98, bottom=.10, top=.88, wspace=.10)
fig.savefig(Path('cardiac-offset-slice-foreshortening.png'), facecolor='white', dpi=100)
plt.close(fig)