點是否在多變形內

來源:互聯網
上載者:User

int PtInRgn(POINT *xy, int points, POINT *p)<br /> //xy--多邊形頂點座標串地址;points--頂點數;p—任一點座標<br /> // return(0)--點p在輪廓上;return(1)--點p在多邊形內;return(-1)--點p在多邊形外<br /> {<br /> int i, CrossPoints=0;<br /> int xmin, ymin, xmax, ymax;<br /> double x; </p><p> for(i=0; i<points-1; i++)<br /> {<br /> xmin=min((xy+i)->x, (xy+i+1)->x);<br /> xmax=max((xy+i)->x, (xy+i+1)->x);<br /> ymin=min((xy+i)->y, (xy+i+1)->y);<br /> ymax=max((xy+i)->y, (xy+i+1)->y);<br /> if(p->y>ymax || p->y<ymin) continue; //水平射線不可能與輪廓線段i->i+1有交<br /> if(p->y==ymax)<br /> {<br /> if(ymax==(xy+i)->y && p->x==(xy+i)->x || ymax==(xy+i+1)->y &&<br /> p->x==(xy+i+1)->x)<br /> return(0); // p位於多邊形輪廓上<br /> else if(ymin==ymax)<br /> {<br /> if(p->x>=xmin && p->x<=xmax) return(0); // p位於多邊形輪廓上<br /> }<br /> continue; // 放棄求交(避免重複交點)<br /> }<br /> //計算水平射線與輪廓線段i->i+1交點的x值<br /> x=(double)(p->y-(xy+i)->y)/((xy+i+1)->y-(xy+i)->y)*((xy+i+1)->x-(xy+i)->x)+(xy+i)->x;<br /> if(x==p->x) return(0); // p位於多邊形輪廓上<br /> if(x>p->x) CrossPoints++; // 統計位於p右方的交點數<br /> }<br /> if(CrossPoints%2==0) return(-1); // p在多邊形外<br /> else return(1); // p在多邊形內<br /> }<br />  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.