阅读:10228回复:14
三角网的生成算法
<PRE>#ifndef TIN_H
#define TIN_H #include<iostream> #include<cmath> #include<vector> #include<fstream> #include"WGraphics.h" using namespace std; typedef struct mo { int m; int n; int mark; }mo; typedef struct o { double x; double y; }Point; typedef struct sample//文件读入的采样点结构 { Point point; int Mark; int Number;//采样点编号 }Sample; class TIN { public: void GetData(); void Display(int scale); //--------------------------------------------------------------------- ~TIN() { delete [] dot; } inline double Distance(Point p1,Point p2)//计算两点之间的距离 { return(sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); } //---------------------------------------------------------------------------- inline double ComputeAngle(Point p1,Point p2,Point p3)//计算角C的弧度 { double a=Distance(p2,p3); double b=Distance(p3,p1); double c=Distance(p1,p2); return acos((a*a+b*b-c*c)/(2*a*b)); } //-------------------------------------------------------------------------------- inline int F(Point p1,Point p2,Point p3)//计算点在直线的区间 { double A=(p2.y-p1.y)/(p2.x-p1.x); double B=(p1.y*p2.x-p2.y*p1.x)/(p2.x-p1.x); double f=p3.y-A*p3.x-B; if(f>0) return 1; else if(f<0) return -1; else return 0; } //-------------------------------------------------------------------------------- int Search(int p1,int p2,int p3); //检查三角形是否重复或交叉 //-------------------------------------------------------------------------------- int Extend(int p1,int p2,int p3);//以p1,p2位基边扩展三角形 //-------------------------------------------------------------------------------- void Terminal(int key); //---------------------------------------------------------------------- protected: vector<int> t1; vector<int> t2; vector<int> t3; int K; int L; Sample * dot; int N; }; #endif int TIN::Search(int p1,int p2,int p3) //检查三角形是否重复或交叉 { mo Field[7]; Field[1].m=p1; Field[1].n=p2; Field[1].mark=0; Field[2].m=p2; Field[2].n=p1; Field[2].mark=0; Field[3].m=p1; Field[3].n=p3; Field[3].mark=0; Field[4].m=p3; Field[4].n=p1; Field[4].mark=0; Field[5].m=p2; Field[5].n=p3; Field[5].mark=0; Field[6].m=p3; Field[6].n=p2; Field[6].mark=0; for(int i=1;i<=L;i++) { for(int j=1;j<7;j++) { if((Field[j].m==t1 || Field[j].m==t2 || Field[j].m==t3) ;; (Field[j].n==t1 || Field[j].n==t2 || Field[j].n==t3) ) Field[j].mark++; } } for( i=1;i<7;i++) if(Field.mark==2) return true; return false;//三角形没有重复 } //------------------------------------------------------ int TIN::Extend(int p1,int p2,int p3)//以p1,p2为基线生成三角形 { int m=F(dot[p1].point,dot[p2].point,dot[p3].point); double Angle1=0; double Angle2=0; int sign=0; for(int i=1;i<=N;i++) { dot.Mark=F(dot[p1].point,dot[p2].point,dot.point); if(dot.Mark+m==0 ) { Angle2=ComputeAngle(dot[p1].point,dot[p2].point,dot.point); if(Angle2>Angle1) { Angle1=Angle2; sign=i; } } } if(sign!=0 ;; !Search(p1,p2,sign)) { L++; t1.push_back(p1); t2.push_back(p2); t3.push_back(sign); return sign;//新生成三角形找到了 } else return 0; } //-------------------------------------------------------------------- void TIN::Terminal(int key) { K=0; L=1; t1.push_back(0); t2.push_back(0); t3.push_back(0); int a,b,c; double dis1=999999999; double dis2; double angle1=0; double angle2=0; int m; for(int i=1;i<=N;i++) { if(key!=i) {dis2=Distance(dot[key].point,dot.point); if(dis2<dis1) { m=i; dis1=dis2; } } } t1.push_back(key); t2.push_back(m); for(i=1;i<=N;i++) { angle2=ComputeAngle(dot[t1[1]].point,dot[t2[1]].point,dot.point); if(angle2>angle1) { angle1=angle2; m=i; } } t3.push_back(m); //********************************第一个种子三角形生成 while(K!=L) { K++; a=Extend(t1[K],t2[K],t3[K]); b=Extend(t2[K],t3[K],t1[K]); c=Extend(t3[K],t1[K],t2[K]); } } //----------------------------------------------------------------------- void TIN::GetData() { ifstream in_stream; in_stream.open("data2.txt"); if(in_stream==NULL) exit(0); in_stream>>N; dot=new Sample[N+1]; for(int i=1;i<=N;i++) in_stream>>dot.Number>>dot.point.x>>dot.point.y; in_stream.close(); } //-------------------------------------------------------------------------- void TIN::Display(int scale) { //cout<<t1.size()<<endl; SetColor(RGB(0,255,0)); for(int i=1;i<t1.size();i++) { //cout<<t1<<" "<<t2<<" "<<t3<<endl; DrawLine((long)(dot[t1].point.x*scale+20),(long)(600-dot[t1].point.y*scale),(long)(dot[t2].point.x*scale+20),(long)(600-dot[t2].point.y*scale)); DrawLine((long)(dot[t2].point.x*scale+20),(long)(600-dot[t2].point.y*scale),(long)(dot[t3].point.x*scale+20),(long)(600-dot[t3].point.y*scale)); DrawLine((long)(dot[t3].point.x*scale+20),(long)(600-dot[t3].point.y*scale),(long)(dot[t1].point.x*scale+20),(long)(600-dot[t1].point.y*scale)); } }</PRE> |
|
|
1楼#
发布于:2005-08-19 12:03
<P>数据:</P>
<P>200<BR> <BR> 1 36.859 33.744<BR> 2 2.723 94.694<BR> 3 20.689 51.958<BR> 4 41.067 98.866<BR> 5 80.606 88.158<BR> 6 97.301 43.727<BR> 7 57.425 5.487<BR> 8 46.739 20.691<BR> 9 87.433 56.556<BR> 10 15.629 58.531<BR> 11 89.023 18.050<BR> 12 71.585 85.100<BR> 13 63.809 83.486<BR> 14 31.507 59.100<BR> 15 1.477 28.037<BR> 16 72.248 55.988<BR> 17 9.174 24.542<BR> 18 67.880 24.042<BR> 19 40.638 30.574<BR> 20 25.508 98.535<BR> 21 48.797 34.709<BR> 22 38.793 78.996<BR> 23 47.156 64.855<BR> 24 61.102 71.650<BR> 25 77.669 59.760<BR> 26 80.200 87.957<BR> 27 100.328 3.589<BR> 28 7.663 14.271<BR> 29 90.359 21.649<BR> 30 89.856 35.670<BR> 31 48.987 39.285<BR> 32 20.560 28.807<BR> 33 24.909 3.826<BR> 34 70.220 16.015<BR> 35 51.336 69.704<BR> 36 27.788 75.450<BR> 37 94.686 42.673<BR> 38 57.284 23.382<BR> 39 12.875 89.178<BR> 40 60.830 52.608<BR> 41 31.272 11.840<BR> 42 63.477 36.018<BR> 43 25.260 75.041<BR> 44 98.533 90.232<BR> 45 19.367 10.666<BR> 46 14.559 5.438<BR> 47 100.155 71.949<BR> 48 95.993 34.243<BR> 49 34.592 53.480<BR> 50 86.434 43.775<BR> 51 10.135 82.279<BR> 52 33.439 33.834<BR> 53 79.426 55.050<BR> 54 42.090 10.172<BR> 55 23.557 54.520<BR> 56 43.061 29.914<BR> 57 23.051 17.921<BR> 58 70.431 99.134<BR> 59 45.410 69.604<BR> 60 52.959 43.468<BR> 61 39.257 20.780<BR> 62 21.171 29.941<BR> 63 50.218 25.351<BR> 64 21.946 60.886<BR> 65 39.274 4.882<BR> 66 46.722 28.976<BR> 67 23.905 29.757<BR> 68 77.455 25.403<BR> 69 17.693 66.351<BR> 70 36.036 23.152<BR> 71 98.680 85.436<BR> 72 55.442 17.430<BR> 73 79.296 34.620<BR> 74 53.492 24.219<BR> 75 91.891 73.976<BR> 76 32.203 49.933<BR> 77 85.951 68.772<BR> 78 75.789 15.216<BR> 79 41.902 50.305<BR> 80 69.295 50.845<BR> 81 90.513 39.917<BR> 82 29.805 42.798<BR> 83 49.009 26.065<BR> 84 44.369 53.391<BR> 85 71.669 19.588<BR> 86 30.467 96.601<BR> 87 7.239 55.017<BR> 88 94.112 80.485<BR> 89 74.794 69.083<BR> 90 17.639 6.685<BR> 91 47.312 26.346<BR> 92 46.039 37.124<BR> 93 49.865 24.882<BR> 94 93.987 22.801<BR> 95 71.444 47.136<BR> 96 61.430 92.721<BR> 97 87.347 86.972<BR> 98 62.872 6.875<BR> 99 92.277 31.664<BR> 100 70.192 70.514<BR> 101 54.009 7.262<BR> 102 64.188 15.325<BR> 103 68.701 89.810<BR> 104 7.698 36.949<BR> 105 35.845 75.937<BR> 106 15.294 95.434<BR> 107 43.777 14.413<BR> 108 53.718 84.993<BR> 109 67.933 6.863<BR> 110 42.830 61.732<BR> 111 82.579 64.665<BR> 112 70.247 28.603<BR> 113 80.317 29.913<BR> 114 25.615 92.399<BR> 115 12.091 60.492<BR> 116 38.417 80.006<BR> 117 29.887 75.364<BR> 118 31.545 13.603<BR> 119 52.064 64.896<BR> 120 92.348 49.151<BR> 121 37.237 61.317<BR> 122 50.426 84.888<BR> 123 61.907 73.355<BR> 124 66.003 21.027<BR> 125 28.547 82.984<BR> 126 37.895 89.548<BR> 127 69.535 2.208<BR> 128 19.134 47.090<BR> 129 71.470 98.126<BR> 130 47.252 23.013<BR> 131 13.303 72.986<BR> 132 84.088 35.583<BR> 133 9.977 15.425<BR> 134 71.343 74.222<BR> 135 77.802 3.839<BR> 136 97.690 34.952<BR> 137 5.281 81.849<BR> 138 77.486 22.961<BR> 139 34.782 92.882<BR> 140 42.168 21.210<BR> 141 69.653 72.988<BR> 142 45.265 17.286<BR> 143 76.048 10.579<BR> 144 79.980 90.295<BR> 145 10.347 72.497<BR> 146 5.465 67.898<BR> 147 53.264 83.097<BR> 148 92.697 39.479<BR> 149 58.453 71.447<BR> 150 75.926 33.459<BR> 151 14.893 81.714<BR> 152 19.881 58.712<BR> 153 14.765 43.660<BR> 154 99.414 6.055<BR> 155 44.044 9.182<BR> 156 92.974 15.997<BR> 157 79.664 44.135<BR> 158 82.516 3.484<BR> 159 97.344 89.099<BR> 160 75.171 47.805<BR> 161 97.005 85.413<BR> 162 30.473 50.688<BR> 163 38.836 52.262<BR> 164 100.257 44.408<BR> 165 88.978 36.920<BR> 166 87.112 73.121<BR> 167 81.824 23.084<BR> 168 6.539 35.250<BR> 169 91.977 3.377<BR> 170 70.681 81.092<BR> 171 21.766 9.953<BR> 172 77.685 81.373<BR> 173 96.495 15.186<BR> 174 14.724 56.168<BR> 175 77.160 88.574<BR> 176 77.595 60.731<BR> 177 83.974 17.729<BR> 178 98.978 42.565<BR> 179 27.370 93.421<BR> 180 90.300 52.597<BR> 181 63.717 55.855<BR> 182 94.233 29.477<BR> 183 48.579 4.013<BR> 184 54.821 30.956<BR> 185 12.718 65.154<BR> 186 94.216 44.474<BR> 187 48.588 31.374<BR> 188 6.054 46.814<BR> 189 7.558 55.384<BR> 190 57.453 35.896<BR> 191 10.722 7.347<BR> 192 8.641 18.663<BR> 193 69.313 98.852<BR> 194 41.168 64.191<BR> 195 25.738 68.032<BR> 196 97.820 18.780<BR> 197 96.344 21.765<BR> 198 40.106 85.323<BR> 199 33.638 48.083<BR> 200 12.357 61.651<BR>-----------------------------------------<BR>//Data end</P> |
|
|
2楼#
发布于:2005-11-19 18:26
没有参考文献还不是像看天书一样。
|
|
3楼#
发布于:2006-01-01 10:39
谢了大大~~~~
|
|
4楼#
发布于:2006-01-25 15:38
我试过了,无法编译,还缺个函数什么的
|
|
|
5楼#
发布于:2006-05-21 00:47
<img src="images/post/smile/dvbbs/em01.gif" />谢谢了
|
|
6楼#
发布于:2006-06-13 16:33
请问.WGraphics.h 头文件怎么写哦.
|
|
7楼#
发布于:2006-06-28 22:13
看不懂!
|
|
8楼#
发布于:2006-07-11 21:12
麻烦老大把头文件WGraphics.h补一下哦,要不这个程序无法运行,自定义的头文件。。。
|
|
9楼#
发布于:2006-07-11 23:35
另外好像没有主函数。只是一个类的定义。
|
|
上一页
下一页