码迷,mamicode.com
首页 > 其他好文 > 详细

test

时间:2019-01-09 21:43:36      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:orm   clu   逆时针   ||   abs   class   include   ble   枚举   

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point {
    double x, y;

    Point(double a = 0, double b = 0) : x(a), y(b) {}

    Point operator - (const Point &b) const {
        return Point(x - b.x, y - b.y);
    }

    bool operator < (const Point &b) const {
        return x < b.x || (x == b.x && y < b.y);
    }
    
    double norm(){//模的长度
        return sqrt(x * x + y * y);
    }
};

//计算a与b(向量积)的值
double cross(Point a, Point b) {
    return a.x * b.y - a.y * b.x;
}

//判断b在a顺时针还是逆时针
bool IsClockWise(Point p0, Point p1, Point p2) {
    if (cross(p1 - p0, p2 - p1) < 0)
        return true;
    else
        return false;
}

//安德鲁算法(Andrew’s Algorithm)求凸包
int Andrew(Point *res, Point *p, int n) {
    sort(p, p+n);
    int m = 0; //凸包集合计数索引
    for (int i = 0; i < n; i++) { //从左向右扫描,创建凸包的上部
        while (m >= 2 && !IsClockWise(res[m-2], res[m-1], p[i]))
            m--;
        res[m++] = p[i];
    }
    int k = m;
    for (int i = n - 2; i >= 0; i--) { //从n-2开始是因为n-1一定在凸包的上部分,已包含,
        while (m > k && !IsClockWise(res[m-2], res[m-1], p[i]))
            m--;
        res[m++] = p[i];
    }
    if (m > 0) m--;
    return m;
}

//求点到边的距离
double DistanceToLine(Point p, Point a, Point b) {
    double buttom = (a-b).norm();
    return fabs(cross(a-p, b-p)) / buttom;
}

int main() {
    Point res[105],p[105];
    int n;
    int cnt = 1;
    while (cin >> n && n) {
        for (int i = 0; i < n; i++)
            cin >> p[i].x >> p[i].y;
        int m = Andrew(res, p, n);
        double ans = INFINITY;
        //枚举每一条边作为低,求高的最小值。i取值从0~m,形成一个换
        for (int i = 0; i <= m; i++) { 
            double max_high = 0.0;
            for (int j = 0; j < m; j++) {
                max_high = max(max_high, DistanceToLine(res[j], res[i], res[(i+1)%m]));
            }
            ans = min(ans,max_high);
        }
        ans = ceil(ans * 100) / 100;
        cout << "Case " << cnt++ << ": " << ans << endl;
    }
    return 0;
}

 

test

标签:orm   clu   逆时针   ||   abs   class   include   ble   枚举   

原文地址:https://www.cnblogs.com/zhanyeye/p/10246592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!