We develop a general method for calculating the thermal efficiency of arbitrary thermodynamic cycles defined in the pressure-volume (p − V ) diagram. To demonstrate how effective our approach is, we calculate the thermal efficiency of ideal gas engines for a few non-trivial cycles in the p − V diagram, including a circular shape, a heart shape, a cycloid of Ceva, and a star-shaped curve. We determine the segments along the cycle where heat is absorbed or released from the heat engine. Our method can be applied to any gas model, and, as an example, we present the results for the van der Waals gas.