Skip to content

Instantly share code, notes, and snippets.

@not522
Created July 12, 2013 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save not522/5984663 to your computer and use it in GitHub Desktop.
Save not522/5984663 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<complex>
#include<cstdio>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
typedef long double D;
typedef complex<D> P;
struct L{P a, b;};
#define X real()
#define Y imag()
D det(P a, P b) {return a.X * b.Y - a.Y * b.X;}
P vec(L a) {return a.b - a.a;}
P pLL(L a, L b) {return a.a + vec(a) * (det(vec(b), b.a - a.a) / det(vec(b), vec(a)));}
int n;
P pp[11];
D l[11];
const D EPS = 1e-8;
D mysr(D a) {return sqrt(max(a, (D)0));}
bool can(P p, D h) {
rep (i, n) {
if (mysr(norm(p - pp[i]) + pow(h, 2)) > l[i] + EPS) {
return false;
}
}
return true;
}
int main() {
while (true) {
cin >> n;
if (n == 0) break;
rep (i, n) {
cin >> pp[i].X >> pp[i].Y >> l[i];
}
D res = 0;
rep (i, n) rep (j, i) {
D theta = acos((pow(l[i], 2) + norm(pp[i] - pp[j]) - pow(l[j], 2)) / (2 * l[i] * abs(pp[i] - pp[j])));
P btm = pp[i] + (pp[j] - pp[i]) / abs(pp[j] - pp[i]) * l[i] * cos(theta);
D h = l[i] * sin(theta);
if (can(btm, h)) res = max(res, h);
}
rep (i, n) {
if (can(pp[i], l[i])) res = max(res, l[i]);
}
rep (i, n) rep (j, i) rep (k, j) {
D theta1 = acos((pow(l[i], 2) + norm(pp[i] - pp[j]) - pow(l[j], 2)) / (2 * l[i] * abs(pp[i] - pp[j])));
P btm1 = pp[i] + (pp[j] - pp[i]) / abs(pp[j] - pp[i]) * l[i] * cos(theta1);
D theta2 = acos((pow(l[i], 2) + norm(pp[i] - pp[k]) - pow(l[k], 2)) / (2 * l[i] * abs(pp[i] - pp[k])));
P btm2 = pp[i] + (pp[k] - pp[i]) / abs(pp[k] - pp[i]) * l[i] * cos(theta2);
L l1 = (L){btm1, btm1 + (pp[i] - pp[j]) * P(0, 1)};
L l2 = (L){btm2, btm2 + (pp[i] - pp[k]) * P(0, 1)};
P ppp = pLL(l1, l2);
if (!can(ppp, 0)) continue;
D h = mysr(l[i] * l[i] - norm(pp[i] - ppp));
if (can(ppp, h)) res = max(res, h);
}
printf("%.12Lf\n", res);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment