hdu 1140 War on Weather (3DGeometry)

Problem - 1140

  简单的三维几何题,对于每一个点,搜索是否存在卫星直接可见,也就是直线与球体是否交于较近的点。其中,套用的主要是直线与平面交点的函数。

模板以及代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cmath>
  4 #include <set>
  5 #include <vector>
  6 #include <iostream>
  7 #include <algorithm>
  8 
  9 using namespace std;
 10 
 11 // Point class
 12 struct Point {
 13     double x, y;
 14     Point() {}
 15     Point(double x, double y) : x(x), y(y) {}
 16 } ;
 17 template<class T> T sqr(T x) { return x * x;}
 18 inline double ptDis(Point a, Point b) { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}
 19 
 20 // basic calculations
 21 typedef Point Vec;
 22 Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
 23 Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
 24 Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
 25 Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);}
 26 
 27 const double EPS = 1e-8;
 28 const double PI = acos(-1.0);
 29 inline int sgn(double x) { return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}
 30 bool operator < (Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y);}
 31 bool operator == (Point a, Point b) { return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}
 32 
 33 inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
 34 inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
 35 inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
 36 inline double vecLen(Vec x) { return sqrt(dotDet(x, x));}
 37 inline double toRad(double deg) { return deg / 180.0 * PI;}
 38 inline double angle(Vec v) { return atan2(v.y, v.x);}
 39 inline double angle(Vec a, Vec b) { return acos(dotDet(a, b) / vecLen(a) / vecLen(b));}
 40 inline double triArea(Point a, Point b, Point c) { return fabs(crossDet(b - a, c - a));}
 41 inline Vec vecUnit(Vec x) { return x / vecLen(x);}
 42 inline Vec rotate(Vec x, double rad) { return Vec(x.x * cos(rad) - x.y * sin(rad), x.x * sin(rad) + x.y * cos(rad));}
 43 Vec normal(Vec x) {
 44     double len = vecLen(x);
 45     return Vec(- x.y / len, x.x / len);
 46 }
 47 
 48 // Line class
 49 struct Line {
 50     Point s, t;
 51     Line() {}
 52     Line(Point s, Point t) : s(s), t(t) {}
 53     Point point(double x) {
 54         return s + (t - s) * x;
 55     }
 56     Line move(double x) { // while x > 0 move to (s->t)'s left
 57         Vec nor = normal(t - s);
 58         nor = nor * x;
 59         return Line(s + nor, t + nor);
 60     }
 61     Vec vec() { return t - s;}
 62 } ;
 63 typedef Line Seg;
 64 
 65 inline bool onLine(Point x, Point a, Point b) { return sgn(crossDet(a - x, b - x)) == 0;}
 66 inline bool onLine(Point x, Line l) { return onLine(x, l.s, l.t);}
 67 inline bool onSeg(Point x, Point a, Point b) { return sgn(crossDet(a - x, b - x)) == 0 && sgn(dotDet(a - x, b - x)) < 0;}
 68 inline bool onSeg(Point x, Seg s) { return onSeg(x, s.s, s.t);}
 69 
 70 // 0 : not intersect
 71 // 1 : proper intersect
 72 // 2 : improper intersect
 73 int segIntersect(Point a, Point c, Point b, Point d) {
 74     Vec v1 = b - a, v2 = c - b, v3 = d - c, v4 = a - d;
 75     int a_bc = sgn(crossDet(v1, v2));
 76     int b_cd = sgn(crossDet(v2, v3));
 77     int c_da = sgn(crossDet(v3, v4));
 78     int d_ab = sgn(crossDet(v4, v1));
 79     if (a_bc * c_da > 0 && b_cd * d_ab > 0) return 1;
 80     if (onSeg(b, a, c) && c_da) return 2;
 81     if (onSeg(c, b, d) && d_ab) return 2;
 82     if (onSeg(d, c, a) && a_bc) return 2;
 83     if (onSeg(a, d, b) && b_cd) return 2;
 84     return 0;
 85 }
 86 inline int segIntersect(Seg a, Seg b) { return segIntersect(a.s, a.t, b.s, b.t);}
 87 
 88 // point of the intersection of 2 lines
 89 Point lineIntersect(Point P, Vec v, Point Q, Vec w) {
 90     Vec u = P - Q;
 91     double t = crossDet(w, u) / crossDet(v, w);
 92     return P + v * t;
 93 }
 94 inline Point lineIntersect(Line a, Line b) { return lineIntersect(a.s, a.t - a.s, b.s, b.t - b.s);}
 95 
 96 // Warning: This is a DIRECTED Distance!!!
 97 double pt2Line(Point x, Point a, Point b) {
 98     Vec v1 = b - a, v2 = x - a;
 99     return crossDet(v1, v2) / vecLen(v1);
100 }
101 inline double pt2Line(Point x, Line L) { return pt2Line(x, L.s, L.t);}
102 
103 double pt2Seg(Point x, Point a, Point b) {
104     if (a == b) return vecLen(x - a);
105     Vec v1 = b - a, v2 = x - a, v3 = x - b;
106     if (sgn(dotDet(v1, v2)) < 0) return vecLen(v2);
107     if (sgn(dotDet(v1, v3)) > 0) return vecLen(v3);
108     return fabs(crossDet(v1, v2)) / vecLen(v1);
109 }
110 inline double pt2Seg(Point x, Seg s) { return pt2Seg(x, s.s, s.t);}
111 
112 Point ptOnLine(Point p, Point a, Point b) {
113     Vec v = b - a;
114     return a + v * (dotDet(v, p - a) / dotDet(v, v));
115 }
116 inline Point ptOnLine(Point p, Line x) { return ptOnLine(p, x.s, x.t);}
117 
118 // Polygon class
119 struct Poly {
120     vector<Point> pt;
121     Poly() { pt.clear();}
122     ~Poly() {}
123     Poly(vector<Point> pt) : pt(pt) {}
124     Point operator [] (int x) const { return pt[x];}
125     int size() { return pt.size();}
126     double area() {
127         double ret = 0.0;
128         int sz = pt.size();
129         for (int i = 1; i < sz; i++) {
130             ret += crossDet(pt[i], pt[i - 1]);
131         }
132         return fabs(ret / 2.0);
133     }
134 } ;
135 
136 // Circle class
137 struct Circle {
138     Point c;
139     double r;
140     Circle() {}
141     Circle(Point c, double r) : c(c), r(r) {}
142     Point point(double a) {
143         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
144     }
145 } ;
146 
147 inline bool ptOnCircle(Point x, Circle c) { return sgn(ptDis(c.c, x) - c.r) == 0;}
148 
149 // Cirlce operations
150 int lineCircleIntersect(Line L, Circle C, double &t1, double &t2, vector<Point> &sol) {
151     double a = L.s.x, b = L.t.x - C.c.x, c = L.s.y, d = L.t.y - C.c.y;
152     double e = sqr(a) + sqr(c), f = 2 * (a * b + c * d), g = sqr(b) + sqr(d) - sqr(C.r);
153     double delta = sqr(f) - 4.0 * e * g;
154     if (sgn(delta) < 0) return 0;
155     if (sgn(delta) == 0) {
156         t1 = t2 = -f / (2.0 * e);
157         sol.push_back(L.point(t1));
158         return 1;
159     }
160     t1 = (-f - sqrt(delta)) / (2.0 * e);
161     sol.push_back(L.point(t1));
162     t2 = (-f + sqrt(delta)) / (2.0 * e);
163     sol.push_back(L.point(t2));
164     return 2;
165 }
166 
167 int lineCircleIntersect(Line L, Circle C, vector<Point> &sol) {
168     Vec dir = L.t - L.s, nor = normal(dir);
169     Point mid = lineIntersect(C.c, nor, L.s, dir);
170     double len = sqr(C.r) - sqr(ptDis(C.c, mid));
171     if (sgn(len) < 0) return 0;
172     if (sgn(len) == 0) {
173         sol.push_back(mid);
174         return 1;
175     }
176     Vec dis = vecUnit(dir);
177     len = sqrt(len);
178     sol.push_back(mid + dis * len);
179     sol.push_back(mid - dis * len);
180     return 2;
181 }
182 
183 // -1 : coincide
184 int circleCircleIntersect(Circle C1, Circle C2, vector<Point> &sol) {
185     double d = vecLen(C1.c - C2.c);
186     if (sgn(d) == 0) {
187         if (sgn(C1.r - C2.r) == 0) {
188             return -1;
189         }
190         return 0;
191     }
192     if (sgn(C1.r + C2.r - d) < 0) return 0;
193     if (sgn(fabs(C1.r - C2.r) - d) > 0) return 0;
194     double a = angle(C2.c - C1.c);
195     double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (2.0 * C1.r * d));
196     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
197     sol.push_back(p1);
198     if (p1 == p2) return 1;
199     sol.push_back(p2);
200     return 2;
201 }
202 
203 void circleCircleIntersect(Circle C1, Circle C2, vector<double> &sol) {
204     double d = vecLen(C1.c - C2.c);
205     if (sgn(d) == 0) return ;
206     if (sgn(C1.r + C2.r - d) < 0) return ;
207     if (sgn(fabs(C1.r - C2.r) - d) > 0) return ;
208     double a = angle(C2.c - C1.c);
209     double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (2.0 * C1.r * d));
210     sol.push_back(a - da);
211     sol.push_back(a + da);
212 }
213 
214 int tangent(Point p, Circle C, vector<Vec> &sol) {
215     Vec u = C.c - p;
216     double dist = vecLen(u);
217     if (dist < C.r) return 0;
218     if (sgn(dist - C.r) == 0) {
219         sol.push_back(rotate(u, PI / 2.0));
220         return 1;
221     }
222     double ang = asin(C.r / dist);
223     sol.push_back(rotate(u, -ang));
224     sol.push_back(rotate(u, ang));
225     return 2;
226 }
227 
228 // ptA : points of tangency on circle A
229 // ptB : points of tangency on circle B
230 int tangent(Circle A, Circle B, vector<Point> &ptA, vector<Point> &ptB) {
231     if (A.r < B.r) {
232         swap(A, B);
233         swap(ptA, ptB);
234     }
235     double d2 = sqr(A.c.x - B.c.x) + sqr(A.c.y - B.c.y);
236     double rdiff = A.r - B.r, rsum = A.r + B.r;
237     if (d2 < sqr(rdiff)) return 0;
238     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
239     if (d2 == 0 && A.r == B.r) return -1;
240     if (d2 == sqr(rdiff)) {
241         ptA.push_back(A.point(base));
242         ptB.push_back(B.point(base));
243         return 1;
244     }
245     double ang = acos((A.r - B.r) / sqrt(d2));
246     ptA.push_back(A.point(base + ang));
247     ptB.push_back(B.point(base + ang));
248     ptA.push_back(A.point(base - ang));
249     ptB.push_back(B.point(base - ang));
250     if (d2 == sqr(rsum)) {
251         ptA.push_back(A.point(base));
252         ptB.push_back(B.point(PI + base));
253         return 3;
254     } else if (d2 > sqr(rsum)) {
255         ang = acos((A.r + B.r) / sqrt(d2));
256         ptA.push_back(A.point(base + ang));
257         ptB.push_back(B.point(PI + base + ang));
258         ptA.push_back(A.point(base - ang));
259         ptB.push_back(B.point(PI + base - ang));
260         return 4;
261     }
262     return 2;
263 }
264 
265 // -1 : onside
266 // 0 : outside
267 // 1 : inside
268 int ptInPoly(Point p, Poly &poly) {
269     int wn = 0, sz = poly.size();
270     for (int i = 0; i < sz; i++) {
271         if (onSeg(p, poly[i], poly[(i + 1) % sz])) return -1;
272         int k = sgn(crossDet(poly[(i + 1) % sz] - poly[i], p - poly[i]));
273         int d1 = sgn(poly[i].y - p.y);
274         int d2 = sgn(poly[(i + 1) % sz].y - p.y);
275         if (k > 0 && d1 <= 0 && d2 > 0) wn++;
276         if (k < 0 && d2 <= 0 && d1 > 0) wn--;
277     }
278     if (wn != 0) return 1;
279     return 0;
280 }
281 
282 // if DO NOT need a high precision
283 /*
284 int ptInPoly(Point p, Poly poly) {
285     int sz = poly.size();
286     double ang = 0.0, tmp;
287     for (int i = 0; i < sz; i++) {
288         if (onSeg(p, poly[i], poly[(i + 1) % sz])) return -1;
289         tmp = angle(poly[i] - p) - angle(poly[(i + 1) % sz] - p) + PI;
290         ang += tmp - floor(tmp / (2.0 * PI)) * 2.0 * PI - PI;
291     }
292     if (sgn(ang - PI) == 0) return -1;
293     if (sgn(ang) == 0) return 0;
294     return 1;
295 }
296 */
297 
298 
299 // Convex Hull algorithms
300 // return the number of points in convex hull
301 
302 // andwer's algorithm
303 // if DO NOT want the points on the side of convex hull, change all "<" into "<="
304 int andrew(Point *pt, int n, Point *ch) {
305     sort(pt, pt + n);
306     int m = 0;
307     for (int i = 0; i < n; i++) {
308         while (m > 1 && crossDet(ch[m - 1] - ch[m - 2], pt[i] - ch[m - 2]) <= 0) m--;
309         ch[m++] = pt[i];
310     }
311     int k = m;
312     for (int i = n - 2; i >= 0; i--) {
313         while (m > k && crossDet(ch[m - 1] - ch[m - 2], pt[i] - ch[m - 2]) <= 0) m--;
314         ch[m++] = pt[i];
315     }
316     if (n > 1) m--;
317     return m;
318 }
319 
320 // graham's algorithm
321 // if DO NOT want the points on the side of convex hull, change all "<=" into "<"
322 Point origin;
323 inline bool cmpAng(Point p1, Point p2) { return crossDet(origin, p1, p2) > 0;}
324 inline bool cmpDis(Point p1, Point p2) { return ptDis(p1, origin) > ptDis(p2, origin);}
325 
326 void removePt(Point *pt, int &n) {
327     int idx = 1;
328     for (int i = 2; i < n; i++) {
329         if (sgn(crossDet(origin, pt[i], pt[idx]))) pt[++idx] = pt[i];
330         else if (cmpDis(pt[i], pt[idx])) pt[idx] = pt[i];
331     }
332     n = idx + 1;
333 }
334 
335 int graham(Point *pt, int n, Point *ch) {
336     int top = -1;
337     for (int i = 1; i < n; i++) {
338         if (pt[i].y < pt[0].y || (pt[i].y == pt[0].y && pt[i].x < pt[0].x)) swap(pt[i], pt[0]);
339     }
340     origin = pt[0];
341     sort(pt + 1, pt + n, cmpAng);
342     removePt(pt, n);
343     for (int i = 0; i < n; i++) {
344         if (i >= 2) {
345             while (!(crossDet(ch[top - 1], pt[i], ch[top]) <= 0)) top--;
346         }
347         ch[++top] = pt[i];
348     }
349     return top + 1;
350 }
351 
352 
353 // Half Plane
354 // The intersected area is always a convex polygon.
355 // Directed Line class
356 struct DLine {
357     Point p;
358     Vec v;
359     double ang;
360     DLine() {}
361     DLine(Point p, Vec v) : p(p), v(v) { ang = atan2(v.y, v.x);}
362     bool operator < (const DLine &L) const { return ang < L.ang;}
363     DLine move(double x) { // while x > 0 move to v's left
364         Vec nor = normal(v);
365         nor = nor * x;
366         return DLine(p + nor, v);
367     }
368 
369 } ;
370 
371 Poly cutPoly(Poly &poly, Point a, Point b) {
372     Poly ret = Poly();
373     int n = poly.size();
374     for (int i = 0; i < n; i++) {
375         Point c = poly[i], d = poly[(i + 1) % n];
376         if (sgn(crossDet(b - a, c - a)) >= 0) ret.pt.push_back(c);
377         if (sgn(crossDet(b - a, c - d)) != 0) {
378             Point ip = lineIntersect(a, b - a, c, d - c);
379             if (onSeg(ip, c, d)) ret.pt.push_back(ip);
380         }
381     }
382     return ret;
383 }
384 inline Poly cutPoly(Poly &poly, DLine L) { return cutPoly(poly, L.p, L.p + L.v);}
385 
386 inline bool onLeft(DLine L, Point p) { return crossDet(L.v, p - L.p) > 0;}
387 Point dLineIntersect(DLine a, DLine b) {
388     Vec u = a.p - b.p;
389     double t = crossDet(b.v, u) / crossDet(a.v, b.v);
390     return a.p + a.v * t;
391 }
392 
393 Poly halfPlane(DLine *L, int n) {
394     Poly ret = Poly();
395     sort(L, L + n);
396     int fi, la;
397     Point *p = new Point[n];
398     DLine *q = new DLine[n];
399     q[fi = la = 0] = L[0];
400     for (int i = 1; i < n; i++) {
401         while (fi < la && !onLeft(L[i], p[la - 1])) la--;
402         while (fi < la && !onLeft(L[i], p[fi])) fi++;
403         q[++la] = L[i];
404         if (fabs(crossDet(q[la].v, q[la - 1].v)) < EPS) {
405             la--;
406             if (onLeft(q[la], L[i].p)) q[la] = L[i];
407         }
408         if (fi < la) p[la - 1] = dLineIntersect(q[la - 1], q[la]);
409     }
410     while (fi < la && !onLeft(q[fi], p[la - 1])) la--;
411     if (la - fi <= 1) return ret;
412     p[la] = dLineIntersect(q[la], q[fi]);
413     for (int i = fi; i <= la; i++) ret.pt.push_back(p[i]);
414     return ret;
415 }
416 
417 
418 // 3D Geometry
419 void getCoor(double R, double lat, double lng, double &x, double &y, double &z) {
420     lat = toRad(lat);
421     lng = toRad(lng);
422     x = R * cos(lat) * cos(lng);
423     y = R * cos(lat) * sin(lng);
424     z = R * sin(lat);
425 }
426 
427 struct Point3 {
428     double x, y, z;
429     Point3() {}
430     Point3(double x, double y, double z) : x(x), y(y), z(z) {}
431 } ;
432 typedef Point3 Vec3;
433 
434 Vec3 operator + (Vec3 a, Vec3 b) { return Vec3(a.x + b.x, a.y + b.y, a.z + b.z);}
435 Vec3 operator - (Vec3 a, Vec3 b) { return Vec3(a.x - b.x, a.y - b.y, a.z - b.z);}
436 Vec3 operator * (Vec3 a, double p) { return Vec3(a.x * p, a.y * p, a.z * p);}
437 Vec3 operator / (Vec3 a, double p) { return Vec3(a.x / p, a.y / p, a.z / p);}
438 
439 bool operator < (Point3 a, Point3 b) {
440     if (a.x != b.x) return a.x < b.x;
441     if (a.y != b.y) return a.y < b.y;
442     return a.z < b.z;
443 }
444 bool operator == (Point3 a, Point3 b) { return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0 && sgn(a.z - b.z) == 0;}
445 
446 inline double dotDet(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z;}
447 inline Vec3 crossDet(Vec3 a, Vec3 b) { return Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);}
448 inline double vecLen(Vec3 x) { return sqrt(dotDet(x, x));}
449 inline double angle(Vec3 a, Vec3 b) { return acos(dotDet(a, b) / vecLen(a) / vecLen(b));}
450 inline double ptDis(Point3 a, Point3 b) { return sqrt(vecLen(a - b));}
451 inline double triArea(Point3 a, Point3 b, Point3 c) { return vecLen(crossDet(b - a, c - a));}
452 Vec3 vecUnit(Vec3 x) { return x / vecLen(x);}
453 
454 struct Plane {
455     Point3 p;
456     Vec3 n;
457     Plane() {}
458     Plane(Point3 p, Vec3 n) : p(p), n(n) {}
459 } ;
460 
461 // Warning: This is a DIRECTED Distance!!!
462 inline double pt2Plane(Point3 p, Point3 p0, Vec3 n) { return dotDet(p - p0, n) / vecLen(n);}
463 inline double pt2Plane(Point3 p, Plane P) { return pt2Plane(p, P.p, P.n);}
464 // get projection on plane
465 inline Point3 ptOnPlane(Point3 p, Point3 p0, Vec3 n) { return p + n * pt2Plane(p, p0, n);}
466 inline Point3 ptOnPlane(Point3 p, Plane P) { return ptOnPlane(p, P.p, P.n);}
467 inline bool ptInPlane(Point3 p, Point3 p0, Vec3 n) { return sgn(dotDet(p - p0, n)) == 0;}
468 inline bool ptInPlane(Point3 p, Plane P) { return ptInPlane(p, P.p, P.n);}
469 
470 struct Line3 {
471     Point3 s, t;
472     Line3() {}
473     Line3(Point3 s, Point3 t) : s(s), t(t) {}
474     Vec3 vec() { return t - s;}
475 } ;
476 typedef Line3 Seg3;
477 
478 double pt2Line(Point3 p, Point3 a, Point3 b) {
479     Vec3 v1 = b - a, v2 = p - a;
480     return vecLen(crossDet(v1, v2)) / vecLen(v1);
481 }
482 inline double pt2Line(Point3 p, Line3 l) { return pt2Line(p, l.s, l.t);}
483 
484 double pt2Seg(Point3 p, Point3 a, Point3 b) {
485     if (a == b) return vecLen(p - a);
486     Vec3 v1 = b - a, v2 = p - a, v3 = p - b;
487     if (sgn(dotDet(v1, v2)) < 0) return vecLen(v2);
488     else if (sgn(dotDet(v1, v3)) > 0) return vecLen(v3);
489     else return vecLen(crossDet(v1, v2)) / vecLen(v1);
490 }
491 inline double pt2Seg(Point3 p, Seg3 s) { return pt2Seg(p, s.s, s.t);}
492 
493 struct Tri {
494     Point3 a, b, c;
495     Tri() {}
496     Tri(Point3 a, Point3 b, Point3 c) : a(a), b(b), c(c) {}
497 } ;
498 
499 bool ptInTri(Point3 p, Point3 a, Point3 b, Point3 c) {
500     double area1 = triArea(p, a, b);
501     double area2 = triArea(p, b, c);
502     double area3 = triArea(p, c, a);
503     double area = triArea(a, b, c);
504     return sgn(area1 = area2 + area3 - area) == 0;
505 }
506 inline bool ptInTri(Point3 p, Tri t) { return ptInTri(p, t.a, t.b, t.c);}
507 
508 // 0 : not intersect
509 // 1 : intersect at only 1 point
510 // 2 : line in plane
511 int linePlaneIntersect(Point3 s, Point3 t, Point3 p0, Vec3 n, Point3 &x) {
512     double res1 = dotDet(n, p0 - s);
513     double res2 = dotDet(n, t - s);
514     if (sgn(res2) == 0) {
515         if (ptInPlane(s, p0, n)) return 2;
516         return 0;
517     }
518     x = s + (t - s) * (res1 / res2);
519     // use general form : a * x + b * y + c * z + d = 0
520     // Vec3 v = s - t;
521     // double k = (a * s.x + b * s.y + c * s.z + d) / (a * v.x + b * v.y + c * v.z);
522     // x = s + (t - s) * k;
523     return 1;
524 }
525 inline int linePlaneIntersect(Line3 l, Plane p, Point3 &x) { return linePlaneIntersect(l.s, l.t, p.p, p.n, x);}
526 
527 Point3 ptOnLine(Point3 p, Line3 L) {
528     Plane P = Plane(p, L.t - L.s);
529     Point3 ret;
530     if (linePlaneIntersect(L, P, ret) != 1) puts("Shit!");
531     return ret;
532 }
533 
534 // ¡÷abc intersect with segment st
535 bool triSegIntersect(Point3 a, Point3 b, Point3 c, Point3 s, Point3 t, Point3 &x) {
536     Vec3 n = crossDet(b - a, c - a);
537     if (sgn(dotDet(n, t - s)) == 0) return false;
538     else {
539         double k = dotDet(n, a - s) / dotDet(n, t - s);
540         if (sgn(k) < 0 || sgn(k - 1) > 0) return false;
541         x = s + (t - s) * k;
542         return ptInTri(x, a, b, c);
543     }
544 }
545 inline bool triSegIntersect(Tri t, Line3 l, Point3 &x) { return triSegIntersect(t.a, t.b, t.c, l.s, l.t, x);}
546 
547 // Warning: This is a DIRECTED Volume!!!
548 inline double tetraVol(Point3 a, Point3 b, Point3 c, Point3 p) { return dotDet(p - a, crossDet(b - a, c - a)) / 6.0;}
549 inline double tetraVol(Tri t, Point3 p) { return tetraVol(t.a, t.b, t.c, p);}
550 
551 struct Ball {
552     Point3 c;
553     double r;
554     Ball() {}
555     Ball(Point3 c, double r) : c(c), r(r) {}
556 } ;
557 
558 int lineBallIntersect(Ball B, Line3 L, vector<Point3> &sol) {
559     double dis = pt2Line(B.c, L);
560     if (dis > B.r) return 0;
561     Point3 ip = ptOnLine(B.c, L);
562     if (sgn(dis - B.r)) {
563         double ds = sqrt(sqr(B.r) - sqr(dis));
564         sol.push_back(ip + vecUnit(L.t - L.s) * ds);
565         sol.push_back(ip - vecUnit(L.t - L.s) * ds);
566         return 2;
567     } else {
568         sol.push_back(ip);
569         return 1;
570     }
571 }
572 
573 /****************** template above *******************/
574 
575 const int N = 111;
576 const double FINF = 1e100;
577 Point3 sate[N];
578 
579 bool test(Point3 x, Point3 st) {
580     vector<Point3> ips;
581     ips.clear();
582     int c = lineBallIntersect(Ball(Point3(0.0, 0.0, 0.0), vecLen(x)), Line3(st, x), ips);
583     if (c == 0) return false;
584     if (c == 1) return true;
585     double mini = FINF;
586     for (int i = 0; i < 2; i++) {
587         mini = min(mini, ptDis(st, ips[i]));
588     }
589     return sgn(ptDis(st, x) - mini) == 0;
590 }
591 
592 bool check(Point3 x, int n) {
593     for (int i = 0; i < n; i++) {
594         if (test(x, sate[i])) return true;
595     }
596     return false;
597 }
598 
599 int main() {
600 //    freopen("in", "r", stdin);
601     int n, m;
602     while (cin >> n >> m && (n || m)) {
603         double x, y, z;
604         for (int i = 0; i < n; i++) {
605             cin >> x >> y >> z;
606             sate[i] = Point3(x, y, z);
607         }
608         int cnt = 0;
609         for (int i = 0; i < m; i++) {
610             cin >> x >> y >> z;
611             if (check(Point3(x, y, z), n)) cnt++;
612         }
613         cout << cnt << endl;
614     }
615     return 0;
616 }
View Code

  至此,hdu分类中的几道基础几何已经切完。

P.S.: 模板有更新,之前的模板在点到直线上的投影一函数中有错。

——written by Lyon

原文地址:https://www.cnblogs.com/LyonLys/p/hdu_1140_Lyon.html