Title Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503
Description
Enter a point P and an arc (part of the circumference), and your task is to calculate the shortest distance from p to arc. In other words, you need to find a point on the arc, the distance to the P point is minimal.
Tip: Use the exact algorithm as much as you could. In contrast, the approximate algorithm is more difficult to pass the data.
Input
The input contains up to 10000 sets of data. Each set of data contains 8 integers x1, y1, x2, y2, X3, y3, XP, YP. The starting point of the arc is a (x1,y1), passing through point B (x2,y2), and the end position is C (x3,y3). The position of the point P is (XP,YP). The input guarantees a, B, C are different and will not be collinear. The above-mentioned coordinates are not more than 20 in absolute terms.
Output
For each set of data, output the test point number and the distance from p to arc, leaving three decimal places. You can have up to 0.001 errors between your output and the standard output.
Sample Input
0 0 1 1 2 0 1-13 4 0 5-3 4 0 1
Sample Output
Case 1:1.414case 2:4
HINT
Source
The tenth session of Hunan Province College students computer Program design Contest
Ps:
In two cases:
The first kind: The point with the center of the line in the arc range of the sector, point to the shortest distance to the arc of the point to the center of the distance minus the radius and then take absolute value;
The second: the Point and center of the line is not in that segment of the arc range, point to the shortest distance to the arc is the minimum value of the two endpoints of this arc.
The code is as follows: (See Summer laughter)
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include < Algorithm>using namespace Std;const Double PI = ACOs ( -1.0); struct point{double x, y; Friend point operator-(point A,point B)//overloaded friend operator {Point temp; temp.x = a.x-b.x; TEMP.Y = A.Y-B.Y; return temp; }}; Point P1, p2, p3, PC, pp;double R; Point GET_PC1 (Point P1, point P2, point P3)//Seek center {Double A, B, C, D, E, F; Point P; A = (p2.x-p1.x); b = (P2.Y-P1.Y); c = p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y; D = (p3.x-p2.x); E = (P3.Y-P2.Y); f = p3.x*p3.x+p3.y*p3.y-p2.x*p2.x-p2.y*p2.y; p.x = (b*f-e*c)/(B*D-E*A); P.Y = (d*c-a*f)/(B*D-E*A); R = sqrt ((p.x-p1.x) * (p.x-p1.x) + (P.Y-P1.Y) * (P.Y-P1.Y));//Radius return p;} Double Dis (point A,point b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} Double Mult_cha (Point p1,point p2)/cross product {return p1.x * p2.y-p2.x * P1.Y;} Double Get_ans (Point Pc,pointPp,point p1,point p2,point p3) {Double temp = Mult_cha (P2-P1,P3-P1);//Determine whether the direction of the point is clockwise or counterclockwise double F1 = atan2 ((p1-pc). Y, (P 1-PC). x);//and x positive half axis angle double F2 = atan2 ((p2-pc). Y, (P2-PC). x); Double F3 = atan2 ((p3-pc). Y, (P3-PC). x); Double F4 = atan2 ((pp-pc). Y, (PP-PC). x); Double ans1 = fabs (DIS (pp,pc)-R);//to the center minus radius double ans2 = min (Dis (pp,p1), Dis (PP,P3));//lower value of the endpoint if (Temp < 0) Clockwise to point {if (F1 < F3)//Determine interval direction, indent {if (F2 >= F1 && F2 <= f3) = = (F4 >= F 1 && f4 <= F3))//p Point and P2 point return ans1 in the same interval; else//p Point and P2 Point are not in the same interval return ans2; } else//up Convex {if ((F2 >= f3 && f2 <= f1) = = (F4 >=f3 && f4 <= F1)) return ans1; else return ans2; }} else {if (F1 > F3) {if ((F2 <= F1 && F2 >= f3) = = (F4 <= F1 & ;& F4 >= F3) return ans1; else return ans2; } else {if ((F2 <= f3 && f2 >= f1) = = (F4 <= f3 && f4 >= F1)) return ans1; else return ans2; }}}int Main () {int cas = 0; while (~SCANF ("%lf%lf%lf%lf%lf%lf%lf%lf", &p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&pp.x, &PP.Y)) {pc = GET_PC1 (P1,P2,P3);//center Double ans = get_ans (PC,PP,P1,P2,P3); printf ("Case%d:%.3lf\n", ++cas,ans); } return 0;}
CSU 1503: Distance from point to arc (computational geometry)