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. return (xB - xA) * (yP - yA) - (yB - yA) * (xP - xA);
  6. }
  7.  
  8. void czytaj_punkt(double &x, double &y, char nazwa) {
  9. cout << "Podaj wspolrzedne punktu " << nazwa << " (x y): ";
  10. cin >> x >> y;
  11. }
  12.  
  13. bool punkt_w_odcinku(double xA, double yA, double xB, double yB, double xP, double yP) {
  14.  
  15. if (det(xA, yA, xB, yB, xP, yP) != 0)
  16. return false;
  17.  
  18. if (xP >= min(xA, xB) && xP <= max(xA, xB) &&
  19. yP >= min(yA, yB) && yP <= max(yA, yB))
  20. return true;
  21.  
  22. return false;
  23. }
  24.  
  25. int main() {
  26.  
  27. double xA, yA, xB, yB, xP, yP;
  28.  
  29. czytaj_punkt(xA, yA, 'A');
  30. czytaj_punkt(xB, yB, 'B');
  31. czytaj_punkt(xP, yP, 'P');
  32.  
  33. if (punkt_w_odcinku(xA, yA, xB, yB, xP, yP))
  34. cout << "Punkt P nalezy do odcinka AB." << endl;
  35. else
  36. cout << "Punkt P nie nalezy do odcinka AB." << endl;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Podaj wspolrzedne punktu A (x y): Podaj wspolrzedne punktu B (x y): Podaj wspolrzedne punktu P (x y): Punkt P nie nalezy do odcinka AB.