Measurements of the azimuthal anisotropy of prompt and nonprompt charmonia in PbPb collisions at $\sqrt{s_\mathrm{NN}}$ = 5.02 TeV