HDU2528 几何+线段交点

题意:

给定一个凸多边形和一条直线

求这个多边形被切割后的面积

对于代码中的

d1=dblcmp(s1=cross( p.node[i],s,e ));//跨立
d2=dblcmp(s2=cross( p.node[i+1],s,e ));//跨立

不是很理解。。。求指教

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 #include<algorithm>
 6 using namespace std;
 7 const double eps=1e-8;
 8 const int maxn = 505;
 9 struct point{
10     double x,y;
11 };
12 struct ploy{
13     point node[ maxn ];
14     int n;
15 };
16 point left,right,s;
17 ploy p,p1,p2;
18 
19 double cross( point a,point b,point c ){
20     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
21 }
22 
23 double ploy_area( ploy res ){
24     double ans=0;
25     res.node[ res.n ]=res.node[0];
26     for( int i=0;i<res.n;i++ ){
27         ans+=cross( s,res.node[i],res.node[i+1] );
28     }
29     return ans;
30 }
31 
32 int dblcmp(double a) {return a<-eps?-1:a>eps?1:0;}
33 
34 ploy cut( ploy p,point s,point e ){
35     point tmp;
36     ploy bb;
37     int cnt=0;
38     for( int i=0;i<p.n;i++ ){
39         int d1,d2;
40         double s1,s2;
41         d1=dblcmp(s1=cross( p.node[i],s,e ));//跨立
42         d2=dblcmp(s2=cross( p.node[i+1],s,e ));//跨立
43         if( d1>=0 ){
44             bb.node[ cnt ]=p.node[ i ];
45             cnt++;
46         }
47         if( d1*d2<0 ){
48             tmp.x=(s2*p.node[i].x-s1*p.node[i+1].x)/(s2-s1);
49             tmp.y=(s2*p.node[i].y-s1*p.node[i+1].y)/(s2-s1);
50             bb.node[ cnt ]=tmp;
51             cnt++;
52         }
53     }
54     bb.n=cnt;
55     bb.node[ cnt ]=bb.node[ 0 ];
56     return bb;
57 }
58 
59 int main(){
60     while( scanf("%d",&p.n),p.n ){
61         for( int i=0;i<p.n;i++ ){
62             scanf("%lf%lf",&p.node[ i ].x,&p.node[ i ].y);
63         }
64         p.node[ p.n ]=p.node[ 0 ];
65         scanf("%lf%lf%lf%lf",&left.x,&left.y,&right.x,&right.y);
66         s.x=s.y=0;
67         p1=cut( p,left,right );
68         p2=cut( p,right,left );
69         int res1,res2;
70         res1=int(fabs(ploy_area( p1 ))/2+0.5);
71         res2=int(fabs(ploy_area( p2 ))/2+0.5);
72         printf("%d %d\n",res1>res2?res1:res2,res1>res2?res2:res1);
73     }
74     return 0;
75 }

 

posted @ 2013-02-13 16:22  xxx0624  阅读(597)  评论(0编辑  收藏  举报