POJ3384 Feng Shui 【半平面交+向内缩进】

本文介绍了一种使用向内缩进和半平面交模板解决几何问题的方法,通过调整模板提高了计算精度。文章分享了具体的C++代码实现,包括点、线的运算,半平面交算法及向内缩进过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是 向内缩进+半平面交模板
一开始WA哭了,换了个kuangbin的板子就过了(感谢。。
感觉向内缩进的那个模板 直接算的 比用三角函数的精度好一点……(大概?)

(然后这题代码写的很辣鸡)

代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 20000;
const double eps = 1e-12;
const double PI = acos(-1.0);
int dcmp(double x) {
    if (fabs(x) < eps) return 0;
    return x < 0? -1: 1;
}
struct Point{
    double x,y;
    Point(double x=0.0,double y=0.0):x(x),y(y){}
    void Read(){ scanf("%lf%lf",&x,&y); }
    void Write(){ printf("%.10f %.10f\n",x,y); }
    bool operator<(const Point &b) const{//水平序
        return !dcmp(x-b.x)? y<b.y:x<b.x;
    }
    
}p[maxn], pol[maxn];
Point operator +(Point a,Point b){ return Point(a.x+b.x,a.y+b.y); }
Point operator -(Point a,Point b){ return Point(a.x-b.x,a.y-b.y); }
Point operator *(Point a,double k){ return Point(k*a.x,k*a.y); }
Point operator *(double k,Point a){ return Point(k*a.x,k*a.y); }
double operator *(Point a,Point b){ return a.x*b.x+a.y*b.y; }
Point operator /(Point a,double k){ return Point(a.x/k,a.y/k); }
double operator ^(Point a,Point b){ return a.x*b.y-a.y*b.x; }
double cross(Point a,Point b){ return a.x*b.y-a.y*b.x; }
double cross(Point a,Point b,Point c){ return cross(b-a,c-a); }
double dot(Point a,Point b){ return a.x*b.x+a.y*b.y; }
double dot(Point a,Point b,Point c){ return dot(b-a,c-a); }
double length(Point p){ return sqrt(p*p); }
Point unit(Point p){ return 1.0/length(p)*p; }

Point rotateV(Point a,Point b,double ang){
    Point p=b-a;
    return unit(Point(p.x*cos(ang)-p.y*sin(ang),p.x*sin(ang)+p.y*cos(ang)));
}

struct Line
{
    Point s,e;
    double k;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s; e = _e;
        k = atan2(e.y - s.y,e.x - s.x);
    }
    Point operator &(const Line &b)const
    {
        Point res = s;
        double t = ((s - b.s)^(b.s - b.e))/((s - e)^(b.s - b.e));
        res.x += (e.x - s.x)*t;
        res.y += (e.y - s.y)*t;
        return res;
    }
};
//半平面交,直线的左边代表有效区域
bool HPIcmp(Line a,Line b)
{
    if(fabs(a.k - b.k) > eps)return a.k < b.k;
    return ((a.s - b.s)^(b.e - b.s)) < 0;
}
Line Q[1010];
void HPI(Line line[], int n, Point res[], int &resn)
{
    int tot = n;
    sort(line,line+n,HPIcmp);
    tot = 1;
    for(int i = 1;i < n;i++)
        if(fabs(line[i].k - line[i-1].k) > eps)
            line[tot++] = line[i];
    int head = 0, tail = 1;
    Q[0] = line[0];
    Q[1] = line[1];
    resn = 0;
    for(int i = 2; i < tot; i++)
    {
        if(fabs((Q[tail].e-Q[tail].s)^(Q[tail-1].e-Q[tail-1].s)) < eps || fabs((Q[head].e-Q[head].s)^(Q[head+1].e-Q[head+1].s)) < eps)
            return;
        while(head < tail && (((Q[tail]&Q[tail-1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
            tail--;
        while(head < tail && (((Q[head]&Q[head+1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
            head++;
        Q[++tail] = line[i];
    }
    while(head < tail && (((Q[tail]&Q[tail-1]) - Q[head].s)^(Q[head].e-Q[head].s)) > eps)
        tail--;
    while(head < tail && (((Q[head]&Q[head-1]) - Q[tail].s)^(Q[tail].e-Q[tail].e)) > eps)
        head++;
    if(tail <= head + 1)return;
    for(int i = head; i < tail; i++)
        res[resn++] = Q[i]&Q[i+1];
    if(head < tail - 1)
        res[resn++] = Q[head]&Q[tail];
}
Line line[1010];
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
void change(Point a,Point b,Point &c,Point &d,double p)//将线段ab往左移动距离p
{
    double len = dist(a,b);
    double dx = (a.y - b.y)*p/len;
    double dy = (b.x - a.x)*p/len;
    c.x = a.x + dx; c.y = a.y + dy;
    d.x = b.x + dx; d.y = b.y + dy;
}

int n;
double r;
int main() {
    while (~scanf("%d%lf", &n, &r)) {
        for (int i = 0; i < n; i++) p[i].Read(); p[n] = p[0];
        for (int i = n; i; i--) {
            Point t1, t2;
            change(p[i], p[i-1], t1, t2, r);
            line[n-i] = Line(t1, t2);
        }
        int m;
        HPI(line, n, p, m);
        int res1 = 0, res2 = 0;
        double ret = 0.;
        for (int i = 0; i < m; i++) {
            for (int j = i + 1; j < m; j++) {
                double td = dist(p[i], p[j]);
                if (dcmp(td-ret)>=0) {
                    ret = td;
                    res1 = i;
                    res2 = j;
                }
            }
        }
        printf("%.4f %.4f %.4f %.4f\n", p[res1].x, p[res1].y, p[res2].x, p[res2].y);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值