2013-07-05 16:16:29Morris

[UVA] 10406 - Cutting tabletops

Problem D: Cutting tabletops

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 n pairs 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

給一個凸多邊形, 且向內部切割一個距離 d。

先計算該多邊形的面積,接著計算夾角的三角形夾角 theta,
theta 對應的是直角三角形的邊 d, 而這個三角形有兩個, 計算其面積。

而矩形的周長會是原本多邊形的周長減去三角形另一個邊的長度。


#include <stdio.h>
#include <math.h>
struct Pt {
    double x, y;
};
int main() {
    const double pi = acos(-1);
    double d;
    int n, i, j;
    Pt D[1005];
    while(scanf("%lf %d", &d, &n) == 2 && n) {
        for(i = 0; i < n; i++)
            scanf("%lf %lf", &D[i].x, &D[i].y);
        D[n] = D[0];
        double area = 0;
        for(i = 0; i < n; i++)
            area += D[i].x*D[i+1].y-D[i].y*D[i+1].x;
        area = fabs(area)/2;
        double perimeter = 0;
        for(i = 0; i < n; i++)
            perimeter += hypot(D[i].x-D[i+1].x, D[i].y-D[i+1].y);
        D[n+1] = D[1];
        for(i = 1; i <= n; i++) {
            double a, b, c;
            a = hypot(D[i].x-D[i-1].x, D[i].y-D[i-1].y);
            b = hypot(D[i].x-D[i+1].x, D[i].y-D[i+1].y);
            c = hypot(D[i+1].x-D[i-1].x, D[i+1].y-D[i-1].y);
            double theta = acos((a*a+b*b-c*c)/(2*a*b));
            theta /= 2;
            double w = d/tan(theta);
            perimeter -= w+w;
            area -= d*w;
        }
        area -= perimeter*d;
        printf("%.3lf\n", area);
    }
    return 0;
}