1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   |  
  public static Point2D nearestColinearPoint(final double x1, final double y1, final double x2, final double y2, double x,        double y)
  {
    final double slope = (y2-y1)/(x2-x1);
    if (!Double.isInfinite(slope)) 
    {
      final double y0 = (y2-slope*x2);
      x = ((y-y0)*slope+x)/(slope*slope+1);
      y = x*slope+y0;
    }
    else x=x2;
 
    if (x1<=x2) 
    {
      if (x<x1) x=x1;
      if (x>x2) x=x2;
    } 
    else 
    {
      if (x>x1) x=x1;
      if (x<x2) x=x2;
    }
 
    if (y1<=y2) 
    {
      if (y<y1) y=y1;
      if (y>y2) y=y2;
    } 
    else 
    {
      if (y>y1) y=y1;
      if (y<y2) y=y2;
    }
    return new Point2D.Double(x,y);
  } | 
Partager