Cattle passenger Challenge 38 - A - Polygon and circle

Topic links: https://ac.nowcoder.com/acm/contest/4643/A

The meaning of problems: a hollow circle of radius r, and n has an internal convex polygon points, the polygon roll shell inner circle. Anticlockwise order given polygon vertices, each vertex has guaranteed access to the circular shell.

Initially, point 1, point 2, in a certain circle, the initial point 1 to the axis, and then successively to 2, 3, ..., n number of points as the axis "rolling." Specifically, when the number i points to axis "roll", the polygon will remain fixed point number i, and in that the center point starts to rotate clockwise, until the point i + 1 contact circles, then replace the shaft heart, continue to "roll."

Seeking polygon from the initial start rolling until 1 arrived on the circle again until the axis, 1st points in the overall process of scrolling through the journey is.

Solution: found after painting for a long time, the central angle consisting of two strings is calculated chord i-th central angle rotation, may be composed of the i-th point, i + 1-th point, the i + 2 points of the pairs (calculated using the law of cosines), and then calculates the chord of the isosceles triangle with base angles consisting of a central angle of the center (or directly calculated from the law of cosines corner). Then these two base angles is the rotation angle of the i + 1 and subtracting the polygon corner points are located. Radius of rotation will not necessarily be the edge of the polygon, but the first point and the i + 1-point connection. And the first point will be noted that the n-th point after the rotation time n-1 reappears on the circle.

const double PI = acos(-1.0);

double x[105], y[105];

double distance(int i, int j) {
    return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}

double angle(int i, int j, int k) {
    double p1x = x[i] - x[j], p1y = y[i] - y[j];
    double p2x = x[k] - x[j], p2y = y[k] - y[j];
    double cosval = (p1x * p2x + p1y * p2y) / (sqrt(p1x * p1x + p1y * p1y) * sqrt(p2x * p2x + p2y * p2y));
    return acos(cosval);
}

int main() {
    int n, r;
    scanf("%d%d", &n, &r);
    for(int i = 1; i <= n; ++i)
        scanf("%lf%lf", &x[i], &y[i]);
    x[n + 1] = x[1], y[n + 1] = y[1];
    x[n + 2] = x[2], y[n + 2] = y[2];
    double sum = 0;
    for(int i = 1; i <= n - 1; ++i) {
        double alpha = acos(distance(i, i + 1) / (2.0 * r));
        double beta = acos(distance(i + 1, i + 2) / (2.0 * r));
        double A = alpha + beta - angle(i, i + 1, i + 2);
        sum += A * distance(1, i + 1);
    }
    printf("%.12f\n", sum);
    return 0;
}

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/12536403.html