fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. double ax, ay, bx, by, px, py;
  9. if (!(cin >> ax >> ay >> bx >> by >> px >> py)) return 0;
  10.  
  11.  
  12. double abx = bx - ax;
  13. double aby = by - ay;
  14. double apx = px - ax;
  15. double apy = py - ay;
  16.  
  17. double dot_product = abx * apx + aby * apy;
  18.  
  19. double distance;
  20.  
  21. if (dot_product < 0) {
  22. distance = sqrt(apx * apx + apy * apy);
  23. } else {
  24. double numerator = abs(aby * px - abx * py + bx * ay - by * ax);
  25. double denominator = sqrt(abx * abx + aby * aby);
  26. distance = numerator / denominator;
  27. }
  28.  
  29. cout << fixed << setprecision(4) << distance << endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty