uvalive 3218 Find the Border

时间:2023-03-09 19:10:44
uvalive 3218 Find the Border

题意:一条封闭折线将平面分成了若干个区域,按顺序给出折线各点的坐标,要求输出封闭折线的轮廓。
题解:用类似卷包裹的算法,先确定一个一定会被选中的点(x坐标最小,y坐标最小)作为起点,然后把可能是下一个极点(凸包顶点)的点都存起来,下一个极点有可能是当前点所在线段的前一个点和后一个点或当前点所在线段和其他线段的有交点的线段的起点和终点。
找出最右侧的点(用角度判断)和当前点的连线是否和其他线段有交点,如果有就找最近的交点当做答案的下一个点,如果没有最右侧的点就是下一个点。最后转回起点结束。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-);
const double eps = 1e-;
struct Point
{
double x, y;
Point(double x = , double y = ): x(x), y(y) {}
};
typedef Point Vector;
double dcmp(double x)
{
if (fabs(x) < eps)
return ;
return x < ? - : ;
}
Vector operator + (const Point& A, const Point& B)
{
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B)
{
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Point& A, double a)
{
return Vector(A.x * a, A.y * a);
}
Vector operator / (const Point& A, double a)
{
return Vector(A.x / a, A.y / a);
}
double Cross(const Vector& A, const Vector& B)
{
return A.x * B.y - A.y * B.x;
}
double Dot(const Vector& A, const Vector& B)
{
return A.x * B.x + A.y * B.y;
}
double Length(const Vector& A)
{
return sqrt(Dot(A, A));
}
bool operator < (const Point& A, const Point& B)
{
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B)
{
return A.x == B.x && A.y == B.y;
}
Point GetLineIntersection(Point P, Point v, Point Q, Point w)
{
Point u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
double c1 = Cross(a2 - a1, b1 - a1);
double c2 = Cross(a2 - a1, b2 - a1);
double c3 = Cross(b2 - b1, a1 - b1);
double c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
}
bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
return dcmp(Cross(a1 - p, a2 - p)) == && dcmp(Dot(a1 - p, a2 - p)) < ;
} const int N = ;
const int M = ;
Point P[M], res[N], temp[M];
double ang[M], s;
int n, cnt; void add(Point a, Point b)
{
temp[cnt] = a;
ang[cnt] = atan2(temp[cnt].y - b.y, temp[cnt].x - b.x) - s;
while (dcmp(ang[cnt]) <= )
ang[cnt] += * PI;
cnt++;
} int main()
{
while (scanf("%d", &n) == )
{
int minid = ;
for (int i = ; i < n; i++)
{
scanf("%lf%lf", &P[i].x, &P[i].y);
if (P[i] < P[minid])
minid = i;
}
res[] = P[minid];
int num = ;
s = -PI;
while ()
{
cnt = ;
for (int i = ; i < n; i++)
{
if (res[num - ] == P[i])
{
add(P[(i + ) % n], res[num - ]);
add(P[(i + n - ) % n], res[num - ]);
break;
}
}
for (int i = ; i < n; i++)
{
if (OnSegment(res[num - ], P[i], P[(i + ) % n]))
{
add(P[(i + ) % n], res[num - ]);
add(P[i], res[num - ]);
}
}
int id = ;
for (int i = ; i < cnt; i++)
if (ang[i] < ang[id])
id = i;
double minlen = 1e9;
Point RP = temp[id], its;
for (int i = ; i < n; i++)
{
if (SegmentProperIntersection(temp[id], res[num - ], P[i], P[(i + ) % n]))
{
its = GetLineIntersection(temp[id], temp[id] - res[num - ], P[i], P[i] - P[(i + ) % n]);
if (Length(its - res[num - ]) < minlen)
{
minlen = Length(its - res[num - ]);
RP = its;
}
}
}
res[num] = RP;
s = atan2(res[num - ].y - res[num].y, res[num - ].x - res[num].x);
num++;
if (res[num - ] == res[])
break;
}
printf("%d\n", num - );
for (int i = ; i < num - ; i++)
printf("%.4lf %.4lf\n", res[i].x, res[i].y);
}
return ;
}

后一种解法是用PSLG的外轮廓。

 #include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cassert>
using namespace std; const double eps = 1e-;
double dcmp(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
} struct Point
{
double x, y;
Point(double x=, double y=):x(x),y(y) { }
}; typedef Point Vector; Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
} Vector operator - (Point A, Point B)
{
return Vector(A.x-B.x, A.y-B.y);
} Vector operator * (Vector A, double p)
{
return Vector(A.x*p, A.y*p);
} // 理论上这个“小于”运算符是错的,因为可能有三个点a, b, c, a和b很接近(即a<b好b<a都不成立),b和c很接近,但a和c不接近
// 所以使用这种“小于”运算符的前提是能排除上述情况
bool operator < (const Point& a, const Point& b)
{
return dcmp(a.x - b.x) < || (dcmp(a.x - b.x) == && dcmp(a.y - b.y) < );
} bool operator == (const Point& a, const Point &b)
{
return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ;
} double Dot(Vector A, Vector B)
{
return A.x*B.x + A.y*B.y;
}
double Cross(Vector A, Vector B)
{
return A.x*B.y - A.y*B.x;
}
double Length(Vector A)
{
return sqrt(Dot(A, A));
} typedef vector<Point> Polygon; Point GetLineIntersection(const Point& P, const Vector& v, const Point& Q, const Vector& w)
{
Vector u = P-Q;
double t = Cross(w, u) / Cross(v, w);
return P+v*t;
} bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1), c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
} bool OnSegment(Point p, Point a1, Point a2)
{
return dcmp(Cross(a1-p, a2-p)) == && dcmp(Dot(a1-p, a2-p)) < ;
} // 多边形的有向面积
double PolygonArea(Polygon poly)
{
double area = ;
int n = poly.size();
for(int i = ; i < n-; i++)
area += Cross(poly[i]-poly[], poly[(i+)%n]-poly[]);
return area/;
} struct Edge
{
int from, to; // 起点,终点,左边的面编号
double ang;
}; const int maxn = + ; // 最大边数 // 平面直线图(PSGL)实现
struct PSLG
{
int n, m, face_cnt;
double x[maxn], y[maxn];
vector<Edge> edges;
vector<int> G[maxn];
int vis[maxn*]; // 每条边是否已经访问过
int left[maxn*]; // 左面的编号
int prev[maxn*]; // 相同起点的上一条边(即顺时针旋转碰到的下一条边)的编号 vector<Polygon> faces;
double area[maxn]; // 每个polygon的面积 void init(int n)
{
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
faces.clear();
} // 有向线段from->to的极角
double getAngle(int from, int to)
{
return atan2(y[to]-y[from], x[to]-x[from]);
} void AddEdge(int from, int to)
{
edges.push_back((Edge)
{
from, to, getAngle(from, to)
});
edges.push_back((Edge)
{
to, from, getAngle(to, from)
});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} // 找出faces并计算面积
void Build()
{
for(int u = ; u < n; u++)
{
// 给从u出发的各条边按极角排序
int d = G[u].size();
for(int i = ; i < d; i++)
for(int j = i+; j < d; j++) // 这里偷个懒,假设从每个点出发的线段不会太多
if(edges[G[u][i]].ang > edges[G[u][j]].ang) swap(G[u][i], G[u][j]);
for(int i = ; i < d; i++)
prev[G[u][(i+)%d]] = G[u][i];
} memset(vis, , sizeof(vis));
face_cnt = ;
for(int u = ; u < n; u++)
for(int i = ; i < G[u].size(); i++)
{
int e = G[u][i];
if(!vis[e]) // 逆时针找圈
{
face_cnt++;
Polygon poly;
for(;;)
{
vis[e] = ;
left[e] = face_cnt;
int from = edges[e].from;
poly.push_back(Point(x[from], y[from]));
e = prev[e^];
if(e == G[u][i]) break;
assert(vis[e] == );
}
faces.push_back(poly);
}
} for(int i = ; i < faces.size(); i++)
{
area[i] = PolygonArea(faces[i]);
}
}
}; PSLG g; const int maxp = + ;
int n, c;
Point P[maxp]; Point V[maxp*(maxp-)/+maxp]; // 在V数组里找到点p
int ID(Point p)
{
return lower_bound(V, V+c, p) - V;
} // 假定poly没有相邻点重合的情况,只需要删除三点共线的情况
Polygon simplify(const Polygon& poly)
{
Polygon ans;
int n = poly.size();
for(int i = ; i < n; i++)
{
Point a = poly[i];
Point b = poly[(i+)%n];
Point c = poly[(i+)%n];
if(dcmp(Cross(a-b, c-b)) != ) ans.push_back(b);
}
return ans;
} void build_graph()
{
c = n;
for(int i = ; i < n; i++)
V[i] = P[i]; vector<double> dist[maxp]; // dist[i][j]是第i条线段上的第j个点离起点(P[i])的距离
for(int i = ; i < n; i++)
for(int j = i+; j < n; j++)
if(SegmentProperIntersection(P[i], P[(i+)%n], P[j], P[(j+)%n]))
{
Point p = GetLineIntersection(P[i], P[(i+)%n]-P[i], P[j], P[(j+)%n]-P[j]);
V[c++] = p;
dist[i].push_back(Length(p - P[i]));
dist[j].push_back(Length(p - P[j]));
} // 为了保证“很接近的点”被看作同一个,这里使用了sort+unique的方法
// 必须使用前面提到的“理论上是错误”的小于运算符,否则不能保证“很接近的点”在排序后连续排列
// 另一个常见的处理方式是把坐标扩大很多倍(比如100000倍),然后四舍五入变成整点(计算完毕后再还原),用少许的精度损失换来鲁棒性和速度。
sort(V, V+c);
c = unique(V, V+c) - V; g.init(c); // c是平面图的点数
for(int i = ; i < c; i++)
{
g.x[i] = V[i].x;
g.y[i] = V[i].y;
}
for(int i = ; i < n; i++)
{
Vector v = P[(i+)%n] - P[i];
double len = Length(v);
dist[i].push_back();
dist[i].push_back(len);
sort(dist[i].begin(), dist[i].end());
int sz = dist[i].size();
for(int j = ; j < sz; j++)
{
Point a = P[i] + v * (dist[i][j-] / len);
Point b = P[i] + v * (dist[i][j] / len);
if(a == b) continue;
g.AddEdge(ID(a), ID(b));
}
} g.Build(); Polygon poly;
for(int i = ; i < g.faces.size(); i++)
if(g.area[i] < ) // 对于连通图,惟一一个面积小于零的面是无限面
{
poly = g.faces[i];
reverse(poly.begin(), poly.end()); // 对于内部区域来说,无限面多边形的各个顶点是顺时针的
poly = simplify(poly); // 无限面多边形上可能会有相邻共线点
break;
} int m = poly.size();
printf("%d\n", m); // 挑选坐标最小的点作为输出的起点
int start = ;
for(int i = ; i < m; i++)
if(poly[i] < poly[start]) start = i;
for(int i = start; i < m; i++)
printf("%.4lf %.4lf\n", poly[i].x, poly[i].y);
for(int i = ; i < start; i++)
printf("%.4lf %.4lf\n", poly[i].x, poly[i].y);
} int main()
{
while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
P[i] = Point(x, y);
}
build_graph();
}
return ;
}