




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、把點集凸包化Granham-Scan算法(使用水平序)#include#include#include#includeusing nameaace std;const int M=100000+5;struct Pointdouble x,y; pM;double dis(Point A,Point B)return sqrt(B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y);bool cmp(Point a,Point b)if (a.xb.x) return false;if (a.yb.y) return true;return false;double Xd
2、et(Point A,Point B,Point C)double x1,x2,y1,y2;x1=B.x-A.x;y1=B.y-A.y;x2=C.x-A.x;y2=C.y-A.y;return x1*y2-x2*y1; /大于0在左手邊,逆時針bool boM;Point ppM;int stackM; /from 1 to tvoid Gram_Scan(Point *p,int &n) /p從1-n,把點集土包化 n*log(n);int i,t;sort(p+1,p+1+n,cmp);for(t=0,i=1;i1 & pi.x=pi-1.x & pi.y=pi-1.y) continue
3、;p+t=pi;n=t;t=0;memset(bo+1,true,n*sizeof(bo0);if (n0)stack+t=1;bostackt=false;if (n1)stack+t=2;bostackt=false;if (n2)for(i=3;i=0) stack+t=i,boi=false;elsewhile(t=2& Xdet(pstackt-1,pstackt,pi)=1;i-)if (boi & Xdet(pstackt-1,pstackt,pi)=0) stack+t=i,boi=false;elsewhile(t=2&Xdet(pstackt-1,pstackt,pi)0)
4、 bostackt=true,t-;stack+t=i;bostackt=false;t-;for(i=1;i=t;i+)ppi=pstacki;memcpy(p+1,pp+1,t*sizeof(Point);n=t;求凸點集的面積double area(Point *p,int n)double sum=0;int i;pn+1=p1;for(i=1;i0?(x):-(x)EPS)struct Pointdouble x,y; pM;struct LINESEGPoint st,ed;struct LINEdouble a,b,c;double dist(Point a,Point b)re
5、turn sqrt(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);double multiply(Point a,Point b,Point c)return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);double dotmultiply(Point a,Point b,Point c)return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);bool online(LINESEG L,Point q) LINE makeline(Point p1,Point p2)LINE tl;tl.
6、a=p2.y-p1.y;tl.b=p1.x-p2.x;if (tl.ay?x:y;inline double MIN(double x,double y)return x=0& multiply(u.st,v.ed,v.st)*multiply(v.ed,u.ed,v.st)=0;bool intersect_A(LINESEG u,LINESEG v)return intersect(u,v) & !online(u,v.st) & !online(u,v.ed) & !online(v,u.st) & !online(u,v.ed);bool intersect_L(LINESEG u,L
7、INESEG v)return multiply(u.st,v.ed,v.st)*multiply(v.ed,u.ed,v.st)=0;bool intersection_line(LINE L1,LINE L2,Point &inter)if (fabs(det)EPS) return false;return true;bool intersection_lineseg(LINESEG u,LINESEG v,Point &inter)LINE L1,L2;L1=makeline(u.st,u.ed);L2=makeline(v.st,v.ed);if (intersection_line
8、(L1,L2,inter) return online(u,inter) & online(v,inter);else return false;bool intersection_LLS(LINE L1,LINESEG u,Point &inter)LINE L2;L2=makeline(u.st,u.ed);if (intersection_line(L1,L2,inter) return online(u,inter);else return false;int Inside(Point q,int n)Point q2;const int on_edge=0;int i=0,count
9、;pn=p0;while (in)for (count=0,i=0,q2.x=rand()+offset,q2.y=rand()+offset;in;i+)if (zero(multiply(q,pi,pi+1) & (pi.x-q.x)*(pi+1.x-q.x)EPS & (pi.y-q.y)*(pi+1.y-q.y)EPS)return on_edge;else if (zero(multiply(q,q2,pi)break;else if (multiply(q,pi,q2)*multiply(q,pi+1,q2)-EPS & multiply(pi,q,pi+1)*multiply(p
10、i,q2,pi+1)-EPS)count+;return count&1; bool cmp(Point p,Point q)if (p.xq.x) return false;if (p.yq.y) return true;return false;/線段與多邊形相交定義為至少有1個點在多邊形內(nèi),返回true; bool LINESEG_intersect_Polygon(LINESEG LS1,int n) LINESEG LS2;Point mid;int i;int j,top;Point stackM;pn=p000;for(i=0;in;i+)LS2.st=pi;LS2.ed=pi+
11、1;if (intersect_A(LS1,LS2) return true;top=0;stacktop+=LS1.st;stacktop+=LS1.ed;for(i=0;in;i+)if (online(LS1,pi) stacktop+=pi;sort(stack,stack+top,cmp);stacktop=stack0;for(j=0;jtop;j+)mid.x=(stackj.x+stackj+1.x)/2;mid.y=(stackj.y+stackj+1.y)/2;if (Inside(mid,n) return true;return false;const int M=15
12、000;struct Linedouble a,b,c;LM;struct Pointdouble x,y; pM,ppM;/由兩點化一般式Line LineChange(Point P1,Point P2) Line K;K.a=P2.y-P1.y;/(y2-y1)*x+(x1-x2)*y-(x1-x2)*y1-x1*(y2-y1)=0;counterclockK.b=P1.x-P2.x; return k;/求兩直線交點bool GetSegcross(Line K1,Line K2,Point &pp)if (fabs(det)1e-8) return 0;return 1;/p在直線上
13、返回0.00double p_on_line(Line K,Point p) /求兩點的垂直平分線void MidLine(Point P1,Point P2,Line &K)K.a=2*(P1.x-P2.x);K.b=2*(P1.y-P2.y);K.c=(-K.a*(P1.x+P2.x)-K.b*(P1.y+P2.y)/2;/求任意點對的最小距離,分治nlogn.double nearest(Point *p, int n) /p should already be sorted by xif(n=2) return dist(p0, p1);if(n=1) return INF;int m=pn/2.x,i,j; double left,right,tmp,ret,t;left=nearest(p,n/2);right=nearest(&pn/2,n-n/2);ret=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年調(diào)查分析師考試試題及答案
- 人工智能技術(shù)研發(fā)經(jīng)驗工作證明(5篇)
- 授權(quán)代表證明書及授權(quán)事項清單(8篇)
- 某中學(xué)圖書借閱統(tǒng)計分析制度
- 2025年電式混動車項目申請報告
- 網(wǎng)絡(luò)維護外包服務(wù)協(xié)議合同書
- 經(jīng)濟學(xué)原理與經(jīng)濟形勢分析題目
- 物聯(lián)網(wǎng)技術(shù)在智慧城市規(guī)劃中的應(yīng)用協(xié)議
- 2025年注冊會計師考試《會計》財務(wù)報表分析解題思路與技巧試題
- 介紹我的日常用品作文9篇范文
- 中意紙質(zhì)文物脫酸技術(shù)應(yīng)用與思考
- 中央民族大學(xué)強基校測面試題
- 2025年 中國南水北調(diào)集團新能源投資公司第一批中層及考試筆試試卷附答案
- 期末試卷(五)(含答案含聽力原文無聽力音頻)-2024-2025學(xué)年人教PEP版英語(新教材)三年級下冊
- 湖南2024生地會考試卷及答案
- 廣東省深圳市2024年中考英語真題(含答案)
- 敘事護理學(xué)智慧樹知到答案2024年中國人民解放軍海軍軍醫(yī)大學(xué)
- 六年級主題班隊會記錄表(6個表)
- 產(chǎn)品量產(chǎn)生產(chǎn)準備計劃
- 方案軌道交通pis+cctv系統(tǒng)解決方案淺析
- 證明(員工喪事請假村委會出具證明)
評論
0/150
提交評論