HDU1756 几何+点在多边形内

详见代码

View Code
 1 /*

 2 几何+判定点是否在多边形内

 3 */

 4 #include<stdio.h>

 5 #include<string.h>

 6 #include<stdlib.h>

 7 #include<math.h>

 8 const int maxn = 105;

 9 const double eps = 1e-8;

10 const int inf = 99999999;

11 const int max_x = 1000;

12 const int max_y = 1000;

13 struct point{

14     double x,y;

15 };

16 point p1,p[ maxn ];

17 double xmult( point a,point b,point c ){

18     return ( b.x-a.x )*( c.y-a.y )-( b.y-a.y )*( c.x-a.x );

19 }

20 int zero( double x ){

21     if( x>eps )

22         return 1;

23     if( x<-eps )

24         return -1;

25     if( x<eps&&x>-eps )

26         return 0;

27 }

28 

29 int inPoly( int n ,point p1,point p[] ){

30     int count;

31     int i=0;

32     p[ n ]=p[ 0 ];

33     point p2;

34     while( i<n ){

35         p2.x = rand()+max_x;

36         p2.y = rand()+max_y;//随机选取一个远点

37         for( i=count=0;i<n;i++ ){

38             if( zero( xmult( p1,p[i],p[i+1] ) )==0 && (p[i].x-p1.x)*(p[i+1].x-p1.x)<eps && (p[i].y-p1.y)*(p[i+1].y-p1.y)<eps )

39             return true;//点在边上

40             else if( zero(xmult(p1,p2,p[i]))==0 )

41                 break;//p[i]在p1,p2上

42             else if( xmult( p[i],p[i+1],p1 )*xmult( p[i],p2,p[i+1])>eps&&xmult( p1,p2,p[i] )*xmult( p1,p[i+1],p2 )>eps )

43             count++;//枚举每条边,统计p1,p2和边的相交点的个数

44         }

45     }

46     return count & 1;

47 }

48 

49 int main() {

50     int n;

51     while (scanf("%d", &n) != EOF) {

52         for (int i=0; i<n; ++i) 

53             scanf ("%lf%lf", &p[i].x, &p[i].y);

54         int m;

55         scanf ("%d", &m);

56         while (m--) {

57             scanf ("%lf%lf", &p1.x, &p1.y);

58             if ( inPoly( n,p1,p )==true ) 

59                 printf ("Yes\n");

60             else

61                 printf ("No\n");

62         }

63     }

64     return 0;

65 }

 

你可能感兴趣的:(HDU)