This looks very similar to code in DimensionsOfLine. Can it be consolidated to a separate common function?