Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. bool on_segment(Point p, Point q, Point r)
  2. {
  3. if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
  4. q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
  5. return true;
  6.  
  7. return false;
  8. }
  9.  
  10. int orientation(Point p, Point q, Point r)
  11. {
  12. int val = (q.y - p.y) * (r.x - q.x) -
  13. (q.x - p.x) * (r.y - q.y);
  14.  
  15. if (val == 0) return 0;
  16.  
  17. return (val > 0)? 1: 2;
  18. }
  19.  
  20. int calculate_intersections(){
  21. int intersections = 0;
  22. for(int i=0; i < number_colliders; i++){
  23. for(int j=i+1; j < number_colliders; j++){
  24. intersections += check_if_intersect(colliders[i], colliders[j]);
  25. }
  26. }
  27. return intersections;
  28. }
  29.  
  30. int check_if_intersect(Collider c1, Collider c2){
  31. if(c1.src == c2.src || c1.tgt == c2.tgt || c1.src == c2.tgt || c1.tgt == c2.src){
  32. return 0;
  33. }
  34.  
  35. int o1, o2, o3, o4;
  36. o1 = check_orientation(c1.src.point, c1.tgt.point, c2.src.point);
  37. o2 = check_orientation(c1.src.point, c1.tgt.point, c2.tgt.point);
  38. o3 = check_orientation(c2.src.point, c2.tgt.point, c1.src.point);
  39. o4 = check_orientation(c2.src.point, c2.tgt.point, c1.tgt.point);
  40.  
  41. if (o1 != o2 && o3 != o4){
  42. return 1;
  43. }
  44.  
  45. if (o1 == 0 && onSegment(c1.src.point, c1.tgt.point, c2.src.point)) return 1;
  46.  
  47. if (o2 == 0 && onSegment(c1.src.point, c1.tgt.point, c2.tgt.point)) return 1;
  48.  
  49. if (o3 == 0 && onSegment(c2.src.point, c2.tgt.point, c1.src.point)) return 1;
  50.  
  51. if (o4 == 0 && onSegment(c2.src.point, c2.tgt.point, c1.tgt.point)) return 1;
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement