what's the bug in this very simple line drawing code?

 
Hi, Today I accidentally find a code by @futurespec, which makes hand drawing a segment line on chart very easy. Thanks @futurespec! It's what I have been dreaming of, for a long time ! I spend a little time to transform this segment line indi into a polygonal line indi. Now it's usable. But wait, there seems to be some bug - if I draw a long polygonal line of dozen of points, there will be random missing segments in between. So I add a print() debug, it turnes out that when a segment is missing, counting is still adding up. Although this code is quite simple, only a few lines. But I just don't know why. Any idea? Please help me. My code: