I want to set some points in an array to use them for displaying many [circle]s in Gem. I want to check the distance between two subsequent points, and if they're too far apart (particularly further than 0.04), I want to place another point in between, using cubic interpolation, and repeat this until the two subsequent points are close enough.

I'm trying to implement the cubic interpolation equation found here http://paulbourke.net/miscellaneous/interpolation/ (the third chuck of code) in my patch, but it doesn't quite work (most of the times). There's a point that it should stop, but it doesn't. I've attached the patch and two abstractions I'm using in it ([guard_points] and [loop]), if anyone wants to have a look and give some tips. My math is quite limited...