您的位置:首页 > 其它

nyoj 226 && hdu HDU 1348 && poj 1113 wall [凸包周长]

2014-07-21 18:20 435 查看
题目链接:nyoj poj hdu

题目的意思很是简单,就是求一个凸包的周长+以L为半径的圆的周长。

凸包算法有待完善。。。。。

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = 1e3 + 4;
const double PI = acos(-1);
const double eps = 1e-8;

struct Point{
    double x, y;
}p
;

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

double Distance(Point a, Point b){
    return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}

bool cmp(Point a, Point b){
    if(a.y < b.y || (a.y == b.y && a.x < b.x)) return true;
    return false;
}

bool cmp1(Point a, Point b){
    if(cross(p[0], a, b) > eps ) return true;
    else if(fabs(cross(p[0], a, b)) < eps && Distance(a, p[0]) > Distance(b, p[0])) return true;
    else return false;
}

double Judge(Point a, Point b, Point c){
    if(cross(a, b, c) > eps) return true;
    else if(fabs(cross(a, b, c)) < eps && Distance(b, a) < Distance(b, c)) return true;
    else return false;
}

void Graham_scan(int n, double r){
    sort(p, p + n, cmp);
    sort(p + 1, p + n, cmp1);
    Point st
;
    int  top = 0;
    st[top ++] = p[0]; st[top ++] = p[1];
    for(int i = 2; i < n; i ++){
        while(top >= 2 && Judge(st[top - 1], st[top - 2], p[i])){
            top --;
        }
        st[top ++] = p[i];
    }

    st[top] = st[0];
    double ans = 0;
    for(int i = 0; i < top; i ++){
        ans += Distance(st[i], st[i + 1]);
    }
    ans += (PI * r * 2.0);
    printf("%.0f\n",ans);
}

int main(){
    int n;
    double r;
    bool flag = false;
    while(~scanf("%d %lf",&n, &r)){
        for(int i = 0; i < n; i ++){
            scanf("%lf %lf",&p[i].x, &p[i].y);
        }
        if(flag) puts("");
        Graham_scan(n,r);
        flag = true;
    }
    return 0;
}


慢慢学计算几何!!。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: