POJ3384 Feng Shui 【半平面交+向內縮排】
阿新 • • 發佈:2018-12-19
就是 向內縮排+半平面交模板 一開始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;
}