题目描述
Bahiyyah has a convex polygon with n vertices P0, P1, · · · , Pn−1 in the counterclockwiseorder. Two vertices with consecutive(连续的) indexes are adjacent(邻近的), and besides, P0 and Pn−1 are adjacent. She also assigns(指出) a point Q inside the polygon which may appear on the border.
Now, Bahiyyah decides to roll the polygon along a straight line and calculate the length of the trajectory(弹道) (or track) of point Q.
To help clarify, we suppose Pn = P0, Pn+1 = P1 and assume the edge between P0 and P1 is lying on the line at first. At that point when the edge between Pi−1 and Pi lies on the line, Bahiyyah rolls the polygon forward rotating(旋转) the polygon along the vertex Pi until the next edge (which is between Pi and Pi+1) meets the line. She will stop the rolling when the edge between Pn and Pn+1 (which is same as the edge between P0 and P1) meets the line again.
Input
The input contains several test cases, and the first line is a positive integer T indicating the number of test cases which is up to 50.
For each test case, the first line contains an integer n (3 ≤ n ≤ 50) indicating the number of vertices of the given convex polygon. Following n lines describe vertices of the polygon in the counterclockwise order. The i-th line of them contains two integers xi−1 and yi−1, which are the coordinates of point Pi−1. The last line contains two integers xQ and yQ, which are the coordinates of point Q.
We guarantee that all coordinates are in the range of −103 to 103, and point Q is located inside the polygon or lies on its border.
Output
For each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y is the length of the trajectory of the point Q rounded to 3 places. We guarantee that 4-th place after the decimal point in the precise answer would not be 4 or 5.
样例输入
4
4
0 0
2 0
2 2
0 2
1 1
3
0 0
2 1
1 2
1 1
5
0 0
1 0
2 2
1 3
-1 2
0 0
6
0 0
3 0
4 1
2 2
1 2
-1 1
1 0
样例输出
Case #1: 8.886
Case #2: 7.318
Case #3: 12.102
Case #4: 14.537
题目理解
题目描述很短,大意是现在有一个n边的凸多边形,给你这个多边形所有顶点的坐标。然后再给你多边形内一个点的坐标,接着滚动这个多边形一周,需要我们计算出目标点移动轨迹的长度。为了更好理解,题目第三段详细说明了如何滚动:假设P0为初始点,那么在P0和P1之间有一条边1,在P1和P2之间有一条边2,连接P0和P2也有一条边3,每次都沿着一条边滚动,且第一次都是沿着边1滚。如何停止呢?就是当滚动的边再一次是边1时就停止。可以认为最后是Pn和Pn+1之间的边,即Pn=P0,Pn+1=P1。
解题思路
观察多边形和目标点滚动的方式和轨迹,可以知道每次滚动所形成的轨迹是一段弧长,那么为了求出此弧长则需要知道此弧长所在圆的半径和旋转的角度。对于半径,首先确定圆心即使滚动角的顶点(滚动角就是Pi、Pi+1和Pi+2所形成的夹角,那么滚动角顶点则是P1),而目标点则是圆边上的一点,那么半径= 滚动角顶点(v[i+1])到目标点(tv)的距离 = sqrt(pow(tv.x-ver[i+1].x,2)+pow(tv.y-ver[i+1].y,2));对于旋转角度就是180°减去滚动角的角度,三点坐标有了,Pi和Pi+1之间边a,那么就用余弦定理(复习下:cosx = (a^2 + b^2 - c^2)/(2ab))。旋转角度= 180°-滚动角° = π-acos((a*a+b*b-c*c)/(2*a*b)) ,(注:Pi和Pi+1之间边a,Pi+1和Pi+2之间边b,Pi和Pi+2之间边c);最后求弧长 = 半径 * 旋转角度即可。
弧长计算的循环如果从P0开始,Pn-1结束,就设Pn=P0,Pn+1=P1,因为根据题意的停止条件(当边再次滚到初始的边1时,滚完即止,最后是Pn-1要和Pn和Pn+1构成一个角,那就要设Pn=P0,Pn+1=P1)。
AC代码
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
const double PI (3.1415926535);
typedef struct polygon{
int x;
int y;
}vertices;
int main(){
int t;
scanf("%d",&t);
for(int i = 1; i <= t; i++){
int n;
scanf("%d",&n);
vertices ver[55]; //多边形顶点初始化
double arclen = 0; //弧长初始化
for(int j = 1; j <= n; j++){
scanf("%d %d",&ver[j].x,&ver[j].y);
}
scanf("%d %d",&ver[0].x,&ver[0].y); //我在这设目标点为ver[0]
ver[n+1] = ver[1];
ver[n+2] = ver[2];
for(int k = 1; k <= n; k++){
double a,b,c;
a = sqrt(pow(ver[k].x-ver[k+1].x,2)+pow(ver[k].y-ver[k+1].y,2)); //计算边a
b = sqrt(pow(ver[k+2].x-ver[k+1].x,2)+pow(ver[k+2].y-ver[k+1].y,2)); //计算边b
c = sqrt(pow(ver[k].x-ver[k+2].x,2)+pow(ver[k].y-ver[k+2].y,2)); //计算边c
double radius = sqrt(pow(ver[0].x-ver[k+1].x,2)+pow(ver[0].y-ver[k+1].y,2)); //计算半径
double angle = PI - acos((a*a+b*b-c*c)/(2*b*a)); //计算旋转角度
arclen += (angle * radius); //累加弧长
}
printf("Case #%d: %.3lf\n", i, arclen);
}
return 0;
}