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