pistHeight := (L,a,`θ`) --> a*cos(`θ`) +
sqrt(L^2 - a^2*sin(`θ`)^2) - (L-a)

(L, a, `θ`) -> (a - L) + a*cos(`θ`) + (L^2 - a^2*sin(`θ`)^2)^(1/2)

plot(
// Blue lines on left and right sides
plot::Line2d([-43,50],[-43,205]), plot::Line2d([43,50],[43,205]),

// Blue line on top
plot::Line2d([-43,205],[43,205]),

// Rectangle (animated for theta = 0 to 2*PI)
plot::Rectangle(-43..43, pistHeight(150,50,th)+100..pistHeight(150,50,th)+110,
th=0..2*PI,Filled=TRUE,FillPattern=Solid,FillColor=RGB::Grey),

//Red line representing connecting rod (animated for theta = 0 to 2*PI)
plot::Line2d([50*sin(th),50*cos(th)],[0,pistHeight(150,50,th)+100],th=0..2*PI,
LineWidth=0.5,Color=RGB::Red,Legend="Connecting Rod"),

// Green line representing crank arm
plot::Line2d([0,0],[50*sin(th),50*cos(th)],th=0..2*PI,LineWidth=0.5,
Color=RGB::Green,Legend="Crank arm"),

// Blue arc that updates with theta
plot::Arc2d(25,[0,0],PI/2-th..PI/2,th=0..2*PI,Legend="Crank angle"),
Scaling=Constrained)

MuPAD graphics