给出两个底面共面且不相交的正四棱锥, 求它们的顶点之间的最段路径的长度,要求只能经过正四棱锥的表面及其底面所在平面。
枚举最段路径穿过的底面的棱, 三分在这些棱上经过的点即可。
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) { }
};
typedef const Point& Pref;
Point operator+(Pref a, Pref b) { return {a.x + b.x, a.y + b.y}; }
Point operator-(Pref a, Pref b) { return {a.x - b.x, a.y - b.y}; }
Point operator*(double k, Pref p) { return {k * p.x, k * p.y}; }
double dis2(Pref a, Pref b) {
auto sq = [&](double x) { return x * x; };
return sq(a.x - b.x) + sq(a.y - b.y);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
auto get = [&](array<Point, 5>& a, Point& o, double& h) {
cin >> a[0].x >> a[0].y >> a[1].x >> a[1].y >> h;
a[2] = {a[1].x + a[0].y - a[1].y, a[1].y + a[1].x - a[0].x};
a[3] = {a[0].x + a[0].y - a[1].y, a[0].y + a[1].x - a[0].x};
a[4] = a[0];
o = 0.5 * (a[0] + a[2]);
};
array<Point, 5> a, b;
Point ao, bo;
double ah, bh;
get(a, ao, ah);
get(b, bo, bh);
auto findmin = [&](auto fval) {
double l = 0, r = 1, w;
while (r - l > eps) {
double mid = (l + r) / 2, nl = mid - eps, nr = mid + eps;
if ((w = fval(nl)) < fval(nr)) r = mid; else l = mid;
}
return w;
};
auto calc2 = [&](Pref p, Pref b0, Pref b1) {
return findmin([&](double x) {
Point bp = b0 + x * (b1 - b0);
return sqrt(dis2(p, bp)) + sqrt(dis2(bp, bo) + bh * bh);
});
};
auto calc1 = [&](Pref a0, Pref a1, Pref b0, Pref b1) {
return findmin([&](double x) {
Point ap = a0 + x * (a1 - a0);
return sqrt(dis2(ao, ap) + ah * ah) + calc2(ap, b0, b1);
});
};
double ans = 1e100;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ans = min(ans, calc1(a[i], a[i + 1], b[j], b[j + 1]));
}
}
cout << fixed << setprecision(9) << ans << '\n';
return 0;
}