The distance function and defect energy