@@ -357,17 +357,53 @@ private static PointF Translated(PointF p, float translation_x)
357357
358358 private static PointF [ ] DensifyAndConvert ( LocalCS cs , Geometry . Point p0 , Geometry . Point p1 , double length , Geometry . Unit unit )
359359 {
360+ double distNorm = Geometry . NormalizedAngleSigned ( p1 [ 0 ] - p0 [ 0 ] , unit ) ;
361+ bool intersPole = IsAntipodal ( distNorm , unit ) ;
362+ double halfPi = Geometry . HalfAngle ( unit ) / 2 ;
363+ double poleLat = p1 [ 1 ] - p0 [ 1 ] >= 0 ? halfPi : - halfPi ;
364+ int intersPoleIndex = - 1 ;
365+
360366 Geometry . Point [ ] densPoints = Geometry . SphericalDensify ( p0 , p1 , length , unit ) ;
361- PointF [ ] result = new PointF [ densPoints . Length ] ;
362- for ( int j = 0 ; j < densPoints . Length ; ++ j )
367+ PointF [ ] result = new PointF [ densPoints . Length + ( intersPole ? 2 : 0 ) ] ;
368+ int k = 0 ;
369+ for ( int j = 0 ; j < densPoints . Length ; ++ j , ++ k )
363370 {
364371 double densDistNorm = Geometry . NormalizedAngleSigned ( densPoints [ j ] [ 0 ] - p0 [ 0 ] , unit ) ;
365372 densPoints [ j ] [ 0 ] = p0 [ 0 ] + densDistNorm ;
366- result [ j ] = cs . Convert ( densPoints [ j ] ) ;
373+
374+ if ( intersPole
375+ && intersPoleIndex == - 1
376+ && Math . Abs ( densDistNorm ) > halfPi )
377+ {
378+ intersPoleIndex = j ;
379+ Geometry . Point p = j == 0 ? p0 : densPoints [ j - 1 ] ;
380+ float poleF = cs . ConvertY ( poleLat ) ;
381+ result [ k ++ ] = new PointF ( cs . ConvertX ( p [ 0 ] ) , poleF ) ;
382+ result [ k ++ ] = new PointF ( cs . ConvertX ( densPoints [ j ] [ 0 ] ) , poleF ) ;
383+ }
384+
385+ result [ k ] = cs . Convert ( densPoints [ j ] ) ;
367386 }
387+
388+ // last segment
389+ if ( intersPole && intersPoleIndex == - 1 )
390+ {
391+ int j = densPoints . Length ;
392+ intersPoleIndex = j ;
393+ float poleF = cs . ConvertY ( poleLat ) ;
394+ result [ j ] = new PointF ( cs . ConvertX ( densPoints [ j - 1 ] [ 0 ] ) , poleF ) ;
395+ result [ j + 1 ] = new PointF ( cs . ConvertX ( p1 [ 0 ] ) , poleF ) ;
396+ }
397+
368398 return result ;
369399 }
370400
401+ public static bool IsAntipodal ( double distNorm , Geometry . Unit unit )
402+ {
403+ double pi = Geometry . HalfAngle ( unit ) ;
404+ return Math . Abs ( Math . Abs ( distNorm ) - pi ) < double . Epsilon * pi ;
405+ }
406+
371407 // NOTE: This method assumes that the geometry is closed
372408 // It's suitable only for areal geometries
373409 public PointF [ ] AreaPointsF ( Drawer drawer , float translation_x )
0 commit comments