⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delauneyview.cpp

📁 三角剖分源程序对于平面插值的改进有帮助
💻 CPP
字号:
// DelauneyView.cpp : implementation of the CDelauneyView class
//

#include "stdafx.h"
#include "Delauney.h"

#include "DelauneyDoc.h"
#include "DelauneyView.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDelauneyView

IMPLEMENT_DYNCREATE(CDelauneyView, CView)

BEGIN_MESSAGE_MAP(CDelauneyView, CView)
	//{{AFX_MSG_MAP(CDelauneyView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDelauneyView construction/destruction

CDelauneyView::CDelauneyView()
{
	for(long i=1;i<=2000;i++)
	{
	
		pt[i].x=rand()%1000;
		pt[i].y=rand()%600;
	}

	/*pt[1].x=600;
	pt[1].y=200;
	pt[2].x=100;
    pt[2].y=20;
    pt[3].x=500;
	pt[3].y=30;
    pt[4].x=290;
	pt[4].y=470;
	pt[5].x=700;
	pt[5].y=134;
	pt[6].x=80;
	pt[6].y=450;
	pt[7].x=490;
	pt[7].y=400;
	pt[8].x=230;
	pt[8].y=230;
	pt[9].x=345;
	pt[9].y=34;*/
	num=2000;
	// TODO: add construction code here

}

CDelauneyView::~CDelauneyView()
{
}

BOOL CDelauneyView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDelauneyView drawing

void CDelauneyView::OnDraw(CDC* pDC)
{
	CDelauneyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	Compute();
	Display(pDC);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CDelauneyView printing

BOOL CDelauneyView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDelauneyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDelauneyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDelauneyView diagnostics

#ifdef _DEBUG
void CDelauneyView::AssertValid() const
{
	CView::AssertValid();
}

void CDelauneyView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDelauneyDoc* CDelauneyView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDelauneyDoc)));
	return (CDelauneyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDelauneyView message handlers

void CDelauneyView::Compute()
{
	float distance=pow(pt[1].x-pt[2].x,2)+pow(pt[1].y-pt[2].y,2);
	tag=2;
	for(i=3;i<=num;i++)
	{
		float tempdis=(pt[1].x-pt[i].x)*(pt[1].x-pt[i].x)+(pt[1].y-pt[i].y)*(pt[1].y-pt[i].y);
		if(tempdis<distance)
		{
			distance=tempdis;
			tag=i;
		}
	}
	t1[1]=1;
	t2[1]=tag;
	POINT midpoint;
	midpoint.x=(pt[1].x+pt[tag].x)/2;
	midpoint.y=(pt[1].y+pt[tag].y)/2;
	distance=1000000;
	for(i=2;i<=num;i++)
	{
		if(i!=tag)
		{
			if((pt[tag].x-pt[1].x)*(pt[i].y-pt[1].y)-(pt[i].x-pt[1].y)*(pt[tag].y-pt[1].y)<0)
			{
				float tempdis=sqrt((pt[i].x-midpoint.x)*(pt[i].x-midpoint.x)
					             +(pt[i].y-midpoint.y)*(pt[i].y-midpoint.y));

				if(tempdis<distance)
				{
					distance=tempdis;
					tag1=i;
				}
			}
		}
	}
	t3[1]=tag1;
	L=1;
	K=1;
	do
	{
		cycle(t1[K],t2[K],t3[K]);
		cycle(t2[K],t3[K],t1[K]);
	    cycle(t3[K],t1[K],t2[K]);
		K++;
	}
	while(K!=L);

}

void CDelauneyView::cycle(int a,int b,int c)
{
	float k1;
	if(pt[b].x!=pt[a].x)
	{
	    k1=pt[c].y-(pt[b].y-pt[a].y)*pt[c].x/(pt[b].x-pt[a].x)
		     +(pt[a].x*pt[b].y-pt[b].x*pt[a].y)/(pt[b].x-pt[a].x);
	}
	else 
	{
		k1=pt[c].y;
	}
	float cos=1;
	int tag=1;
	for(int i=2;i<=num;i++)
	{
		if(i!=a&&i!=b&&i!=c)
		{  
			float k2;
		 	if(pt[b].x!=pt[a].x)
			{
		        k2=pt[i].y-(pt[b].y-pt[a].y)*pt[i].x/(pt[b].x-pt[a].x)
			         +(pt[a].x*pt[b].y-pt[b].x*pt[a].y)/(pt[b].x-pt[a].x);
			}
			else
			{
				k2=pt[i].y;
			}
		    if(k1*k2<0)
			{
			   float d1=sqrt(pow((pt[a].x-pt[i].x),2)+pow((pt[a].y-pt[i].y),2));
			   float d2=sqrt(pow((pt[b].x-pt[i].x),2)+pow((pt[b].y-pt[i].y),2));
			   float tempcos=((pt[a].x-pt[i].x)*(pt[b].x-pt[i].x)+(pt[a].y-pt[i].y)*(pt[b].y-pt[i].y))/(d1*d2);
			   if(tempcos<cos)
			   {
				   cos=tempcos;
				   tag=i;
			   }
		   }
		}
	}
	
	if(cos!=1)
	{
		L++;
		t1[L]=a;
		t2[L]=b;
		t3[L]=tag;
		int k[3]={0,0,0};
        for(i=1;i<=L-1;i++)
		{
	        if((t1[i]==t1[L]&&t2[i]==t2[L])||(t1[i]==t2[L]&&t2[i]==t1[L])
			   ||(t1[i]==t1[L]&&t3[i]==t2[L])||(t3[i]==t1[L]&&t1[i]==t2[L])
			   ||(t2[i]==t1[L]&&t3[i]==t2[L])||(t3[i]==t1[L]&&t2[i]==t2[L]))
			{
				k[0]++;
			}
		    if((t1[i]==t3[L]&&t2[i]==t2[L])||(t1[i]==t2[L]&&t2[i]==t3[L])
			   ||(t1[i]==t3[L]&&t3[i]==t2[L])||(t3[i]==t3[L]&&t1[i]==t2[L])
			   ||(t2[i]==t3[L]&&t3[i]==t2[L])||(t3[i]==t3[L]&&t2[i]==t2[L]))
			{
				k[1]++;
			}
            if((t1[i]==t1[L]&&t2[i]==t3[L])||(t1[i]==t3[L]&&t2[i]==t1[L])
			   ||(t1[i]==t1[L]&&t3[i]==t3[L])||(t3[i]==t1[L]&&t1[i]==t3[L])
			   ||(t2[i]==t1[L]&&t3[i]==t3[L])||(t3[i]==t1[L]&&t2[i]==t3[L]))
			{
				k[2]++;
			}
		}
	    if(k[0]==2||k[1]==2||k[2]==2)
		{
	    	L--;
		}
	}

}



void CDelauneyView::Display(CDC *pDC)
{
	for(i=1;i<=L;i++)
	{
		pDC->MoveTo(pt[t1[i]].x,pt[t1[i]].y);
		pDC->LineTo(pt[t2[i]].x,pt[t2[i]].y);
		pDC->LineTo(pt[t3[i]].x,pt[t3[i]].y);
		pDC->LineTo(pt[t1[i]].x,pt[t1[i]].y);
	}
                            
}                                         
                                                              

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -