#include <stdio.h>
#include <math.h>
typedef struct point {
double x;
double y;
} Point;
//거리함수
double get_dist(double time,Point a,Point b,Point c,Point d) {
//민호
double x1= a.x + (b.x-a.x) * time;
double y1= a.y + (b.y-a.y) * time;
//강호
double x2= c.x + (d.x-c.x) * time;
double y2= c.y + (d.y-c.y) * time;
return sqrt(pow(x2-x1,2) + pow(y2-y1,2));
}
int main() {
Point a,b,c,d;
scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
//시간 0부터 1까지
double low=0.0 ,high =1.0;
//삼분탐색
for(int i=0; i<100; i++) {
double m1=(2.0*low+high)/3.0;
double m2=(low+2.0*high)/3.0;
if(get_dist(m1,a,b,c,d) < get_dist (m2,a,b,c,d)) {
high = m2;
} else {
low = m1;
}
}
printf("%.10f\n", get_dist(low,a,b,c,d));
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: