JavaScript port of FitCurves.c by Philip J. Sneider from "Graphic Gems"