fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Punkt {
  5. double x, y;
  6. };
  7.  
  8. // funkcja obliczająca wyznacznik
  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. // funkcja wczytująca punkt
  14. Punkt czytaj_punkt() {
  15. Punkt P;
  16. cout << "Podaj wspolrzedna x punktu P: ";
  17. cin >> P.x;
  18. cout << "Podaj wspolrzedna y punktu P: ";
  19. cin >> P.y;
  20. return P;
  21. }
  22.  
  23. // funkcja sprawdzająca czy punkt P należy do odcinka AB
  24. bool punkt_w_odcinku(Punkt A, Punkt B, Punkt P) {
  25.  
  26. if (det(A, B, P) != 0)
  27. return false;
  28.  
  29. if (P.x >= min(A.x, B.x) && P.x <= max(A.x, B.x) &&
  30. P.y >= min(A.y, B.y) && P.y <= max(A.y, B.y))
  31. return true;
  32.  
  33. return false;
  34. }
  35.  
  36. int main() {
  37. Punkt A = {0, 0};
  38. Punkt B = {2, 2};
  39.  
  40. Punkt P = czytaj_punkt();
  41.  
  42. if (punkt_w_odcinku(A, B, P))
  43. cout << "Punkt P nalezy do odcinka AB." << endl;
  44. else
  45. cout << "Punkt P nie nalezy do odcinka AB." << endl;
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Podaj wspolrzedna x punktu P: Podaj wspolrzedna y punktu P: Punkt P nie nalezy do odcinka AB.