fork(2) download
  1. // A C++ program to check if two given line segments intersect
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct Point
  6. {
  7. int x;
  8. int y;
  9. };
  10.  
  11.  
  12. // To find orientation of ordered triplet (p, q, r).
  13. // The function returns following values
  14. // 0 --> p, q and r are colinear
  15. // 1 --> Clockwise
  16. // 2 --> Counterclockwise
  17. int orientation(Point p, Point q, Point r)
  18. {
  19. // See 10th slides from following link for derivation of the formula
  20. // http://w...content-available-to-author-only...c.uk/~pat/52233/slides/Geometry1x1.pdf
  21. int val = (q.y - p.y) * (r.x - q.x) -
  22. (q.x - p.x) * (r.y - q.y);
  23.  
  24. if (val == 0) return 0; // colinear
  25.  
  26. return (val > 0)? 1: 2; // clock or counterclock wise
  27. }
  28.  
  29. // Driver program to test above functions
  30. int main()
  31. {
  32. struct Point P1 = {-5,-5}, P2 = {0,0}, P3 = {-4,-3};
  33. cout << orientation(P1, P2, P3);
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
2