poj 1113 凸包+简单几何计算

题意:

给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。


解析:

韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。

用convexhull算法把凸包求出来,然后加加减减就A了。

计算见下图:

好久没玩画图了啊好开心。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>

#define LL long long

using namespace std;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-10;
const double pi = 4 * atan(1.0);

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0) : 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 - (Vector A, Vector 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);
}

Vector operator / (Vector A, double p)
{
    return Vector(A.x / p, A.y / p);
}

bool operator < (const Point& a, const Point& b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

int dcmp(double x)
{
    if (fabs(x) < eps)
    {
        return 0;
    }
    else
    {
        return x < 0 ? -1 : 1;
    }
}

bool operator == (const Point& a, const Point& b)
{
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

double Dot(Vector A, Vector B)
{
    return A.x * B.x + A.y * B.y;
}

double Length(Vector A)
{
    return sqrt(Dot(A, A));
}

double Angle(Vector A, Vector B)
{
    return acos(Dot(A, B) / Length(A) / Length(B));
}

double Cross(Vector A, Vector B)
{
    return A.x * B.y - A.y * B.x;
}

double Area2(Point A, Point B, Point C)
{
    return Cross(B - A, C - A);
}

bool cmp(Point A, Point B)
{
    if (A.x == B.x)
        return A.y < B.y;
    return A.x < B.x;
}

int ConvexHull(Point* p, int n, Point* ch)
{
    sort(p, p + n, cmp);
    int m = 0;
    for (int i = 0; i < n; i++)
    {
        while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n - 2; i >= 0; i--)
    {
        while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    if (n > 1)
        m--;
    return m;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int n;
    double l;
    while (scanf("%d%lf", &n, &l) == 2)
    {
        Point p[maxn];
        for (int i = 0; i < n; i++)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        Point ch[maxn];
        int index = ConvexHull(p, n, ch);
//        for (int i = 0; i < index; i++)
//            cout << ch[i].x << " " << ch[i].y << endl;
        Vector g[maxn];
        double angle = 0;
        double ans = 0;
        for (int i = 0; i < index - 1; i++)
        {
            g[i].x = ch[i + 1].x - ch[i].x;
            g[i].y = ch[i + 1].y - ch[i].y;
            ans += Length(g[i]);
        }
        g[index - 1].x = ch[0].x - ch[index - 1].x;
        g[index - 1].y = ch[0].y - ch[index - 1].y;
        ans += Length(g[index - 1]);
        //cout << ans << endl;
        for (int i = 0; i < index - 1; i++)
        {
            angle += Angle(g[i], g[i + 1]);
        }
        angle += Angle(g[index - 1], g[0]);
        //cout << angle << endl;
        ans += pi * l * angle / pi;
        printf("%.lf\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值