getLeftSegment and getNearestSegment functions
change demo