delauney三角形剖分程序.docx
《delauney三角形剖分程序.docx》由会员分享,可在线阅读,更多相关《delauney三角形剖分程序.docx(17页珍藏版)》请在冰豆网上搜索。
![delauney三角形剖分程序.docx](https://file1.bdocx.com/fileroot1/2022-12/16/bf01adad-f665-4b05-a596-e7a494fa8dde/bf01adad-f665-4b05-a596-e7a494fa8dde1.gif)
delauney三角形剖分程序
//T_TIN.cpp:
implementationoftheT_TINclass.
//
//////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"tin2.h"
#include"T_TIN.h"
#ifdef_DEBUG
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#definenewDEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
T_TIN:
:
T_TIN()
{
}
T_TIN:
:
~T_TIN()
{
}
doubleT_TIN:
:
rand()
{
staticlongx=5;
staticlongy=11;
staticlongz=17;
x=(171*x)%30269;
y=(172*y)%30307;
z=(170*z)%30323;
longx1=x;
longy1=y;
longz1=z;
doubleT,R;
T=double(x1)/double(30269)+double(y1)/double(30307)+double(z1)/double(30323);
R=T-long(T);
returnR;
}
doubleT_TIN:
:
angle(T_lineline,longp)
{
doublec;
doublea;
doubleb;
c=distance(line.p0,line.p1);
a=distance(line.p0,p);
b=distance(line.p1,p);
doublez=0;
z=acos((b*b+a*a-c*c)/(2*a*b));
returnz;
}
longT_TIN:
:
Max(T_lineline,vectorusableList)
{
longindex=-1;
doublexitaMax=0;
doublexita=0;
for(longi=0;i{
xita=angle(line,usableList[i]);
if(xitaMax<=xita)
{
xitaMax=xita;
index=usableList[i];
}
}
returnindex;
}
doubleT_TIN:
:
F(T_lineline,longp3)
{
T_pointa0=pointList[line.p0];
T_pointa1=pointList[line.p1];
T_pointa2=pointList[p3];
doubleA=(a1.y-a0.y)/(a1.x-a0.x);
doubleB=(a1.x*a0.y-a0.x*a1.y)/(a1.x-a0.x);
doublez=0;
z=a2.y-a2.x*A-B;
returnz;
}
doubleT_TIN:
:
distance(longp1,longp2)
{
doublez=0;
if(p1!
=p2)
{
T_pointa1=pointList[p1];
T_pointa2=pointList[p2];
z=sqrt((a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y));
}
returnz;
}
voidT_TIN:
:
createpoint(intn)
{
T_pointpoint;
for(longi=0;i{
point.x=rand()*980;
point.y=rand()*600;
pointList.push_back(point);
}
}
voidT_TIN:
:
drawPoint(CDC*pDC)
{
for(intlongi=0;i{
intx=pointList[i].x;
inty=pointList[i].y;
CRectpp(x-2.5,y-2.5,x+2.5,y+2.5);
CBrushbrush;
brush.CreateSolidBrush(RGB(255,0,0));
pDC->SelectObject(brush);
//pDC->SetPixel(pp,RGB(255,0,0));
pDC->Ellipse(&pp);
}
}
voidT_TIN:
:
createTin()
{
vectorusalbePlointList;
//生成第0个三角形
T_tritri;
longk=0;
//第L0边
T_lineline0;
line0.p0=0;
line0.p1=1;
line0.useCount=1;
lineList.push_back(line0);
tri.L0=lineList.size()-1;
///////////
for(longi=2;i{
usalbePlointList.push_back(i);
}
longindex=-1;
index=Max(line0,usalbePlointList);
//生成L1,L2边
T_lineline1;
T_lineline2;
line1.p0=0;
line1.p1=index;
line1.useCount=1;
lineList.push_back(line1);
tri.L1=lineList.size()-1;
line2.p0=1;
line2.p1=index;
line2.useCount=1;
lineList.push_back(line2);
tri.L2=lineList.size()-1;
triList.push_back(tri);
//CStrings;
//s.Format("连接顶点0,1,%d",index);
//AfxMessageBox(s);
/************************************
对于第k个三角形为拓展三角形时
****************************************/
while
(1)
{
/****第L0边***/
if(lineList[triList[k].L0].useCount<2)//是可拓展边
{
//找拓展顶点
longtuo=-1;
if(lineList[triList[k].L0].p0==lineList[triList[k].L1].p0
||lineList[triList[k].L0].p1==lineList[triList[k].L1].p0)
tuo=lineList[triList[k].L1].p1;
if(lineList[triList[k].L0].p0==lineList[triList[k].L1].p1
||lineList[triList[k].L0].p1==lineList[triList[k].L1].p1)
tuo=lineList[triList[k].L1].p0;
usalbePlointList.clear();
doubleFuhao=0;
doubleFu=0;
Fuhao=F(lineList[triList[k].L0],tuo);
for(longi=0;i{
if(lineList[triList[k].L0].p0!
=i//不是三角形的顶点
&&lineList[triList[k].L0].p1!
=i
&&lineList[triList[k].L1].p0!
=i
&&lineList[triList[k].L1].p1!
=i
&&lineList[triList[k].L2].p0!
=i
&&lineList[triList[k].L2].p1!
=i)
{
Fu=F(lineList[triList[k].L0],i);
if(Fuhao*Fu<0)//异号
{
usalbePlointList.push_back(i);
}
}
}
if(usalbePlointList.size()>0)
{
intidx=-1;
idx=Max(lineList[triList[k].L0],usalbePlointList);
longlineIdx1=-1;
longlineIdx2=-1;
intcout1=0;
intcout2=0;
T_lineline1;
T_lineline2;
line1.p0=lineList[triList[k].L0].p0;
line1.p1=idx;
line2.p0=lineList[triList[k].L0].p1;
line2.p1=idx;
for(longi=0;i{
if(line1==lineList[i])
{
lineIdx1=i;
cout1=lineList[lineIdx1].useCount;
}
if(line2==lineList[i])
{
lineIdx2=i;
cout2=lineList[lineIdx2].useCount;
}
}
if(cout1<2&&cout2<2)
{
T_tritri;
tri.L0=triList[k].L0;
if(cout1==0)
{
T_lineline;
line.p0=lineList[triList[k].L0].p0;
line.p1=idx;
line.useCount=1;
lineList.push_back(line);
tri.L1=lineList.size()-1;
}
if(cout1==1)
{
lineList[lineIdx1].useCount=2;
tri.L1=lineIdx1;
}
if(cout2==0)
{
T_lineline;
line.p0=lineList[triList[k].L0].p1;
line.p1=idx;
line.useCount=1;
lineList.push_back(line);
tri.L2=lineList.size()-1;
}
if(cout2==1)
{
lineList[lineIdx2].useCount=2;
tri.L2=lineIdx2;
}
triList.push_back(tri);
}
}
lineList[triList[k].L0].useCount=2;
}
/****第L1边***/
if(lineList[triList[k].L1].useCount<2)
{
//找拓展顶点
longtuo=-1;
if(lineList[triList[k].L1].p0==lineList[triList[k].L0].p0
||lineList[triList[k].L1].p1==lineList[triList[k].L0].p0)
tuo=lineList[triList[k].L0].p1;
if(lineList[triList[k].L1].p0==lineList[triList[k].L0].p1
||lineList[triList[k].L1].p1==lineList[triList[k].L0].p1)
tuo=lineList[triList[k].L0].p0;
usalbePlointList.clear();
doubleFuhao=0;
doubleFu=0;
Fuhao=F(lineList[triList[k].L1],tuo);
for(longi=0;i{
if(lineList[triList[k].L0].p0!
=i//不是三角形的顶点
&&lineList[triList[k].L0].p1!
=i
&&lineList[triList[k].L1].p0!
=i
&&lineList[triList[k].L1].p1!
=i
&&lineList[triList[k].L2].p0!
=i
&&lineList[triList[k].L2].p1!
=i)
{
Fu=F(lineList[triList[k].L1],i);
if(Fuhao*Fu<0)//异号
{
usalbePlointList.push_back(i);
}
}
}
if(usalbePlointList.size()>0)
{
intidx=-1;
idx=Max(lineList[triList[k].L1],usalbePlointList);
longlineIdx1=-1;
longlineIdx2=-1;
intcout1=0;
intcout2=0;
T_lineline1;
T_lineline2;
line1.p0=lineList[triList[k].L1].p0;
line1.p1=idx;
line2.p0=lineList[triList[k].L1].p1;
line2.p1=idx;
for(longi=0;i{
if(line1==lineList[i])
{
lineIdx1=i;
cout1=lineList[lineIdx1].useCount;
}
if(line2==lineList[i])
{
lineIdx2=i;
cout2=lineList[lineIdx2].useCount;
}
}
if(cout1<2&&cout2<2)
{
T_tritri;
tri.L0=triList[k].L1;
if(cout1==0)
{
T_lineline;
line.p0=lineList[triList[k].L1].p0;
line.p1=idx;
line.useCount=1;
lineList.push_back(line);
tri.L1=lineList.size()-1;
}
if(cout1==1)
{
lineList[lineIdx1].useCount=2;
tri.L1=lineIdx1;
}
if(cout2==0)
{
T_lineline;
line.p0=lineList[triList[k].L1].p1;
line.p1=idx;
line.useCount=1;
lineList.push_back(line);
tri.L2=lineList.size()-1;
}
if(cout2==1)
{
lineList[lineIdx2].useCount=2;
tri.L2=lineIdx2;
}
triList.push_back(tri);
}
}
lineList[triList[k].L1].useCount=2;
}
/****第L2边***/
if(lineList[triList[k].L2].useCount<2)
{
//找拓展顶点
longtuo=-1;
if(lineList[triList[k].L2].p0==lineList[triList[k].L0].p0
||lineList[triList[k].L2].p1==lineList[triList[k].L0].p0)
tuo=lineList[triList[k].L0].p1;
if(lineList[triList[k].L2].p0==lineList[triList[k].L0].p1
||lineList[triList[k].L2].p1==lineList[triList[k].L0].p1)
tuo=lineList[triList[k].L0].p0;
usalbePlointList.clear();
doubleFuhao=0;
doubleFu=0;
Fuhao=F(lineList[triList[k].L2],tuo);
for(longi=0;i{
if(lineList[triList[k].L0].p0!
=i//不是三角形的顶点
&&lineList[triList[k].L0].p1!
=i
&&lineList[triList[k].L1].p0!
=i
&&lineList[triList[k].L1].p1!
=i
&&lineList[triList[k].L2].p0!
=i
&&lineList[triList[k].L2].p1!
=i)
{
Fu=F(lineList[triList[k].L2],i);
if(Fuhao*Fu<0)//异号
{
usalbePlointList.push_back(i);
}
}
}
if(usalbePlointList.size()>0)
{
intidx=-1;
idx=Max(lineList[triList[k].L2],usalbePlointList);
longlineIdx1=-1;
longlineIdx2=-1;
intcout1=0;
intcout2=0;
T_lineline1;
T_lineline2;
line1.p0=lineList[triList[k].L2].p0;
line1.p1=idx;
line2.p0=lineList[triList[k].L2].p1;
line2.p1=idx;
for(longi=0;i{
if(line1==lineList[i])
{
lineIdx1=i;
cout1=lineList[lineIdx1].useCount;
}
if(line2==lineList[i])
{
lineIdx2=i;
cout2=lineList[lineIdx2].useCount;
}
}
if(cout1<2&&cout2<2)
{
T_tritri;
tri.L0=triList[k].L2;
if(cout1==0)
{
T_lineline;
line.p0=lineList[triList[k].L2].p0;
line.p1=idx;
line.useCount=1;
lineList.push_back(line);
tri.L1=lineList.size()-1;
}
if(