Engine piston simulator
A crank and connecting rod turn the piston's up-and-down into spin. Each of the four strokes (intake, compression, power, exhaust) takes half a turn, so the crank turns twice for every power stroke.
The formula
x = r cos θ + √(l² − r² sin² θ)
x is the distance from the crankshaft to the piston pin, θ the crank angle from the top. With an endlessly long rod this would be a pure sine wave (x = l + r cos θ). A real rod tilts by up to arcsin(r/l), which pulls the piston down further near the top and less near the bottom. So the piston moves faster through the top half and lingers in the bottom half. Make the rod shorter to see the effect grow.
Valve timing here is simplified: each valve opens for exactly half a turn. Real engines open valves early and close them late to help the gas flow.
Tested: the rod keeps its length and the piston stays on the cylinder axis, stroke = 2r, the speed formula matches the numerical derivative, and the piston is past mid-stroke before 90° (packages/mechanics/test/crank.test.ts).
Related: Torque to horsepower · Four-bar linkage · Cam and follower