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

📄 dlgbackground.cpp

📁 Hi guys, I have a B-spline curve algorithm by using Genetic algorithm. Any interested?
💻 CPP
字号:
// dlgBackground.cpp : implementation file
//

#include "stdafx.h"
#include "GA.h"
#include "dlgBackground.h"
#include "mshflexgrid.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"

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

#define data 900
#define Draw 1000
/////////////////////////////////////////////////////////////////////////////
// CdlgBackground dialog
//global variables
	int number ;
	int IndividualLoop, generation,times;
	int space = 32;
	int area;

	float x[data],y[data],z[data];
	int countX,countXd,begincountX;
	int countY,countYd,begincountY;
	extern int scale;
	extern float pocx0[data],pocy0[data],pocx1[data],pocy1[data];
	extern int countPoc;

/////////////////////////////////////////////////////////////////////////////

CdlgBackground::CdlgBackground(CWnd* pParent /*=NULL*/)
	: CDialog(CdlgBackground::IDD, pParent)
{
	//{{AFX_DATA_INIT(CdlgBackground)
	m_individual = 0;
	m_generation = 0;
	//}}AFX_DATA_INIT
}


void CdlgBackground::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CdlgBackground)
	DDX_Control(pDX, IDC_MSHFLEXGRID1, m_grid);
	DDX_Text(pDX, IDC_EDIT1, m_individual);
	DDX_Text(pDX, IDC_EDIT2, m_generation);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CdlgBackground, CDialog)
	//{{AFX_MSG_MAP(CdlgBackground)
	ON_BN_CLICKED(IDC_SaveData, OnSaveData)
	ON_BN_CLICKED(IDC_Reset, OnReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CdlgBackground message handlers


/////////////////////////////////////////////////////////////////////////////

void CdlgBackground::Refresh()
{

	m_grid.Clear();
	m_grid.SetTextMatrix(1,0,"y");
	m_grid.SetTextMatrix(times+1,0,"Z");
	

}
/////////////////////////////////////////////////////////////////////////////

BOOL CdlgBackground::OnInitDialog()
{
	CDialog::OnInitDialog();
	int i,j;
	char str[10];
// set random for measure error
	float fp,ap;
	char  ch ;
	area = 1;

	char *pdest;
	int result;
	times = 10;

	CString path, file, TrajectoryName;
	static char BASED_CODE szFilter[] = "Data Files (*.txt)|*.txt||";
	path=_T("C:\\Documents and Settings\\LeTatHien\\Desktop\\GACurve\\draw.txt");
	CFileDialog dlg(TRUE, NULL, path,OFN_HIDEREADONLY,szFilter);
	if(dlg.DoModal()!=IDOK)
		{
		MessageBox("Fail to Open","ABC",MB_OK);
		return 0;
		}

	path=dlg.GetPathName();
	file= dlg.GetFileName();

	int countLine = 1;
	FILE *f1tmp,*f2tmp;
	CString szLine,szLine2;
	CString tmp,tmp2;
	sfText.Open(path);
	while(sfText.GetNextLine(szLine)!=0){
		f1tmp = fopen("testD.db","wb");
		f2tmp = fopen("testN.txt","w+t");
//		MessageBox(szLine,"ABC",MB_OK);
		fwrite(&szLine,1,40,f1tmp);
		fprintf(f2tmp,"%s\n",szLine);
		fseek( f2tmp, 0L, SEEK_SET );	
		if (countLine <= space){
			i = countLine;
				fscanf( f2tmp, "%f", &fp );
				z[i] = fp;
		}
		else{
			if (countLine > space && countLine <= space*2){
				i = countLine-space;
					fscanf( f2tmp, "%f", &fp );
					x[i] = fp;
			}
			else{
				i = countLine-space;
					fscanf( f2tmp, "%f", &fp );
					y[i] = fp;
					if (y[i] == y[i-1] && y[i] != 0){
						y[i] = 0;
						break;
					}
			}
		}
		fclose(f1tmp);
		fclose(f2tmp);
		countLine = countLine + 1;
	}
	sfText.Close();
	j = 1;

	m_grid.SetRows(times+2);
	m_grid.SetCols(10);

	Refresh();

	for (i = 1; i < number+2; i++) {
		sprintf(str,"%d",i);
		m_grid.SetTextMatrix(0,i,str);
	}
	for ( j = 1; j <= times; j++) {	
		for (i = 1; i <= number+1; i++) {
			sprintf(str,"%7.4f",y[i+(number+1)*(j-1)]);
			m_grid.SetTextMatrix(j,i,str);
		}
	}
	for (i = 1; i <= number+1; i++) {
		sprintf(str,"%7.7f",z[i]);
		m_grid.SetTextMatrix(times+1,i,str);
	}

	UpdateData(FALSE);
	return TRUE;

}



void CdlgBackground::OnSaveData() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	IndividualLoop = m_individual;
	generation = m_generation;
	DataInput();
	UpdateData(false);
}

void CdlgBackground::OnReset() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);	
	m_individual = 0;
	m_generation = 0;
	UpdateData(false);
}

void CdlgBackground::DataInput()
{
// Note: the byte number of float is 4 (in fwrite)	
	FILE *f1, *f2;
	f1 = fopen("test.db","wb");
	f2 = fopen("DataInput.txt","w+t");
	
	char inputvalue[10],title[10];
	strcpy(title,"Data Input");

	fwrite(&title,1,10,f1);
	fprintf(f2,"%s\n",title);

	fprintf(f2,"%s","Number of individuals in 1st generation:  ");
	fwrite(&IndividualLoop,4,1,f1);
	fprintf(f2,"%10d",IndividualLoop);
	fprintf(f2,"\n");

	fprintf(f2,"%s","Number of generations in GA:  ");
	fwrite(&generation,4,1,f1);
	fprintf(f2,"%10d",generation);
	fprintf(f2,"\n");

	fprintf(f2,"         Y           Z");
	fprintf(f2,"\n");

	for (int i=1;i<=number+1;i++){
		fwrite(&x[i],4,1,f1);
		fwrite(&y[i],4,1,f1);
		fwrite(&z[i],4,1,f1);

		fprintf(f2,"%10.4f",x[i]);
		fprintf(f2,"%10.4f",y[i]);
		fprintf(f2,"%10.4f",z[i]);

		fprintf(f2,"\n");
	}
//	fseek(f1,sizeof(int)*6,SEEK_SET);

	fclose(f1);
	fclose(f2);
}

int CdlgBackground::getArea()
{
	return area;
}

void CdlgBackground::drawO(CDC *pDC)
{

	float dt,b0,b1,b2,b3,xp,yp,zp,xd[202],yd[202],zd[202];
	int nisegs,ntsegs,t,i,k,j,l,m,n;

	int scalex = scale;int scaley = scale;int scalez = scale;

	for ( i = 1; i <= countX; i++) {
		xp = (x[i])*scalex; zp = z[i]*scalez;
		pDC->MoveTo((int) xp-40,(int) zp+40);
		pDC->LineTo((int) xp+40,(int) zp-40);
		pDC->MoveTo((int) xp+40,(int) zp+40);
		pDC->LineTo((int) xp-40,(int) zp-40);
	}
	countX = space;
	for (k = 1; k<=1; k++){
		xd[1]=(x[begincountX+1])*scalex;	zd[1]=z[begincountX+1]*scalez;
		xd[2]=(x[begincountX+1])*scalex;	zd[2]=z[begincountX+1]*scalez;

		xd[countX+3]=(x[countX+begincountX])*scalex;	zd[countX+3]=z[countX+begincountX]*scalez;
		xd[countX+4]=(x[countX+begincountX])*scalex;	zd[countX+4]=z[countX+begincountX]*scalez;
		xd[countX+5]=(x[countX+begincountX])*scalex;	zd[countX+4]=z[countX+begincountX]*scalez;

		for ( l=begincountX+1;l<=begincountX+countX;l++){
			xd[l+2]=(x[l])*scalex;
			zd[l+2]=z[l]*scalez;
		}

		nisegs = countX-3; ntsegs = 100;
		dt = 1 / (float) ntsegs;

		for (i=3;i<=nisegs+2+5;i++){
			for (j=0,t=0.;j<=ntsegs;j++,t+=dt){
				b0 = t*t*t/6.;
				b1 = (1.+3.*t+3.*t*t-3.*t*t*t)/6.;
				b2 = (4.-6.*t*t+3.*t*t*t)/6.;
				b3 = (1.-t)*(1.-t)*(1.-t)/6.;
				xp = b3*xd[i-2]+b2*xd[i-1]+b1*xd[i]+b0*xd[i+1];
				zp = b3*zd[i-2]+b2*zd[i-1]+b1*zd[i]+b0*zd[i+1];
				if (i==3 && j==0)
					pDC->MoveTo((int) xp,(int) zp);
				else
					pDC->LineTo((int) xp,(int) zp);
			}
		}
	}
/*	CreateOpPocupine();
	for (k = 1; k<=countPoc; k++){	
		xp = pocx0[k]*scalex;zp = pocy0[k]*scalez;
		pDC->MoveTo((int) xp,(int) zp);
		xp = pocx1[k]*scalex;zp = pocy1[k]*scalez;
		pDC->LineTo((int) xp,(int) zp);
	}*/	
}

⌨️ 快捷键说明

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