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