为什么注释掉的地方是错的? 自己的代码好糟烂.....
直接枚举点 判是否在多边形内 加起来求概率 求面积的时候代码写搓了.... 比不过别人两行的代码 而且到现在还找不到错.....
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#define eps 1e-8
#define _sign(x) ((x)>eps?1:((x)<-eps?2:0))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define offset 500
using namespace std;
const int N = 100;
struct point
{double x, y;point(double i, double j){x = i, y = j;}point() {}
} p[N];double xmult(point p1,point p2,point p0)
{return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int inside(point q, int n, point *p)
{int s[3] = {1, 1, 1};for(int i = 0; i < n && s[1]|s[2]; i++)s[_sign(xmult(p[(i+1)%n], q, p[i]))] = 0;return s[1]|s[2];
}
int inside_polygon(point q,int n,point* p)
{point q2;int i=0,count;while (i<n)for (count=i=0,q2.x=rand()+offset,q2.y=rand()+offset; i<n; i++){if (zero(xmult(q,p[i],p[(i+1)%n]))&&(p[i].x-q.x)*(p[(i+1)%n].x-q.x)<eps&&(p[i].y-q.y)*(p[(i+1)%n].y-q.y)<eps)return 1;else if (zero(xmult(q,q2,p[i])))break;else if (xmult(q,p[i],q2)*xmult(q,p[(i+1)%n],q2)<-eps&&xmult(p[i],q,p[(i+1)%n])*xmult(p[i],q2,p[(i+1)%n])<-eps)count++;}return count&1;
}
int main(void)
{double x0,y0,xn,yn;int a,b,n;while(scanf("%lf%lf%lf%lf",&x0,&y0,&xn,&yn) == 4){int _x0 = ceil(x0), _y0 = ceil(y0), _xn = floor(xn), _yn = floor(yn);
// printf("%d %d %d %d",_x0, _y0, _xn, _yn);scanf("%d%d%d",&n,&a,&b);for(int i = 0; i < n; i++){scanf("%lf%lf",&p[i].x,&p[i].y);}double sum = 0;for(int i = _x0; i <= _xn; i++)for(int j = _y0; j <= _yn; j++){if(inside_polygon(point(i, j), n, p)){double r=(min(i+0.5,xn)-max(i-0.5,x0))*(min(j+0.5,yn)-max(j-0.5,y0));sum += r*(i*a+j*b);
// if(i > _x0 && i < _xn && j > _y0 && j < _yn)
// sum += (i*a+j*b);
// else if(i > _x0 && i < _xn && (j == _y0 || j == _yn))
// {
// if(j == _y0)
// sum += (i*a+j*b)*((double)((double)_y0-y0+0.5));
// else
// sum += (i*a+j*b)*((yn-(double)_yn+0.5));
// }
// else if((i == _x0 || i == _xn) && j > _y0 && j < _yn)
// {
// if(i == _x0)
// sum += (i*a+j*b)*(((double)_x0-x0+0.5));
// else
// sum += (i*a+j*b)*((xn-(double)_xn+0.5));
// }
// else
// {
// if(i == _x0 && j == _y0)
// sum += (i*a+j*b)*(((double)_x0-x0+0.5))*(((double)_y0-y0+0.5));
// else if(i == _x0 && j == _yn)
// sum += (i*a+j*b)*(((double)_x0-x0+0.5))*((yn-(double)_yn+0.5));
// else if(i == _xn && j == _y0)
// sum += (i*a+j*b)*((xn-(double)_xn+0.5))*(((double)_y0-y0+0.5));
// else
// sum += (i*a+j*b)*((xn-(double)_xn+0.5))*((yn-(double)_yn+0.5));
// }}}printf("%.3lf\n",(double)sum/(xn-x0)/(yn-y0));}return 0;
}