@@ -173,7 +173,18 @@ private static PointF[] FlattenCurve(PointF[] controlPoints, Vector2 scale)
173173 output . Add ( ( PointF ) p0 ) ;
174174 }
175175
176- SubdivideAndAppend ( 0F , 1F , p0 , p3 , p0 , p1 , p2 , p3 , ref output , 0 ) ;
176+ // The flatness test compares the curve midpoint with the chord midpoint. Both are sums of products of
177+ // the control points, so in single precision each carries a rounding error of a few units in the last
178+ // place of the largest coordinate m (one unit is at most m * 2^-23). The measured deviation can then be
179+ // wrong by about m * 2^-21, which is more than the constant tolerance when m is more than 2^15. With a
180+ // tolerance less than this error, flat spans fail the test at almost every depth and the subdivision runs
181+ // to its depth limit, so the tolerance never goes below the error. Up to m = 32768 the constant tolerance
182+ // applies unchanged. When the squared error overflows to infinity, every span after the root is accepted.
183+ Vector2 extent = Vector2 . Max ( Vector2 . Max ( Vector2 . Abs ( p0 ) , Vector2 . Abs ( p1 ) ) , Vector2 . Max ( Vector2 . Abs ( p2 ) , Vector2 . Abs ( p3 ) ) ) ;
184+ float roundingError = MathF . Max ( extent . X , extent . Y ) * ( 1F / ( 1 << 21 ) ) ;
185+ float flatnessSqrTolerance = MathF . Max ( FlatnessSqrTolerance , roundingError * roundingError ) ;
186+
187+ SubdivideAndAppend ( 0F , 1F , p0 , p3 , p0 , p1 , p2 , p3 , flatnessSqrTolerance , ref output , 0 ) ;
177188 output . Add ( ( PointF ) p3 ) ;
178189 }
179190
@@ -191,6 +202,7 @@ private static PointF[] FlattenCurve(PointF[] controlPoints, Vector2 scale)
191202 /// <param name="p1">The first control point of the cubic.</param>
192203 /// <param name="p2">The second control point of the cubic.</param>
193204 /// <param name="p3">The end point of the cubic.</param>
205+ /// <param name="flatnessSqrTolerance">The squared midpoint-to-chord error accepted for this cubic.</param>
194206 /// <param name="output">The builder receiving the appended points.</param>
195207 /// <param name="depth">The current recursion depth; used to bound the subdivision.</param>
196208 private static void SubdivideAndAppend (
@@ -202,6 +214,7 @@ private static void SubdivideAndAppend(
202214 Vector2 p1 ,
203215 Vector2 p2 ,
204216 Vector2 p3 ,
217+ float flatnessSqrTolerance ,
205218 ref FlattenedPointBuilder output ,
206219 int depth )
207220 {
@@ -218,15 +231,17 @@ private static void SubdivideAndAppend(
218231 // Compare the curve midpoint with the midpoint of the candidate line. An S-shaped cubic
219232 // can cross its chord at the root midpoint while still bending on both sides. Subdivide the
220233 // root once before trusting this test so the two bends are tested separately.
234+ // A point that is not finite gives a NaN deviation or tolerance. Such a span cannot be placed,
235+ // so the negated comparison accepts it instead of subdividing it to the depth limit.
221236 Vector2 chordMid = ( left + right ) * 0.5F ;
222- if ( depth > 0 && ( mid - chordMid ) . LengthSquared ( ) <= FlatnessSqrTolerance )
237+ if ( depth > 0 && ! ( ( mid - chordMid ) . LengthSquared ( ) > flatnessSqrTolerance ) )
223238 {
224239 return ;
225240 }
226241
227- SubdivideAndAppend ( t0 , midT , left , mid , p0 , p1 , p2 , p3 , ref output , depth + 1 ) ;
242+ SubdivideAndAppend ( t0 , midT , left , mid , p0 , p1 , p2 , p3 , flatnessSqrTolerance , ref output , depth + 1 ) ;
228243 output . Add ( ( PointF ) mid ) ;
229- SubdivideAndAppend ( midT , t1 , mid , right , p0 , p1 , p2 , p3 , ref output , depth + 1 ) ;
244+ SubdivideAndAppend ( midT , t1 , mid , right , p0 , p1 , p2 , p3 , flatnessSqrTolerance , ref output , depth + 1 ) ;
230245 }
231246
232247 /// <summary>
0 commit comments