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

📄 shiyuanview.cpp

📁 小世界网络的试验程序!可以观察小世界网络的生成过程和结果!
💻 CPP
字号:
// shiyuanView.cpp : implementation of the CShiyuanView class
//

#include "stdafx.h"
#include "shiyuan.h"
#include <math.h>
#include "shiyuanDoc.h"
#include "shiyuanView.h"
#include "Afxtempl.h"

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

#define PI 3.1415926535
#define RADIAN(angle) ((angle)*PI/180.0) 
/////////////////////////////////////////////////////////////////////////////
// CShiyuanView
BOOL panduan(LINE line);
IMPLEMENT_DYNCREATE(CShiyuanView, CView)

BEGIN_MESSAGE_MAP(CShiyuanView, CView)
	//{{AFX_MSG_MAP(CShiyuanView)
	ON_COMMAND(IDM_FUZA, OnFuza)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CShiyuanView construction/destruction

CShiyuanView::CShiyuanView()
{
	// TODO: add construction code here
	pp=0;
	q=false;
	GailvDlg dlg;
	dlg.m_gailv=0;
	for(int i=0;i<20;i++)
	{
		points[i].x=0;
		points[i].y=0;
	}
}

CShiyuanView::~CShiyuanView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CShiyuanView drawing

void CShiyuanView::OnDraw(CDC* pDC)
{
	CShiyuanDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CPen pen(PS_SOLID,10,RGB(255,0,0));
	CPen *oldpen;
	oldpen=pDC->SelectObject(&pen);
/*	pDC->SetPixel(200,200,RGB(255,0,0));
	//pDC->Ellipse(300-1,200-1,300+1,200+1);
	if(p)
		Draw();
//	CPoint points[20];

	*/
	//LINE l;
	int i,s=0;
	float j=0,fsin=0,fcos=0;
	for(i=0;i<=360;i+=18)
	{
		j=(float)RADIAN(i);
		fsin=(float)sin((double)j);
		fcos=(float)cos((double)j);
	//	CDC *pDC=GetDC();
		
	//	i=i+18;
		points[s].x=(float)(300+200*fcos);
		points[s].y=(float)(300+200*fsin);
		pDC->Ellipse(points[s].x-1*fcos,points[s].y-1*fsin,points[s].x+1*fcos,points[s].y+1*fsin);

		s++;
	}
//	pDC->SetPixel(points[2]);
	for(i=0;i<20;i++)
	{
		points1[i].x=points[i].x+500;
		points1[i].y=points[i].y;
	}
	   for(i=0;i<=20;i++)
	   {
		pDC->Ellipse(points1[i].x-1*fcos,points1[i].y-1*fsin,points1[i].x+1*fcos,points1[i].y+1*fsin);
	
	   }
	   	CPen pen1(PS_SOLID,2,RGB(0,20,250));
		CPen *oldpen1;
		oldpen1=pDC->SelectObject(&pen1);
        BOOL w=false;
	   ////////////////////////////////////////////
		for(i=2;i<17;i++)
		{
		//	if(i>19) i=i-20;
			pDC->MoveTo(points1[i]);
			pDC->LineTo(points1[i-2]);
			
			pDC->MoveTo(points1[i]);
			pDC->LineTo(points1[i-1]);
		
			pDC->MoveTo(points1[i]);
			pDC->LineTo(points1[i+1]);
		
			pDC->MoveTo(points1[i]);
			pDC->LineTo(points1[i+2]);
		
			pDC->MoveTo(points1[i]);
			pDC->LineTo(points1[i+3]);
		}
		
		pDC->MoveTo(points1[0]);
		pDC->LineTo(points1[1]);

		pDC->MoveTo(points1[0]);
		pDC->LineTo(points1[2]);
	
		pDC->MoveTo(points1[0]);
		pDC->LineTo(points1[3]);
		
		pDC->MoveTo(points1[0]);
		pDC->LineTo(points1[18]);
	
		pDC->MoveTo(points1[0]);
		pDC->LineTo(points1[19]);
	
		pDC->MoveTo(points1[1]);
		pDC->LineTo(points1[0]);
		pDC->MoveTo(points1[1]);
		pDC->LineTo(points1[19]);
	
		pDC->MoveTo(points1[1]);
		pDC->LineTo(points1[2]);
		pDC->MoveTo(points1[1]);
		pDC->LineTo(points1[3]);
		pDC->MoveTo(points1[1]);
		pDC->LineTo(points1[4]);
	
		pDC->MoveTo(points1[18]);
		pDC->LineTo(points1[16]);
		pDC->MoveTo(points1[18]);
		pDC->LineTo(points1[17]);
	
		pDC->MoveTo(points1[18]);
		pDC->LineTo(points1[19]);
	
		pDC->MoveTo(points1[18]);
		pDC->LineTo(points1[0]);
		pDC->MoveTo(points1[18]);
		pDC->LineTo(points1[1]);
	

		pDC->MoveTo(points1[17]);
		pDC->LineTo(points1[15]);
		pDC->MoveTo(points1[17]);
		pDC->LineTo(points1[16]);
		pDC->MoveTo(points1[17]);
		pDC->LineTo(points1[18]);
		pDC->MoveTo(points1[17]);
		pDC->LineTo(points1[19]);
		pDC->MoveTo(points1[17]);
		pDC->LineTo(points1[0]);
	
		pDC->MoveTo(points1[19]);
		pDC->LineTo(points1[17]);
		pDC->MoveTo(points1[19]);
		pDC->LineTo(points1[18]);
		pDC->MoveTo(points1[19]);
		pDC->LineTo(points1[0]);
		pDC->MoveTo(points1[19]);
		pDC->LineTo(points1[1]);
		pDC->MoveTo(points1[19]);
		pDC->LineTo(points1[2]);
		//////////////////////////////////////////////////


	
	/*	for(i=2;i<17;i++)
		{
			if(i>19) i=i-20;
			pDC->MoveTo(points[i]);
			pDC->LineTo(points[i-2]);
			l.pStart=points[i];
			l.pEnd=points[i-2];
			if(i==2)
				m_lines.Add(l);
			else if( !panduan(l) )
				m_lines.Add(l);

			pDC->MoveTo(points[i]);
			pDC->LineTo(points[i-1]);
			l.pStart=points[i];
			l.pEnd=points[i-1];
			if( !panduan(l) )
				m_lines.Add(l);

			pDC->MoveTo(points[i]);
			pDC->LineTo(points[i+1]);
			l.pStart=points[i];
			l.pEnd=points[i+1];
			if( !panduan(l) )
				m_lines.Add(l);

			pDC->MoveTo(points[i]);
			pDC->LineTo(points[i+2]);
			l.pStart=points[i];
			l.pEnd=points[i+2];
			if( !panduan(l) )
				m_lines.Add(l);

			pDC->MoveTo(points[i]);
			pDC->LineTo(points[i+3]);
			l.pStart=points[i];
			l.pEnd=points[i+3];
			if( !panduan(l) )
				m_lines.Add(l);
		}
		
		pDC->MoveTo(points[0]);
		pDC->LineTo(points[1]);

		pDC->MoveTo(points[0]);
		pDC->LineTo(points[2]);
	
		pDC->MoveTo(points[0]);
		pDC->LineTo(points[3]);
		l.pStart=points[0];
			l.pEnd=points[3];
			if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[0]);
		pDC->LineTo(points[18]);
		l.pStart=points[0];
			l.pEnd=points[18];
			if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[0]);
		pDC->LineTo(points[19]);
		l.pStart=points[0];
			l.pEnd=points[19];
		if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[1]);
		pDC->LineTo(points[0]);
		pDC->MoveTo(points[1]);
		pDC->LineTo(points[19]);
		l.pStart=points[1];
			l.pEnd=points[19];
			if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[1]);
		pDC->LineTo(points[2]);
		pDC->MoveTo(points[1]);
		pDC->LineTo(points[3]);
		pDC->MoveTo(points[1]);
		pDC->LineTo(points[4]);
		l.pStart=points[1];
			l.pEnd=points[4];
			if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[18]);
		pDC->LineTo(points[16]);
		pDC->MoveTo(points[18]);
		pDC->LineTo(points[17]);
		l.pStart=points[18];
			l.pEnd=points[17];
			if( !panduan(l) )
				m_lines.Add(l);
		pDC->MoveTo(points[18]);
		pDC->LineTo(points[19]);
		l.pStart=points[18];
			l.pEnd=points[19];
			if( !panduan(l) )
				m_lines.Add(l);
		pDC->MoveTo(points[18]);
		pDC->LineTo(points[0]);
		pDC->MoveTo(points[18]);
		pDC->LineTo(points[1]);
		l.pStart=points[18];
			l.pEnd=points[1];
			if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[17]);
		pDC->LineTo(points[15]);
		pDC->MoveTo(points[17]);
		pDC->LineTo(points[16]);
		pDC->MoveTo(points[17]);
		pDC->LineTo(points[18]);
		pDC->MoveTo(points[17]);
		pDC->LineTo(points[19]);
		pDC->MoveTo(points[17]);
		l.pStart=points[17];
			l.pEnd=points[19];
			if( !panduan(l) )
				m_lines.Add(l);
		pDC->LineTo(points[0]);
		l.pStart=points[17];
			l.pEnd=points[0];
			if( !panduan(l) )
				m_lines.Add(l);

		pDC->MoveTo(points[19]);
		pDC->LineTo(points[17]);
		pDC->MoveTo(points[19]);
		pDC->LineTo(points[18]);
		pDC->MoveTo(points[19]);
		pDC->LineTo(points[0]);
		pDC->MoveTo(points[19]);
		pDC->LineTo(points[1]);
		pDC->MoveTo(points[19]);
		pDC->LineTo(points[2]);
		l.pStart=points[19];
			l.pEnd=points[2];
			if( !panduan(l) )
				m_lines.Add(l);

*/

	
	if(q)
	{   
		tongji();
		tuxing();
	//	MessageBox("dafdsafd");
	}	
	//pp=0;
	q=false;
	pDC->SelectObject(oldpen);
	//InvalidateRect(false);
	//Invalidate();
}

/////////////////////////////////////////////////////////////////////////////
// CShiyuanView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CShiyuanView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CShiyuanView message handlers

void CShiyuanView::Draw()
{
	/*float j=0,fsin=0,fcos=0;
	for(int i=0;i<=360;i++)
	{
		j=(float)RADIAN(i);
		fsin=(float)sin((double)i);
		fcos=(float)cos((double)i);
		CDC *pDC=GetDC();
		pDC->Ellipse(200+100*fcos-1,200+100*fsin-1,200+100*fcos+1,200+100*fsin+1);
		i=i+18;
	}*/
}

void CShiyuanView::OnFuza() 
{
	// TODO: Add your command handler code here
	GailvDlg dlg;
	if(IDOK==dlg.DoModal())
         m_y=dlg.m_gailv;
	//pp=1;
	q=true;
	Invalidate();
}

UINT CShiyuanView::MapRand(UINT nMax)
{
	int nRand =rand();
	float fMap=(float)nMax / RAND_MAX;
	float fRetVal =(float)nRand *fMap+0.5F;
	return (UINT)fRetVal;
}

BOOL CShiyuanView::panduan(LINE line)

{	
	int p[100];
	BOOL y=false;
	for(int i=0;i<m_lines.GetSize();i++)
		{
			if((line.pStart.x==m_lines[i].pEnd.x )&&(line.pStart.y==m_lines[i].pEnd.y )&& (line.pEnd.x==m_lines[i].pStart.x)&&(line.pEnd.y==m_lines[i].pStart.y))
				p[i]=0;
			else 
				p[i]=1; 
		}
	for(int j=0;j<100;j++)
	{
		if (p[i]==1)
			y=true;
		else
			y=false;
		//break;
	}
	return y;
}


void CShiyuanView::tuxing()
{
	float y=0;
	int y1=10,y2=10;
	CDC *pDC=GetDC();
		for(int h=0;h<m_lines.GetSize();h++)
		{
		    y=m_y;
			y1=MapRand(20);
			y2=MapRand(20);
			if( y < (y1*0.05) && y < (y2*0.05) )
				if(y1!=h ||y2!= h)
				{
					m_lines[h].pStart=points[y1];
					m_lines[h].pEnd=points[y2];
				}
		}
	
		for(int i=0;i<m_lines.GetSize();i++)
		{
			int x1,x2,x3;
			x1=MapRand(255);
			x2=MapRand(255);
			x3=MapRand(255);
			CPen pen2( PS_SOLID,2,RGB(x1,x2,x3) );
			CPen *old;
			old=pDC->SelectObject(&pen2);
			pDC->MoveTo(m_lines[i].pStart);
			pDC->LineTo(m_lines[i].pEnd);
		}
}

void CShiyuanView::tongji()
{
	m_lines.RemoveAll();
	LINE l;
	for(int i=2;i<17;i++)
		{
			if(i>19) i=i-20;
			l.pStart=points[i];
			l.pEnd=points[i-2];
			if(i==2)
				m_lines.Add(l);
			else if( !panduan(l) )
				m_lines.Add(l);

			l.pStart=points[i];
			l.pEnd=points[i-1];
			if( !panduan(l) )
				m_lines.Add(l);

			l.pStart=points[i];
			l.pEnd=points[i+1];
			if( !panduan(l) )
				m_lines.Add(l);

			l.pStart=points[i];
			l.pEnd=points[i+2];
			if( !panduan(l) )
				m_lines.Add(l);

			l.pStart=points[i];
			l.pEnd=points[i+3];
			if( !panduan(l) )
				m_lines.Add(l);
		}
	l.pStart=points[17];
	l.pEnd=points[18];
	if( !panduan(l) )
			m_lines.Add(l);
	l.pStart=points[17];
	l.pEnd=points[0];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[17];
	l.pEnd=points[19];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[18];
	l.pEnd=points[19];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[18];
	l.pEnd=points[0];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[19];
	l.pEnd=points[1];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[19];
	l.pEnd=points[10];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[19];
	l.pEnd=points[1];
	if( !panduan(l) )
			m_lines.Add(l);
	l.pStart=points[19];
	l.pEnd=points[2];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[0];
	l.pEnd=points[3];
	if( !panduan(l) )
			m_lines.Add(l);

	l.pStart=points[1];
	l.pEnd=points[4];
	if( !panduan(l) )
			m_lines.Add(l);

}

⌨️ 快捷键说明

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