POJ 半平面交 模板题 三枚

POJ 3335 Rotating Scoreboard

 注意:题目给出的点集默认是顺时针的。

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct point
{
    double x, y;
}p[105], tmp[105], q[105];

double a, b, c;

void get_line(point p1, point p2)
{
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x * p1.y - p2.y * p1.x;
}

point intersect(point p1, point p2)
{
    double u = fabs(a * p1.x + b * p1.y + c);
    double v = fabs(a * p2.x + b * p2.y + c);
    point ret;
    ret.x = (v * p1.x + u * p2.x) / (u + v);
    ret.y = (v * p1.y + u * p2.y) / (u + v);
    return ret;
}

int n, m;

void cut()
{
    int i, tm = 0;
    for(i = 1; i <= m; i++)
    {
        if(a * q[i].x + b * q[i].y + c >= 0)
            tmp[++tm] = q[i];
        else
        {
            if(a * q[i-1].x + b * q[i-1].y + c > 0)
                tmp[++tm] = intersect(q[i-1], q[i]);
            if(a * q[i+1].x + b * q[i+1].y + c > 0)
                tmp[++tm] = intersect(q[i], q[i+1]);
        }
    }
    for(i = 1; i <= tm; i++)
        q[i] = tmp[i];
    q[0] = q[tm];
    q[tm+1] = q[1];
    m = tm;
}

void solve()
{
    int i, j;
    for(i = 1; i <= n; i++)
        q[i] = p[i];
    q[0] = q[n];
    q[n+1] = q[1];
    p[n+1] = p[1];
    m = n;
    for(i = 1; i <= n; i++)
    {
        get_line(p[i], p[i+1]);
        cut();
    }
}

int main()
{
    int i, j, cas;
    scanf("%d", &cas);
    while( cas--)
    {
        scanf("%d", &n);
        for(i = 1; i <= n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);

        solve();
        puts(m ? "YES" : "NO");
    }
    return 0;
}

 

POJ 3130 How I Mathematician Wonder What You Are!

注意:题目给出的点集默认是逆时针的。

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct point
{
    double x, y;
}p[105], tmp[105], q[105];

double a, b, c;

void get_line(point p1, point p2)
{
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x * p1.y - p2.y * p1.x;
}

point intersect(point p1, point p2)
{
    double u = fabs(a * p1.x + b * p1.y + c);
    double v = fabs(a * p2.x + b * p2.y + c);
    point ret;
    ret.x = (v * p1.x + u * p2.x) / (u + v);
    ret.y = (v * p1.y + u * p2.y) / (u + v);
    return ret;
}

int n, m;

void cut()
{
    int i, tm = 0;
    for(i = 1; i <= m; i++)
    {
        if(a * q[i].x + b * q[i].y + c <= 0)
            tmp[++tm] = q[i];
        else
        {
            if(a * q[i-1].x + b * q[i-1].y + c < 0)
                tmp[++tm] = intersect(q[i-1], q[i]);
            if(a * q[i+1].x + b * q[i+1].y + c < 0)
                tmp[++tm] = intersect(q[i], q[i+1]);
        }
    }
    for(i = 1; i <= tm; i++)
        q[i] = tmp[i];
    q[0] = q[tm];
    q[tm+1] = q[1];
    m = tm;
}

void solve()
{
    int i, j;
    for(i = 1; i <= n; i++)
        q[i] = p[i];
    q[0] = q[n];
    q[n+1] = q[1];
    p[n+1] = p[1];
    m = n;
    for(i = 1; i <= n; i++)
    {
        get_line(p[i], p[i+1]);
        cut();
    }
}

int main()
{
    int i, j;
    while( ~scanf("%d", &n) && n)
    {
        for(i = 1; i <= n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        solve();
        puts(m ? "1" : "0");
    }
    return 0;
}

 

POJ 1474 Video Surveillance

注意:题目给出的点集默认是顺时针的。

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define eps 1e-8

struct point
{
    double x, y;
}p[105], tmp[105], q[105];



double a, b, c;

void get_line(point p1, point p2)
{
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x * p1.y - p2.y * p1.x;
}

point intersect(point p1, point p2) 
{
    double u = fabs(a * p1.x + b * p1.y + c);
    double v = fabs(a * p2.x + b * p2.y + c);
    point ret;
    ret.x = (p1.x * v + p2.x * u) / (u + v);
    ret.y = (p1.y * v + p2.y * u) / (u + v);
    return ret;
}

int n, m;

void cut() 
{
    int tm = 0;
    int i;
    for(i = 1; i <= m; i++)
    {
        if(a * q[i].x + b * q[i].y + c >= 0)   
            tmp[++tm] = q[i];                    
        else
        {
            if(a * q[i-1].x + b * q[i-1].y + c > 0) 
                tmp[++tm] = intersect(q[i-1], q[i]);  

            if(a * q[i+1].x + b * q[i+1].y + c > 0)
                tmp[++tm] = intersect(q[i], q[i+1]);
        }
    }
    for(i = 1; i <= tm; i++)
        q[i] = tmp[i];
    q[0] = tmp[tm];
    q[tm + 1] = tmp[1];
    m = tm;
}

void solve()
{
    int i;
    for(i = 1; i <= n; i++)
        q[i] = p[i];
    p[n+1] = p[1];
    q[0] = q[n];
    q[n+1] = q[1];
    m = n;
    for(i = 1; i <= n; i++)
    {
        get_line(p[i], p[i+1]); 
        cut();                 
    }
}

int main()
{
    int i, ca = 1;
    while(~scanf("%d",&n) && n)
    {
        for(i = 1;i <= n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        solve();
        printf("Floor #%d\nSurveillance is ", ca++);
        puts(m  ? "possible." : "impossible.");
        puts("");
    }
    return 0;
}
posted @ 2012-09-03 22:11  To be an ACMan  Views(415)  Comments(0)    收藏  举报