Topic Link: Click to open the link
Test instructions
4 points on a given two-dimensional coordinate
Ask:
Find a point to make this point distance from 4 points and the minimum
Output distance and.
Ideas:
If 4 points are not convex 4-edged. Must be the optimal endpoint.
Otherwise, the intersection of 2 diagonal lines is optimal, can be simply proved.
For a convex 4-edged shape, sort the first corner.
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std; typedef double LL;CONST int n = 5;int n = 4;double x[n], y[n];struct Point {ll x, y, dis;} s[4], P0;ll dis (ll x1, ll y 1, ll x2, ll y2) {return (X1-X2) * (X1-X2) + (y1-y2) * (Y1-Y2);} int Cmp_polarangel (struct point p1, struct point p2, struct point pb) {double delta= (p1.x-pb.x) * (P2.Y-PB.Y)-(p2.x-pb.x) * (P1.Y-PB.Y); if (delta<0.0) return 1; else if (delta==0.0) return 0; else return-1;} BOOL Is_leftturn (struct point P3, struct-point P2, struct-point p1) {int Type=cmp_polarangel (P3, p1, p2); if (type<0) return true; return false;} int Cmp (const VOID*P1, const VOID*P2) {struct point*a1= (struct point*) P1; struct point*a2= (struct point*) P2; int Type=cmp_polarangel (*A1, *A2, p0); if (type<0) return-1; else if (type==0) {if (A1->dis<a2->dis) return-1; else if (A1->dis==a2->dis) return 0; else return 1; } else return 1;} Double cal (double x1, double y1, double x2, double y2) {return sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2));} int main () {while (~scanf ("%lf%lf%lf%lf%lf%lf%lf%lf", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2], &X[3], &y[3]) {if (x[0] = =-1) break;double ans = 1e10, di;for (int i = 0; i < n; i + +) {di = 0.0;for (int j = 0; J < N; J + +) {if (j = = i) Continue;di + = Cal (X[i], y[i], x[j], y[j]);} ans = min (ans, di);} Double xx = x[0] + x[1] + x[2] + x[3];d ouble yy = y[0] + y[1] + y[2] + y[3];d i = 0.0;for (int j = 0; J < N; j + +) {di + = Cal (XX/4, YY/4, X[j], y[j]);} ans = min (ans, di);p 0.x = x[0], p0.y = y[0];for (int i = 0; i < 4; i + +) {s[i].x = X[i];s[i].y = Y[i];} for (int i = 0; i < n; i + +) {S[i].dis = cal (s[0].x, S[0].Y, s[i].x, s[i].y);} Qsort (s+1, n-1, sizeof (struct point), Cmp); x[0] = s[0].x; Y[0] = s[0].y;x[1] = s[2].x; Y[1] = s[2].y;x[2] = s[1].x; Y[2] = s[1].y;x[3] = s[3].x; Y[3] = s[3].y;double K1 = (y[0]-y[1])/(X[0]-x[1]);d ouble k2 = (y[3]-y[2])/(X[3]-x[2]);d ouble ansx, ansy;if (x[0] = = X[1]) {ansx = X[0];if (x[2] = = X[3]) {Ansy = yy /4;} else {Ansy = K2 * (ansx-x[2]) + y[2];}} else {if (x[2] = = X[3]) {ansx = X[2];ansy = K1 * (ansx-x[1]) + y[1];} else {if (k1! = K2) {ANSX = (y[2]-y[1] + k1*x[1] -k2*x[2])/(K1-K2); ansy = k1* (ansx-x[1]) + y[1];} else {ansx = 1000;ansy = 1000;}}} DI = 0.0;for (int j = 0; J < N; j + +) {di + = cal (Ansx, Ansy, X[j], y[j]);} ans = min (ans, di);p rintf ("%.4f\n", ans); return 0;}
Uvalive 5102 Fermat Point in quadrangle Polar Sort + Find a distance two-dimensional coordinates 4 points recent points