protected Vec4 bezierNearestPointToSegment(Vec4 p0,Vec4 p1,Vec4[] controlPoints,int[] coefficients,double tolerance){
  double dist1;
  double dist2;
  double t1=0.0;
  double t2=1.0;
  Vec4 p=TacticalGraphicUtil.bezierCurve(controlPoints,t1,coefficients);
  Vec4 nearest=Line.nearestPointOnSegment(p0,p1,p);
  dist1=nearest.distanceTo3(p);
  double delta;
  do {
    p=TacticalGraphicUtil.bezierCurve(controlPoints,t2,coefficients);
    nearest=Line.nearestPointOnSegment(p0,p1,p);
    dist2=nearest.distanceTo3(p);
    double avg=(t1 + t2) / 2;
    delta=Math.abs(dist1 - dist2);
    if (dist2 < dist1) {
      t1=t2;
      dist1=dist2;
    }
    t2=avg;
  }
 while (delta > tolerance);
  return p;
}

protected Vec4 bezierNearestPointToSegment ( Vec4 p0 , Vec4 p1 , Vec4 [ ] controlPoints , int [ ] coefficients , double tolerance ) { double dist1 ; double dist2 ; double t1 = 0.0 ; double t2 = 1.0 ; Vec4 p = TacticalGraphicUtil . bezierCurve ( controlPoints , t1 , coefficients ) ; Vec4 nearest = Line . nearestPointOnSegment ( p0 , p1 , p ) ; dist1 = nearest . distanceTo3 ( p ) ; double delta ; do { p = TacticalGraphicUtil . bezierCurve ( controlPoints , t2 , coefficients ) ; nearest = Line . nearestPointOnSegment ( p0 , p1 , p ) ; dist2 = nearest . distanceTo3 ( p ) ; double avg = ( t1 + t2 ) / 2 ; delta = Math . abs ( dist1 - dist2 ) ; if ( dist2 < dist1 ) { t1 = t2 ; dist1 = dist2 ; } t2 = avg ; } while ( delta > tolerance ) ; return p ; }
determine the point along a bezier curve that is closest to a line segment .
