Time limit1000 ms Memory limit131072 kB
Problem Description
输入一个点P和一条圆弧(圆周的一部分),你的任务是计算P到圆弧的最短距离。换句话说,你需要在圆弧上找一个点,到P点的距离最小。
提示:请尽量使用精确算法。相比之下,近似算法更难通过本题的数据。
Input
输入包含最多10000组数据。每组数据包含8个整数x1, y1, x2, y2, x3, y3, xp, yp。圆弧的起点是A(x1,y1),经过点B(x2,y2),结束位置是C(x3,y3)。点P的位置是 (xp,yp)。输入保证A, B, C各不相同且不会共线。上述所有点的坐标绝对值不超过20。
Output
对于每组数据,输出测试点编号和P到圆弧的距离,保留三位小数。你的输出和标准输出之间最多能有0.001的误差。
Sample Input
0 0 1 1 2 0 1 -1
3 4 0 5 -3 4 0 1
Sample Output
Case 1: 1.414
Case 2: 4.000
Solving Ideas
根据三点确定圆心和半径,关键是确定扇形区域(尤其是优弧).
如果点跟圆心的连线在那段扇形的圆弧范围内,点到圆弧的最短距离为点到圆心的距离减去半径后的绝对值;否则,点到圆弧的最短的距离为到这段圆弧的两个端点的最小值。
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
double r, rx, ry, p_a, p_b, p_c, p_r;
void Get_C(int x1, int y1, int x2, int y2, int x3, int y3) {
int a, b, c, g, e, f;
a = 2 * (x3 - x2);
b = 2 * (y3 - y2);
c = x3 * x3 - x2 * x2 + y3 * y3 - y2 * y2;
e = 2 * (x2 - x1);
f = 2 * (y2 - y1);
g = x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1;
rx = (double)(g * b - c * f) / (e * b - a * f);
ry = (double)(a * g - c * e) / (a * f - b * e);
r = sqrt((rx - x1) * (rx - x1) + (ry - y1) * (ry - y1));
}
double Get_L(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
int t = 1, temp, x1, y1, x2, y2, x3, y3, xp, yp, cba_c;
double crb_a, crb_c, crp_a, crp_c, cra_c;
while (~scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &xp, &yp)) {
temp = 0;
Get_C(x1, y1, x2, y2, x3, y3);
p_a = Get_L(xp, yp, x1, y1);
p_b = Get_L(xp, yp, x2, y2);
p_c = Get_L(xp, yp, x3, y3);
p_r = sqrt((xp - rx) * (xp - rx) + (yp - ry) * (yp - ry));
crb_a = (x2 - rx) * (y1 - ry) - (x1 - rx) * (y2 - ry);
crb_c = (x2 - rx) * (y3 - ry) - (x3 - rx) * (y2 - ry);
crp_a = (xp - rx) * (y1 - ry) - (x1 - rx) * (yp - ry);
crp_c = (xp - rx) * (y3 - ry) - (x3 - rx) * (yp - ry);
cra_c = (x1 - rx) * (y3 - ry) - (x3 - rx) * (y1 - ry);
cba_c = (x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2);
if (cba_c <= 0) {
if (crb_a * crp_a >= 0 && crb_c * crp_c >= 0)
temp = 1;
}
else {
if (cra_c > 0) {
if (crp_a < 0 && crp_c > 0)
temp = 0;
else temp = 1;
}
else {
if (crp_a > 0 && crp_c < 0)
temp = 0;
else temp = 1;
}
}
if (temp)
printf("Case %d: %.3lf\n", t++, fabs(p_r - r));
else printf("Case %d: %.3lf\n", t++, min(p_a, p_c));
}
return 0;
}