Advertisement
Guest User

Angry Birds with laser beam (easy version)

a guest
Nov 13th, 2021
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include<stdio.h>
  2. //the code is small, its the comments that are making it look bigger, the comments are for better understanding
  3. //using struct for easy passing to functions and easy calculations
  4. struct point{
  5.     // taking long long, because their multiplication may overload in int
  6.     long long int x,y;
  7. };
  8.  
  9.  
  10.  
  11. // To find orientation of ordered triplet (s, e, r).
  12. // The function returns following values
  13. // 0 --> s, e and r are collinear
  14. // 1 --> Clockwise side
  15. // 2 --> Counterclockwise side
  16. int cross_product_sign(point s, point e, point r)
  17. {
  18.     //this formula is for cross product of three points
  19.     long long int val = (e.y - s.y) * (r.x - e.x) -
  20.               (e.x - s.x) * (r.y - e.y);
  21.  
  22.     // since we are only concerned with the sign, we are not returning the values
  23.  
  24.     if (val == 0) return 0;  // collinear
  25.     //though for this problem, they will never be colinear, but we will need this concept for next problem
  26.  
  27.     return (val > 0)? 1: 2; // clock or counterclock wise
  28. }
  29.  
  30. int main() {
  31.     int t,n;
  32.     point pig, bird, s, e;
  33.  
  34.     scanf("%d",&t);
  35.     while(t--){
  36.         scanf("%lld %lld %lld %lld",&s.x,&s.y,&e.x,&e.y);
  37.         scanf("%lld %lld",&pig.x,&pig.y);
  38.         scanf("%lld %lld",&bird.x,&bird.y);
  39.         //you have to pass them in same order for both function calls
  40.         int o1 = cross_product_sign(s,e,pig);
  41.         int o2 = cross_product_sign(s,e,bird);
  42.  
  43.         if(o1!=o2){
  44.             //there sign is not same
  45.             //so on different side
  46.             printf("Lose\n");
  47.         }
  48.         else{
  49.             printf("Win\n");
  50.         }
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement