您的位置:首页 > 其它

开始学习计算几何啦

2016-08-05 18:42 375 查看
先预习了计算几何的基本知识,今天就要正式开始学习计算几何了。

先刷了道水题。

nyoj78 圈水池

一看就是个凸包,上网搜了凸包的资料,算是看懂了(不过写题就不会了)。

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const double eps = 1e-6;
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};

Point p[150],s[150];
//int dcmp(double x) {
//        if(fabs(x)<eps) return 0;
//        else return x<0 ? -1 : 1;
//}
double dis(Point a,Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double cross(Point a,Point b,Point c) {
return (b.x-a.x)*(c.y - a.y) - (c.x-a.x)*(b.y-a.y);
}

bool cmp1(Point a,Point b) {
int m = cross(p[1],a,b);
if(m==0) return dis(p[1],a)-dis(p[1],b) <=0 ? true:false;
else return m >0 ? true : false ;
}

bool cmp(const Point &a,const Point &b) {
if(a.x != b.x) return a.x < b.x;
else return a.y<b.y;
}
int main(){
//        freopen("1.in","r",stdin);
int t; scanf("%d",&t);
while(t--) {
int n; scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p+1,p+n+1,cmp);
sort(p+2,p+n+1,cmp1);
int top=1;
s[0].x = p[1].x; s[0].y = p[1].y;
s[1].x = p[2].x; s[1].y=p[2].y;

for(int i=3;i<=n;i++) {
while(i>=1 && cross(s[top-1],s[top],p[i])<0) top--;
s[++top] = p[i];
}
//               cout<<top<<endl;
sort(s,s+top+1,cmp);
for(int i=0;i<=top;i++) {
cout<<s[i].x<<" "<<s[i].y<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: