The first thing that comes up with this question is whether we can use a mathematical method to obtain this point. For example, if we draw a circle with the smallest radius to make it intersection or tangent to all line segments... The center of the circle is to ask for... the idea seems to be okay... but there is no clue about how to come ~ After thinking for a long time, I didn't come up with how to implement it in a mathematical way...
I still use enumeration... The scope of the question is not large .. And the accuracy requirement is not high .. the entire (0, 0 )~ (100,100) the continuous space is discretely divided into 1000 vertices separated by 0.1. Enumeration of each vertices... the answer will be found. The complexity is N * O (100 ^ 2 )... It takes about 10 seconds to estimate the maximum data time. We can find that there is room for optimization... I used the binary classification directly. 9 vertices are enumerated at a time (9 vertices of the square average )... Find the last point and then narrow down the step with the center point of the nine points and try again 9 points .. the difference between two vertices in the histogram enumeration is <0. 01 .. that is, the step size <0.01...
Another important question is how to obtain the shortest distance from a point to a line segment ?.. There are two situations. The first is that this point will be dropped to this line segment... In this case, use the difference multiplication to find the triangle area composed of the point and the line segment * 2... Then divide by the length of this line segment is the length of the vertical line... The second case is that this point is beyond the line segment of the vertical line produced by the line segment. The shortest distance may only be the shortest distance between the two ends of the line segment. How can we determine the two cases... In fact, the second case occurs because the triangle formed by the point and line segment is an inactive triangle .. and not the two sides of this point are blunt angles .. is the other two corners, one of which is the blunt angle .. how to judge the right triangle .. first obtain the length of the three edges .. we all know the coordinates... When x1 ^ 2 + x2 ^ 2 <x3 ^ 2... It indicates that the angle between x1 and x2 is blunt...
Program:
/* ID: zzyzzy12 LANG: C++ TASK: fence3*/ #include<iostream> #include<istream> #include<stdio.h> #include<string.h> #include<math.h> #include<stack>#include<map>#include<algorithm> #include<queue> #define oo 2000000005 #define ll long long #define pi (atan(2)+atan(0.5))*2 using namespace std;struct node{ double x0,y0,x1,y1,len; }line[152];int i,n;double x,y,ansx,ansy,ans,now,MaxX,MaxY,StartX,StartY,len,step; double dis(node a){ double x1,y1,x2,y2; x1=(x-a.x0)*(x-a.x0)+(y-a.y0)*(y-a.y0); x2=(x-a.x1)*(x-a.x1)+(y-a.y1)*(y-a.y1); if (x1-x2-a.len*a.len>-0.001 || x2-x1-a.len*a.len>-0.001) { if (x1<x2) return sqrt(x1); return sqrt(x2); } x1=x-a.x0; y1=y-a.y0; x2=a.x1-a.x0; y2=a.y1-a.y0; x1=x1*y2-x2*y1; if (x1<0) x1=-x1; return x1/a.len;}int main() { freopen("fence3.in","r",stdin); freopen("fence3.out","w",stdout); StartX=StartY=oo; MaxX=MaxY=-oo; scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&line[i].x0,&line[i].y0,&line[i].x1,&line[i].y1); line[i].len=sqrt((line[i].x0-line[i].x1)*(line[i].x0-line[i].x1)+(line[i].y0-line[i].y1)*(line[i].y0-line[i].y1)); if (line[i].x1<line[i].x0) { x=line[i].x1; line[i].x1=line[i].x0; line[i].x0=x; } if (line[i].y1<line[i].y0) { y=line[i].y1; line[i].y1=line[i].y0; line[i].y0=y; } if (line[i].x1>MaxX) MaxX=line[i].x1; if (line[i].x0<StartX) StartX=line[i].x0; if (line[i].y1>MaxY) MaxY=line[i].y1; if (line[i].y0<StartY) StartY=line[i].y0; } if (MaxY>MaxX) len=MaxY/2; else len=MaxX/2; ans=oo; ans*=ans; while (len>0.01) { step=len/4; MaxX=StartX+len; MaxY=StartY+len; for (x=StartX;x<=MaxX;x+=step) for (y=StartY;y<=MaxY;y+=step) { now=0; for (i=1;i<=n;i++) { now+=dis(line[i]); if (now>ans) break; } if (now<ans) { ans=now; ansx=x; ansy=y; } } StartX=ansx-step/2; StartY=ansy-step/2; len/=2; } printf("%.1lf %.1lf %.1lf\n",ansx,ansy,ans); return 0; }