Rolling The Polygon(银川区域赛网络赛B题)

Rolling The Polygon

Bahiyyah has a convex polygon with n vertices P0,P1,⋯,Pn−1 in the counterclockwise order. 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.

Example

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

Output

Case #1: 8.886
Case #2: 7.318
Case #3: 12.102
Case #4: 14.537

在这里插入图片描述
题目大意为给你一个不规则图形,然后给你一个中心点,然后问你这个图形以各个角翻滚,求这个中心点划过的周长;
先求出每个角的cos值,然后求出各个角到中心点的距离,为以这个角翻滚的圆的半径;然后根据公式     弧长=r*θ ;我们知道了cos,求θ 就非常简单,acos就可以;
代码:

#include<bits/stdc++.h>
using namespace std;
struct Node{
	int x,y;
}dian[100];
int main(){
	int t;
	scanf("%d",&t);
	int ans=0;
	while(t--){
		int n,p,q;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d%d",&dian[i].x,&dian[i].y);	
		}
		dian[0].x=dian[n].x,dian[0].y=dian[n].y;
		scanf("%d%d",&p,&q);
		double sum=0;
		for(int i=1;i<=n;i++){
			int x1,y1,x2,y2,x3,y3;
			x1=dian[i].x;y1=dian[i].y;
			x2=dian[(i+1)%n].x;y2=dian[(i+1)%n].y;
			x3=dian[(i+2)%n].x;y3=dian[(i+2)%n].y;
			double r=(double)sqrt((double)(x2-p)*(double)(x2-p)+(double)(y2-q)*(double)(y2-q));
			double a2=(double)(x1-x3)*(double)(x1-x3)+(double)(y1-y3)*(double)(y1-y3);
			double b2=(double)(x1-x2)*(double)(x1-x2)+(double)(y1-y2)*(double)(y1-y2);
			double c2=(double)(x2-x3)*(double)(x2-x3)+(double)(y2-y3)*(double)(y2-y3);
			double b=(double)sqrt((double)(x1-x2)*(double)(x1-x2)+(double)(y1-y2)*(double)(y1-y2));
			double c=(double)sqrt((double)(x2-x3)*(double)(x2-x3)+(double)(y2-y3)*(double)(y2-y3));
			double Jiao=(b2+c2-a2)/(2*b*c);
			sum+=acos(-1*Jiao)*r;
		}
		printf("Case #%d: %.3f\n",++ans,sum);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值