fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double det(double xA, double yA, double xB, double yB, double xP, double yP)
  5. {
  6. return (xB - xA) * (yP - yA) - (yB - yA) * (xP - xA);
  7. }
  8.  
  9. void czytaj_punkt(double &xP, double &yP)
  10. {
  11. cout << "Podaj wspolrzedne punktu P (x y): ";
  12. cin >> xP >> yP;
  13. }
  14.  
  15. bool punkt_w_odcinku(double xA, double yA, double xB, double yB, double xP, double yP)
  16. {
  17. if(det(xA,yA,xB,yB,xP,yP) != 0)
  18. return false;
  19.  
  20. if(xP >= min(xA,xB) && xP <= max(xA,xB) &&
  21. yP >= min(yA,yB) && yP <= max(yA,yB))
  22. return true;
  23.  
  24. return false;
  25. }
  26.  
  27. int main()
  28. {
  29. double xA = 0, yA = 0;
  30. double xB = 2, yB = 2;
  31. double xP, yP;
  32.  
  33. czytaj_punkt(xP, yP);
  34.  
  35. if(punkt_w_odcinku(xA,yA,xB,yB,xP,yP))
  36. cout << "Punkt P nalezy do odcinka AB." << endl;
  37. else
  38. cout << "Punkt P nie nalezy do odcinka AB." << endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 5320KB
stdin
1 1
stdout
Podaj wspolrzedne punktu P (x y): Punkt P nalezy do odcinka AB.