[distance] re-factor segment-box code