Constructing distance functions and piecewise quadratic Lyapunov functions for stability of hybrid trajectories