Guest User

point and polygon

a guest
Aug 21st, 2013
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. struct pt {
  8.     int x,y;
  9. } a[111111],p;
  10.  
  11. int n;
  12.  
  13. int det ( pt a, pt b, pt c )
  14. {
  15.     return (b.x - a.x) * (c.y - b.y) - (c.x - b.x ) * (b.y - a.y );
  16. }
  17. bool intersect( pt a,pt b,pt c,pt d )
  18. {
  19.     return det(a,b,c) * det(a,b,d) <= 0 && det(c,d,a) * det(c,d,b) < 0;
  20. }
  21. int main()
  22. {
  23.     scanf("%d",&n);
  24.     for ( int i = 1; i <= n; i++ )
  25.         scanf("%d%d",&a[i].x,&a[i].y);
  26.     scanf("%d%d",&p.x,&p.y);
  27.    
  28.     if ( det(a[1],a[n],p) > 0 || det(a[1],a[2],p) < 0 ) { puts("NO"); return 0; }
  29.     int l = 2, r = n;
  30.     while ( r - l > 1 ) {
  31.         int m = (r + l) / 2;
  32.         if ( det(a[1],a[m],p) >= 0 ) l = m;
  33.             else r = m;
  34.    
  35.     }
  36.     if ( intersect(a[1],p,a[l],a[r]) ) puts("NO"); 
  37.         else puts("YES");
  38. }
Advertisement
Add Comment
Please, Sign In to add comment