POJ 2365 Rope(计算几何)

Description
给出多边形顶点数n,钉子直径r,求从外面围住的绳子长度。
POJ 2365 Rope(计算几何)_第1张图片
Input
第一行两个整数n和r表示多边形顶点数和钉子直径,之后n行每行两个浮点数表示顶点坐标
Output
输出外围绳子长度
Sample Input
4 1
0.0 0.0
2.0 0.0
2.0 2.0
0.0 2.0
Sample Output
14.28
Solution
简单几何题,看图
POJ 2365 Rope(计算几何)_第2张图片
问题转化成求多边形的周长再加上一个钉子的周长
Code

#include<stdio.h>
#include<math.h>
int main()
{
    int n,i;
    double r,l=0,x[101],y[101];
    scanf("%d%lf",&n,&r);
    for(i=0;i<n;i++)
        scanf("%lf%lf",&x[i],&y[i]);
    for(i=1;i<n;i++)
        l+=sqrt((x[i]-x[i-1])*(x[i]-x[i-1])+(y[i]-y[i-1])*(y[i]-y[i-1]));
    l+=sqrt((x[n-1]-x[0])*(x[n-1]-x[0])+(y[n-1]-y[0])*(y[n-1]-y[0]));
    l+=2*acos(-1)*r;
    printf("%.2lf\n",l);
}

你可能感兴趣的:(POJ 2365 Rope(计算几何))