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, char nazwa) {
  14. cout << "Podaj wspolrzedne punktu " << nazwa << " (x y): ";
  15. cin >> p.x >> p.y;
  16. }
  17.  
  18. bool punkt_w_odcinku(Punkt A, Punkt B, Punkt P) {
  19. if (det(A, B, P) != 0)
  20. return false;
  21.  
  22. if (P.x >= min(A.x, B.x) && P.x <= max(A.x, B.x) &&
  23. P.y >= min(A.y, B.y) && P.y <= max(A.y, B.y))
  24. return true;
  25.  
  26. return false;
  27. }
  28.  
  29. int main() {
  30. Punkt A, B, P;
  31.  
  32. czytaj_punkt(A, 'A');
  33. czytaj_punkt(B, 'B');
  34. czytaj_punkt(P, 'P');
  35.  
  36. if (punkt_w_odcinku(A, B, P))
  37. cout << "Punkt P nalezy do odcinka AB." << endl;
  38. else
  39. cout << "Punkt P nie nalezy do odcinka AB." << endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5280KB
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 nalezy do odcinka AB.