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