@@ -123,7 +123,25 @@ private static List<PointF> FindDrawingPoints(int curveIndex, PointF[] controlPo
123123 pointList . Add ( left ) ;
124124 pointList . Add ( right ) ;
125125
126- FindDrawingPoints ( curveIndex , 0 , 1 , pointList , 1 , controlPoints , 0 ) ;
126+ // The subdivision test normalizes the difference of two curve points. The curve stays inside the hull of
127+ // its control points, so each component of a difference is at most twice their largest coordinate. The
128+ // squared length of a difference overflows to infinity when its length is more than about 1.8E19.
129+ // Normalize then gives a zero vector, the test always subdivides, and one cubic near float.MaxValue
130+ // flattens to millions of points. A direction does not depend on the length of its vector, so the
131+ // differences of a cubic with coordinates more than 1E18 are scaled down before they are normalized.
132+ // Other cubics use a scale of one, which keeps their direction test unchanged. A difference of two
133+ // coordinates with opposite signs near float.MaxValue overflows before the scale. Its direction is NaN,
134+ // so only the root span of the cubic subdivides there.
135+ int nodeIndex = curveIndex * 3 ;
136+ float extent = 0 ;
137+ for ( int i = nodeIndex ; i < nodeIndex + 4 ; i ++ )
138+ {
139+ extent = MathF . Max ( extent , MathF . Max ( MathF . Abs ( controlPoints [ i ] . X ) , MathF . Abs ( controlPoints [ i ] . Y ) ) ) ;
140+ }
141+
142+ float directionScale = extent > 1E18F ? 1F / extent : 1F ;
143+
144+ FindDrawingPoints ( curveIndex , 0 , 1 , pointList , 1 , controlPoints , directionScale , 0 ) ;
127145
128146 return pointList ;
129147 }
@@ -135,6 +153,7 @@ private static int FindDrawingPoints(
135153 List < PointF > pointList ,
136154 int insertionIndex ,
137155 PointF [ ] controlPoints ,
156+ float directionScale ,
138157 int depth )
139158 {
140159 // max recursive depth for control points, means this is approx the max number of points discoverable
@@ -154,17 +173,17 @@ private static int FindDrawingPoints(
154173 float midT = ( t0 + t1 ) / 2 ;
155174 Vector2 mid = CalculateBezierPoint ( curveIndex , midT , controlPoints ) ;
156175
157- var leftDirection = Vector2 . Normalize ( left - mid ) ;
158- var rightDirection = Vector2 . Normalize ( right - mid ) ;
176+ var leftDirection = Vector2 . Normalize ( ( left - mid ) * directionScale ) ;
177+ var rightDirection = Vector2 . Normalize ( ( right - mid ) * directionScale ) ;
159178
160179 if ( Vector2 . Dot ( leftDirection , rightDirection ) > DivisionThreshold || Math . Abs ( midT - 0.5f ) < 0.0001f )
161180 {
162181 int pointsAddedCount = 0 ;
163182
164- pointsAddedCount += FindDrawingPoints ( curveIndex , t0 , midT , pointList , insertionIndex , controlPoints , depth + 1 ) ;
183+ pointsAddedCount += FindDrawingPoints ( curveIndex , t0 , midT , pointList , insertionIndex , controlPoints , directionScale , depth + 1 ) ;
165184 pointList . Insert ( insertionIndex + pointsAddedCount , mid ) ;
166185 pointsAddedCount ++ ;
167- pointsAddedCount += FindDrawingPoints ( curveIndex , midT , t1 , pointList , insertionIndex + pointsAddedCount , controlPoints , depth + 1 ) ;
186+ pointsAddedCount += FindDrawingPoints ( curveIndex , midT , t1 , pointList , insertionIndex + pointsAddedCount , controlPoints , directionScale , depth + 1 ) ;
168187
169188 return pointsAddedCount ;
170189 }
0 commit comments