/// <summary> /// Interpolate a Bezier segment with a given precision /// </summary> /// <param name="i">Index first point of Bezier segment</param> /// <param name="precision">precision</param> /// <param name="points">List of points where to add new points for the segment</param> private void InterpolateSegment(int i, double precision, List <PointD> points) { PointD P0, P1, P2, P3; double maxMedianLengthSqr = Math.Max( ControlPoints[i + 1].Median(ControlPoints[i], ControlPoints[i + 2]).LengthSqr(), ControlPoints[i + 2].Median(ControlPoints[i + 1], ControlPoints[i + 3]).LengthSqr() ); if (maxMedianLengthSqr <= 0.25 * precision * precision) { points.Add(ControlPoints[i + 3]); return; } double cnt = Math.Min((int)Math.Sqrt(Math.Sqrt(maxMedianLengthSqr) / precision) + 3, 1500); double d = 1 / cnt; P0 = ControlPoints[i]; P1 = DeCasteljau(i, d); points.Add(P1); P2 = DeCasteljau(i, 2 * d); points.Add(P2); P3 = DeCasteljau(i, 3 * d); points.Add(P3); P0 = P1.Substract(P0); P1 = P2.Substract(P1); P2 = P3.Substract(P2); // P0 = P1.Substract(P0); P1 = P2.Substract(P1); // P0 = P1.Substract(P0); for (int j = 4; j <= (int)cnt; j++) { P1 = P1.Addition(P0); P2 = P2.Addition(P1); P3 = P3.Addition(P2); points.Add(P3); } }