fork(2) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5.  
  6. public static double GetDist(double ax, double ay, double bx, double by, double x, double y)
  7. {
  8. if ((ax-bx)*(x-bx)+(ay-by)*(y-by) <= 0)
  9. return Math.Sqrt((x - bx) * (x - bx) + (y - by) * (y - by));
  10.  
  11. if ((bx-ax)*(x-ax)+(by-ay)*(y-ay) <= 0)
  12. return Math.Sqrt((x - ax) * (x - ax) + (y - ay) * (y - ay));
  13.  
  14. return Math.Abs((by - ay)*x - (bx - ax)*y + bx*ay - by*ax) /
  15. Math.Sqrt((ay - by) * (ay - by) + (ax - bx) * (ax - bx));
  16. }
  17.  
  18. public static void Main()
  19. {
  20. Console.WriteLine(GetDist(0, 2, 2, 0, 0, 0));
  21. Console.WriteLine(GetDist(0, 2, 2, 0, 0, 3));
  22. }
  23. }
Success #stdin #stdout 0s 131520KB
stdin
Standard input is empty
stdout
1.41421356237309
1