• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Why so serious? --[NKU]schindlerlee

            2010年1月24日星期日.sgu129 求線段在凸多邊形中的長度

            2010年1月24日星期日.sgu129

            sgu129:其實不難,求線段在凸多邊形中的長度
            雖然是基礎計算幾何問題,但是請看題目通過人數:
            129     Inheritance    357    +

            在第一頁最前邊,才這么點人過,說明這道題很有點意思。

            我也看了網上很多人的解題報告,幾乎眾口一辭的說是精度問題,但是我不同意。
            首先題目中已經說了都是整點,所以,完全可以利用整數的性質回避掉精度的問題。


            double proc()
            {
              如果線段和一條凸包的邊所在的直線重合,return 0;

              如果凸包的端點在這條線段上,p[cnt++] = intersect_point;

              如果凸包的一條線段和這條線段相交,且交點不是凸包先端的端點,
                p[cnt++] = intersect_point;

              if(cnt == 0) { //全內或全外
                  if (inPoly(a) && inPoly(b)) { return dist(a,b); }
              }else if (cnt == 1) {  //一個交點,此種情況也可能為0
                  if (inPoly(a)) { return dist(a,p[0]); }
                  if (inPoly(b)) { return dist(b,p[0]); }
              }else {
                  return dist(p[0],p[1]);
              }
              return 0;
            }

            最丑的第一次ac的代碼就不貼了,貼一下很"靚"的沒有用dcmp的代碼
              1 
              2 /*
              3  * SOUR:sgu129
              4  * ALGO:computational geometry
              5  * DATE: 2010年 01月 20日 星期三 22:24:30 CST
              6  * COMM:5 http://www.shnenglu.com/schindlerlee
              7  * 其實都是整點,精度控制可以完全不用dcmp
              8  * */
              9 #include<iostream>
             10 #include<cstdio>
             11 #include<cstdlib>
             12 #include<cstring>
             13 #include<algorithm>
             14 #include<cmath>
             15 using namespace std;
             16 typedef long long LL;
             17 const int maxint = 0x7fffffff;
             18 const long long max64 = 0x7fffffffffffffffll;
             19 
             20 const int N = 1024;
             21 struct point_t {
             22     double x, y;
             23     point_t() {
             24     } point_t(double a, double b) {
             25         x = a, y = b;
             26     }
             27 } p[N], st[N],line[2];
             28 
             29 double sqr(double x) { return x * x;}
             30 point_t operator +(point_t a, point_t b) { return point_t(a.x + b.x, a.y + b.y); }
             31 point_t operator -(point_t a, point_t b) { return point_t(a.x - b.x, a.y - b.y); }
             32 double dot_mul(point_t a, point_t b) { return a.x * b.x + a.y * b.y; }
             33 double cross_mul(point_t a, point_t b) { return a.x * b.y - a.y * b.x; }
             34 double cross_mul(point_t a, point_t b, point_t c) { return cross_mul(a - c, b - c); }
             35 
             36 double dist(double ax,double ay,double bx,double by) { return sqrt(sqr(ax-bx) + sqr(ay-by));}
             37 double dist(point_t a) { return sqrt(sqr(a.x) + sqr(a.y));}
             38 double dist(point_t a,point_t b) { return dist(a-b);}
             39 int m, n, top;
             40 bool cmp(point_t a, point_t b) { return cross_mul(a, b, p[0]) > 0; }
             41 
             42 void graham()
             43 {
             44   int i;
             45   top = 0;
             46   for (i = 1; i < n; i++) {
             47       if (p[i].y < p[0].y) {
             48           swap(p[i], p[0]);
             49       } else if (p[i].y == p[0].y && p[i].x < p[0].x) {
             50           swap(p[i], p[0]);
             51       }
             52   }
             53   sort(p + 1, p + n, cmp);
             54   st[0= p[0];
             55   st[1= p[1];
             56   top = 2;
             57   for (i = 2; i < n; i++) {
             58       if (cross_mul(p[i], st[top - 1], st[top - 2]) <= 0) {
             59           st[top++= p[i];
             60       } else {
             61           top--;
             62       }
             63   }
             64   st[top++= st[0];
             65 }
             66 
             67 bool inPoly (point_t pt)
             68 {
             69   for (int i = 0;i < top - 1;i++) {
             70       point_t a = st[i];
             71       point_t b = st[i+1];
             72       if (cross_mul(b,pt,a) <= 0) {
             73           return false;
             74       }
             75   }
             76   return true;
             77 }
             78 
             79 bool between(point_t a,point_t bg,point_t ed)
             80 {
             81   if (a.x >= min(bg.x,ed.x) && a.x <= max(bg.x,ed.x) &&
             82       a.y >= min(bg.y,ed.y) && a.y <= max(bg.y,ed.y)) {
             83       return 1;
             84   }
             85   return 0;
             86 }
             87 
             88 bool onSeg(point_t a,point_t b,point_t c) //a is on bc
             89 {
             90   if(0 == cross_mul(a,b,c)) {
             91       if(between(a,b,c)) {
             92         return true;
             93       } else {
             94           return false;
             95       }
             96   }
             97   return false;
             98 }
             99 
            100 bool intersect(point_t a,point_t b,point_t c,point_t d,double &x,double &y)
            101 {
            102   double r1,r2;
            103   if (cross_mul(a,c,d) * cross_mul(b,c,d) < 0 &&
            104       (r1=cross_mul(c,a,b)) * (r2=cross_mul(d,a,b)) <= 0) { //!! 注意是 <= 0
            105       r1 = fabs(r1), r2 = fabs(r2);
            106       x = c.x + (d.x - c.x) * (r1/(r1+r2));
            107       y = c.y + (d.y - c.y) * (r1/(r1+r2));
            108       return true;
            109   }
            110   return false;
            111 }
            112 
            113 double proc(point_t bg,point_t ed)
            114 {
            115   int i,j;
            116   for (i = 0;i < top - 1;i ++) {
            117       point_t a = st[i];
            118       point_t b = st[i+1];
            119       if(cross_mul(a,bg,b) == 0 && cross_mul(a,ed,b) == 0//在一條直線上
            120         return 0;
            121   }
            122   double x[2],y[2],tx,ty;
            123   int cnt = 0;
            124 
            125   for (i = 0;i < top - 1;i++) {
            126       point_t a = st[i];
            127       //if (cross_mul(bg,a,ed) == 0 && between(a,bg,ed)) {
            128       if (onSeg(a,bg,ed)) {
            129           x[cnt] = a.x, y[cnt] = a.y, cnt++;
            130       }
            131   }
            132 
            133   for (i = 0;i < top - 1;i++) {
            134       point_t a = st[i];
            135       point_t b = st[i+1];
            136       if (intersect(a,b,bg,ed,tx,ty)) {
            137           x[cnt] = tx, y[cnt] = ty, cnt++;
            138       }
            139   }
            140   if (cnt == 0) {
            141       if (inPoly(bg) && inPoly(ed)) {
            142           return dist(bg,ed);
            143       }
            144   }else if (cnt == 1) {
            145       if (inPoly(bg)) { return dist(x[0],y[0],bg.x,bg.y); }
            146       if (inPoly(ed)) { return dist(x[0],y[0],ed.x,ed.y); }
            147   }else if (cnt == 2) {
            148       return dist(x[0],y[0],x[1],y[1]);
            149   }
            150   return 0;
            151   }
            152 
            153   int main()
            154     {
            155       int i, j, k;
            156       scanf("%d"&n);
            157       for (i = 0; i < n; i++) {
            158           scanf("%lf%lf"&p[i].x, &p[i].y);
            159       }
            160       graham();
            161 
            162       scanf("%d"&m);
            163       while (m--) {
            164           scanf("%lf%lf",&line[0].x,&line[0].y);
            165           scanf("%lf%lf",&line[1].x,&line[1].y);
            166           printf("%f\n",proc(line[0],line[1]));
            167       }
            168       return 0;
            169     }
            170 


            posted on 2010-01-25 00:09 schindlerlee 閱讀(1771) 評論(0)  編輯 收藏 引用 所屬分類: 解題報告

            久久精品人人槡人妻人人玩AV| 国内精品欧美久久精品| 久久亚洲精品无码VA大香大香| 久久综合综合久久综合| 99久久精品国产高清一区二区| 精品久久久久久国产三级| 7777精品伊人久久久大香线蕉| 国产精品久久久久久福利69堂| 久久e热在这里只有国产中文精品99 | 久久久久国产精品嫩草影院| 色综合久久中文字幕无码| 91精品国产91久久久久久| 国产精品99久久久精品无码| 久久精品www| 久久精品国产日本波多野结衣 | 久久亚洲美女精品国产精品| 91久久精品国产91性色也| 国产美女亚洲精品久久久综合| 久久久精品免费国产四虎| 综合网日日天干夜夜久久| 久久无码人妻精品一区二区三区| 久久精品中文闷骚内射| 亚洲精品国产自在久久| 久久亚洲av无码精品浪潮| www.久久精品| 99久久国产热无码精品免费| 久久久久久精品久久久久| 亚洲v国产v天堂a无码久久| 久久精品国产福利国产秒| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 久久亚洲日韩精品一区二区三区 | 麻豆AV一区二区三区久久 | 一本久久a久久精品亚洲| 香蕉久久影院| 精品久久久久成人码免费动漫| 久久久精品久久久久特色影视| 国产精品久久久久一区二区三区| 日本一区精品久久久久影院| 国产精品一久久香蕉产线看| 夜夜亚洲天天久久| 国产精品99久久久久久宅男|