@ Pure public Point1d getNearestPosition ( Point2D < ? , ? > pos ) { Point2d pp = null ; final Vector2d v = new Vector2d ( ) ; double currentPosition = 0. ; double bestPosition = Double . NaN ; double bestDistance = Double . POSITIVE_INFINITY ; for ( final Point2d p : points ( ) ) { if ( pp != null ) { double position = Segment2afp . findsProjectedPointPointLine ( pos . getX ( ) , pos . getY ( ) , pp . getX ( ) , pp . getY ( ) , p . getX ( ) , p . getY ( ) ) ; if ( position < 0. ) { position = 0. ; } if ( position > 1. ) { position = 1. ; } v . sub ( p , pp ) ; final double t = v . getLength ( ) ; v . scale ( position ) ; final double dist = Point2D . getDistanceSquaredPointPoint ( pos . getX ( ) , pos . getY ( ) , pp . getX ( ) + v . getX ( ) , pp . getY ( ) + v . getY ( ) ) ; if ( dist < bestDistance ) { bestDistance = dist ; bestPosition = currentPosition + v . getLength ( ) ; } currentPosition += t ; } pp = p ; } if ( Double . isNaN ( bestPosition ) ) { return null ; } return new Point1d ( toSegment1D ( ) , bestPosition , 0. ) ; }
Return the nearest point 1 . 5D from a 2D position .