开始学习计算几何啦

先预习了计算几何的基本知识,今天就要正式开始学习计算几何了。
先刷了道水题。

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值