A variational approach to the alternating projections method