求任意多邊形的內點的算法

來源:http://www.faqs.org/faqs/graphics/algorithms-faq/  
   
  下面是原文:  
    Subject   2.06:   How   do   I   find   a   single   point   inside   a   simple   polygon?  
   
        Given   a   simple   polygon,   find   some   point   inside   it.     Here   is   a   method  
        based   on   the   proof   that   there   exists   an   internal   diagonal,   in  
        [O'Rourke   (C),   13-14].     The   idea   is   that   the   midpoint   of   a   diagonal  
        is   interior   to   the   polygon.  
     
        1.   Identify   a   convex   vertex   v;   let   its   adjacent   vertices   be   a   and   b.  
        2.   For   each   other   vertex   q   do:  
        2a.   If   q   is   inside   avb,   compute   distance   to   v   (orthogonal   to   ab).  
        2b.   Save   point   q   if   distance   is   a   new   min.  
        3.   If   no   point   is   inside,   return   midpoint   of   ab,   or   centroid   of   avb.  
        4.   Else   if   some   point   inside,   qv   is   internal:   return   its   midpoint.  
     
        Code   for   finding   a   diagonal   is   in   [O'Rourke   (C),   35-39],   and   is   part  
        of   many   other   software   packages.     See   Subject   0.07:   Where   is   all   the    
        source?  
   
  其實實現並不複雜,其實樓上各位的算法要實現起來   代碼量也不會比我的少。  
  下面是我以前寫的程序:  
  /*  
  作者:阿里  
  日期:2002.10  
    給定一簡單多邊形,找出一個肯定在該多邊形內的點  
  定理1:每個多邊形至少有一個凸頂點  
  定理2:頂點數>=4的簡單多邊形至少有一條對角線  
  結論: x座標最大,最小的點肯定是凸頂點  
              y座標最大,最小的點肯定是凸頂點                      
  */  
  POINT   a_point_insidepoly(int   vcount,POINT   polygon[])  
  {  
        POINT   v,a,b,r;  
        int   i,index;  
        v=polygon[0];  
        index=0;  
        for(i=1;i<vcount;i++)                       //尋找一個凸頂點,實際上最低點肯定是凸頂點  
              {  
                    if(polygon[i].y<v.y)  
                          {  
                                v=polygon[i];  
                                index=i;  
                          }  
              }  
        a=polygon[(index-1+vcount)%vcount];               //得到v的前一個頂點  
        b=polygon[(index+1)%vcount];                             //得到v的後一個頂點  
        POINT   tri[3],q;  
        tri[0]=a;tri[1]=v;tri[2]=b;  
        double   md=INF;  
        int   in1=index;  
        bool   bin=false;  
        for(i=0;i<vcount;i++)                                 //尋找在三角形avb內且離頂點v最近的頂點q  
              {  
                    if(i   ==   index)continue;  
                    if(i   ==   (index-1+vcount)%vcount)continue;  
                    if(i   ==   (index+1)%vcount)continue;  
                    if(!InsideConvexPolygon(3,tri,polygon[i]))continue;  
                    bin=true;  
                    if(dist(v,polygon[i])<md)  
                          {  
          q=polygon[i];  
  md=dist(v,q);  
                          }  
              }  
        if(!bin)                                                         //沒有頂點在三角形avb內,返回線段ab中點  
              {  
                    r.x=(a.x+b.x)/2;  
                    r.y=(a.y+b.y)/2;  
                    return   r;  
              }  
        r.x=(v.x+q.x)/2;                                         //返回線段vq的中點  
        r.y=(v.y+q.y)/2;  
        return   r;  
  }  
   
   
   
  上面的函數調用一個InsideConvexPolygon   函數,如下:  
  //   點q是凸多邊形polygon內時,返回true;注意:多邊形polygon一定要是凸多邊形    
  //   否則要用射線法來判斷,比較複雜  
  bool   InsideConvexPolygon(int   vcount,POINT   polygon[],POINT   q) //   可用於三角形!  
  {  
  POINT   p;  
  LINESEG   l;  
  int   i;  
  p.x=0;p.y=0;  
  for(i=0;i<vcount;i++)         //   尋找一個肯定在多邊形polygon內的點p:多邊形頂點平均值  
  {  
  p.x+=polygon[i].x;  
  p.y+=polygon[i].y;  
  }  
  p.x   /=   vcount;  
  p.y   /=   vcount;  
   
  for(i=0;i<vcount;i++)  
  {  
  l.s=polygon[i];l.e=polygon[(i+1)%vcount];  
  if(multiply(p,l.e,l.s)*multiply(q,l.e,l.s)<0)     /*   點p和點q在邊l的兩側,說明  
  點q肯定在多邊形外         */  
  break;  
  }  
  return   (i==vcount);  
  }  

 

原網頁:http://topic.csdn.net/t/20030328/09/1587418.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章