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