POJ2451-半平面交

来源:互联网 发布:java中实现杨辉三角 编辑:程序博客网 时间:2024/04/30 08:23

题目: 题目链接

 

题目:就是给出N个半平面,求这N个半平面交

 

分析:O(n^2),初始化坐标系(0,10000)*(0,10000)。然后对每一个半平面cut一次。最后求一次面积即可:

 

代码:

 

#include <iostream>#include <cstdio>#include <string>#include <string.h>#include <map>#include <vector>#include <cstdlib>#include <algorithm>#include <cmath>#include <queue>#include <set>#include <stack>#include <functional>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cassert>#include <bitset>#include <stack>#include <ctime>#include <list>#define INF 0x7fffffff#define max3(a,b,c) (max(a,b)>c?max(a,b):c)#define min3(a,b,c) (min(a,b)<c?min(a,b):c)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;#define N 200#define eps 1e-8struct point{    double x, y;} s;struct node//数组,保存点和大小{    int n;    point p[N];} P;int n;int judge(double a)//判断-1,0,+1{    if(fabs(a) < eps)        return 0;    if(a > 0)        return 1;    else        return -1;}double cross(point a, point b, point c){    double s1 = b.x - a.x;    double t1 = b.y - a.y;    double s2 = c.x - a.x;    double t2 = c.y - a.y;    return s1*t2 - s2*t1;}node cut(node res, point s, point e){    int d1, d2;    int i, t;    point a;    node b;    b.n = 0;    double s1, s2;    res.p[res.n] = res.p[0];    for(i = 0, t = 0; i < res.n; ++i)    {        d1 = judge(s1 = cross(res.p[i], s, e));        d2 = judge(s2 = cross(res.p[i+1], s, e));        if(d1 >= 0)            b.p[t++] = res.p[i];        if(d1 * d2 < 0)        {            a.x = (s2 * res.p[i].x - s1 * res.p[i+1].x)/(s2 - s1);            a.y = (s2 * res.p[i].y - s1 * res.p[i+1].y)/(s2 - s1);            b.p[t++] = a;        }    }    b.n = t;    return b;}double AREAnode(node res){    int i;    double area = 0;    res.p[res.n] = res.p[0];    s.x = s.y = 0;    for(i = 0; i < res.n; ++i)        area += cross(s, res.p[i], res.p[i+1]);    return fabs(area);}int main(){    scanf("%d", &n);    int i;    P.n = 4;//初始化大小和面积    P.p[0].x = 0;    P.p[0].y = 0;    P.p[1].x = 10000;    P.p[1].y = 0;    P.p[2].x = 10000;    P.p[2].y = 10000;    P.p[3].x = 0;    P.p[3].y = 10000;    point a, b;    for(i = 0; i < n; ++i)    {        scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);        P = cut(P, a, b);    }    double area = AREAnode(P) / 2;    printf("%.1lf\n", area);    return 0;}


 

 

 

原创粉丝点击