Refactoring computeSegmentHeading() code into a function.