I haven't looked through your entire script, but it looks to me like you're dividing by zero. try putting line_length = 1; before the line line_end.x = (line_end.x * 50) / line_length; // create segments every 100 quants
and you should not get the first error any more...
~"I never let school interfere with my education"~ -Mark Twain