首页 > 其他 > 详细

UVa10406 - Cutting tabletops

时间:2014-04-27 09:10:45      阅读:441      评论:0      收藏:0      [点我收藏+]

Problem D: Cutting tabletops

bubuko.com,布布扣bubuko.com,布布扣Bever Lumber hires beavers to cut wood. The company has recently received a shippment of tabletops. Each tabletop is a convex polygon. However, in this hard economic times of cutting costs the company has ordered the tabletops from a not very respectable but cheap supplier. Some of the tabletops have the right shape but they are slightly too big. The beavers have to chomp of a strip of wood of a fixed width from each edge of the tabletop such that they get a tabletop of a similar shape but smaller. Your task is to find the area of the tabletop after beavers are done.

Input consists of a number of cases each presented on a separate line. Each line consists of a sequence of numbers. The first number is d the width of the strip of wood to be cut off of each edge of the tabletop in centimeters. The next number n is an integer giving the number of vertices of the polygon. The next npairs of numbers present xi and yi coordinates of polygon vertices for 1 <= i <= n given in clockwise order. A line containing only two zeroes terminate the input.

d is much smaller than any of the sides of the polygon. The beavers cut the edges one after another and after each cut the number of vertices of the tabletop is the same.

For each line of input produce one line of output containing one number to three decimal digits in the fraction giving the area of the tabletop after cutting.

Sample input

2 4 0 0 0 5 5 5 5 0
1 3 0 0 0 5 5 0
1 3 0 0 3 5.1961524 6 0
3 4 0 -10 -10 0 0 10 10 0
0 0

Output for sample input

1.000
1.257
2.785
66.294

Problem Setter: Piotr Rudnicki


很久没切题了。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
const double eps = 1e-8;
inline double sqr(double x){
    return x*x;
}
struct point{
    double x,y;
    point(double x=0,double y=0):x(x),y(y){}
    double norm(){
        return sqrt(sqr(x)+sqr(y));
    }
    friend point operator + (point a,point b){
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator - (point a,point b){
        return point(a.x-b.x,a.y-b.y);
    }
};
double det(const point &a,const point &b){
    return a.x*b.y-a.y*b.x;
}
double dot(const point &a,const point &b){
    return a.x*b.x+a.y*b.y;
}
double d;
int n;
vector<point> polygon;
int main(){

    while(cin >> d >> n) {
        if(n==0&&fabs(d)<eps) break;
        double ans = 0;
        polygon.clear();
        polygon.resize(n);
        for(int i = 0; i < n; i++){
            scanf("%lf%lf",&polygon[i].x,&polygon[i].y);
        }
        for(int i = 0; i < n; i++){
            int next = (i+1)%n;
            ans += fabs(det(polygon[i],polygon[next]))/2;
            ans -= (polygon[i]-polygon[next]).norm()*d;
        }
        for(int i = 0; i < n; i++){
            int last = (i-1+n)%n;
            int next = (i+1)%n;
            point a = polygon[i]-polygon[last];
            point b = polygon[i]-polygon[next];
            double ang = acos(dot(a,b)/(a.norm()*b.norm()))/2;
            double area = d*d/tan(ang);
            ans += area;
        }
        printf("%.3lf\n",ans);



    }
    return 0;
}


UVa10406 - Cutting tabletops,布布扣,bubuko.com

UVa10406 - Cutting tabletops

原文:http://blog.csdn.net/mowayao/article/details/24538819

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!