• uva 11178 Morley's Theorem (2D Geometry)


    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2119

      在uva跪了一个下午后提交这题,AC了。这题更简单,就是套入几何模板,求出交点就可以了。

      这题测试通过了几何模板中的相交判断并求出交点等几个函数。

    代码如下:

    View Code
      1  #include <cstdio>
      2  #include <cstring>
      3  #include <cmath>
      4  #include <vector>
      5  #include <iostream>
      6  #include <algorithm>
      7  
      8  using namespace std;
      9  
     10  #define REP(i, n) for (int i = 0; i < (n); i++)
     11  
     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  
     19  // basic calculations
     20  typedef Point Vec;
     21  Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
     22  Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
     23  Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
     24  Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);}
     25  
     26  const double eps = 1e-8;
     27  int sgn(double x) { return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1);}
     28  bool operator < (Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y);}
     29  bool operator == (Point a, Point b) { return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}
     30  
     31  double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
     32  double vecLen(Vec x) { return sqrt(sqr(x.x) + sqr(x.y));}
     33  double angle(Vec a, Vec b) { return acos(dotDet(a, b) / vecLen(a) / vecLen(b));}
     34  double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
     35  double triArea(Point a, Point b, Point c) { return fabs(crossDet(b - a, c - a));}
     36  Vec rotate(Vec x, double rad) { return Vec(x.x * cos(rad) - x.y * sin(rad), x.x * sin(rad) + x.y * cos(rad));}
     37  Vec normal(Vec x) {
     38      double len = vecLen(x);
     39      return Vec(- x.y / len, x.x / len);
     40  }
     41  
     42  struct Line {
     43      Point s, t;
     44      Line() {}
     45      Line(Point s, Point t) : s(s), t(t) {}
     46  } ;
     47  typedef Line Seg;
     48  
     49  bool onSeg(Point x, Point a, Point b) { return sgn(crossDet(a - x, b - x)) == 0 && sgn(dotDet(a - x, b - x)) < 0;}
     50  bool onSeg(Point x, Seg s) { return onSeg(x, s.s, s.t);}
     51  // 0 : not intersect
     52  // 1 : proper intersect
     53  // 2 : improper intersect
     54  int segIntersect(Point a, Point c, Point b, Point d) {
     55      Vec v1 = b - a, v2 = c - b, v3 = d - c, v4 = a - d;
     56      int a_bc = sgn(crossDet(v1, v2));
     57      int b_cd = sgn(crossDet(v2, v3));
     58      int c_da = sgn(crossDet(v3, v4));
     59      int d_ab = sgn(crossDet(v4, v1));
     60      if (a_bc * c_da > 0 && b_cd * d_ab > 0) return 1;
     61      if (onSeg(b, a, c) && c_da) return 2;
     62      if (onSeg(c, b, d) && d_ab) return 2;
     63      if (onSeg(d, c, a) && a_bc) return 2;
     64      if (onSeg(a, d, b) && b_cd) return 2;
     65      return 0;
     66  }
     67  int segIntersect(Seg a, Seg b) { return segIntersect(a.s, a.t, b.s, b.t);}
     68  
     69  // point of the intersection of 2 lines
     70  Point lineIntersect(Point P, Vec v, Point Q, Vec w) {
     71      Vec u = P - Q;
     72      double t = crossDet(w, u) / crossDet(v, w);
     73      return P + v * t;
     74  }
     75  Point lineIntersect(Line a, Line b) { return lineIntersect(a.s, a.t - a.s, b.s, b.t - b.s);}
     76  
     77  // directed distance
     78  double pt2Line(Point x, Point a, Point b) {
     79      Vec v1 = b - a, v2 = x - a;
     80      return crossDet(v1, v2) / vecLen(v1);
     81  }
     82  double pt2Line(Point x, Line L) { return pt2Line(x, L.s, L.t);}
     83  
     84  double pt2Seg(Point x, Point a, Point b) {
     85      if (a == b) return vecLen(x - a);
     86      Vec v1 = b - a, v2 = x - a, v3 = x - b;
     87      if (sgn(dotDet(v1, v2)) < 0) return vecLen(v2);
     88      if (sgn(dotDet(v1, v3)) > 0) return vecLen(v3);
     89      return fabs(crossDet(v1, v2)) / vecLen(v1);
     90  }
     91  double pt2Seg(Point x, Seg s) { return pt2Seg(x, s.s, s.t);}
     92  
     93  struct Poly {
     94      vector<Point> pt;
     95      Poly() {}
     96      Poly(vector<Point> pt) : pt(pt) {}
     97      double area() {
     98          double ret = 0.0;
     99          int sz = pt.size();
    100          for (int i = 1; i < sz; i++) {
    101              ret += crossDet(pt[i], pt[i - 1]);
    102          }
    103          return fabs(ret / 2.0);
    104      }
    105  } ;
    106  
    107  /****************** template above *******************/
    108  
    109   Point p[3];
    110   
    111   Point cal(Point a, Point b, Point c) {
    112       double angB = fabs(angle(c - b, a - b) / 3.0);
    113       double angC = fabs(angle(b - c, a - c) / 3.0);
    114       Vec v1, v2;
    115       v1 = rotate(c - b, angB);
    116       v2 = rotate(b - c, -angC);
    117   //    return lineIntersect(Line(b, b + v1), Line(c, c + v2));
    118       return lineIntersect(b, v1, c, v2);
    119   }
    120   
    121   int main() {
    122   //    freopen("in", "r", stdin);
    123       int n;
    124       cin >> n;
    125       while (n--) {
    126           for (int i = 0; i < 3; i++) {
    127               cin >> p[i].x >> p[i].y;
    128           }
    129           for (int i = 0; i < 3; i++) {
    130               Point tmp = cal(p[i % 3], p[(i + 1) % 3], p[(i + 2) % 3]);
    131               if (i) putchar(' ');
    132               printf("%.10f %.10f", tmp.x, tmp.y);
    133           }
    134           cout << endl;
    135       }
    136       return 0;
    137   }

    ——written by Lyon

  • 相关阅读:
    3月6日
    2月28日
    2月23日
    2月20日
    2月19日
    2月18日
    2月17日
    2月16日
    2月15日
    面试算法题——硬币找零
  • 原文地址:https://www.cnblogs.com/LyonLys/p/uva_11178_Lyon.html
Copyright © 2020-2023  润新知