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

📄 demowindow.cpp

📁 大学物理创新平台
💻 CPP
字号:
// demowindow.cpp : implementation file

#include "stdafx.h"
#include "demowindow.h"
#include "dlldef.h"
#include "resource.h"
#include <math.h>
#include "string.h"


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

#define ROTATE_LEFT_RIGHT		1
#define ROTATE_UP_DOWN			2

extern int S_ViewMode;
extern int S_demoID;
extern bool S_IsDemo;
float V,X,P_n,P_m;

int P_direction;
STRING word;

const float Pi =3.1415926f;
int bCalculated = 0;
/////////////////////////////////////////////////////////////////////////////
// demowindow

demowindow::demowindow()
{
    word.buffer=(unsigned char *)malloc(totalbyte("丁二烯电荷密度分布",HZ_SIZE_24));
	LoadHZCharacter24(S_FONT,&word, "丁二烯电荷密度分布");
	
	
	S_ViewMode=VIEWMODE1;
	P_direction=1;
	S_special_function1_pressed=0;
	S_special_function2_pressed=0;
    P_m = P_n = 1;

}
demowindow::~demowindow()
{
	DeInitWnd(0);
}

BEGIN_MESSAGE_MAP(demowindow, CStatic)
	//{{AFX_MSG_MAP(demowindow)
	ON_WM_PAINT()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// demowindow message handlers

void demowindow::S_Begin()
{
     S_Step=0;
	 S_InitGraph();
	 bCalculated = 0;
}

void demowindow::OnPaint() 
{
    static int IsInit=0;

	CPaintDC dc(this); // device context for painting

	// TODO: Add your message handler code here
	if(IsInit==0)
	{ 
     CRect rect;
	 GetClientRect(&rect);
	 InitGLWnd(dc.m_hDC,S_ViewMode,rect.Width(),rect.Height(),0,0);
	 BeginGL(dc.m_hDC,0);
	 S_Step=0;
	 S_switchpic();
	 EndGL(dc.m_hDC);
	 IsInit=1;
 	}
	else 
	{
	 BeginGL(dc.m_hDC,0);
	 S_switchpic();
	 EndGL(dc.m_hDC);
	}

	// Do not call CStatic::OnPaint() for painting messages
}

void demowindow::S_InitGraph()
{
/*	switch(S_demoID)    // 为不同演示的时间初值
	{
	case 1:
		break;
	case 2:
		break;
	case 3:
		break;
	} */
    Invalidate(false);
}

void demowindow::S_Demo()
{
    S_Step+=1;

    if(S_Step>2500)S_Step-=2500;

    Invalidate(false);
}

void demowindow::S_SetParameter(float parameter1,float parameter2,float parameter3,float parameter4,float parameter5)
{
	P_m =parameter1;
	P_n=parameter2;
    X=parameter3;
    V=parameter4;
}

void demowindow::S_ChangeParameter(int parameterID, int value)
{
	switch(S_demoID)
	{
	 case 1:
    	 switch(parameterID)
		 {
  		  case IDC_PARAMETER1:
			  P_m=value;
		      break;
	      case IDC_PARAMETER2:
			  P_n=value;
		      break;
	      case IDC_PARAMETER3:
		      X=value;
			  break;
		  case IDC_PARAMETER4:
		      V=value;
			  break;
		  case IDC_PARAMETER5:
		      break;
		 }
	     break;
	case 2:
    	 switch(parameterID)
		 {
  		  case IDC_PARAMETER1:
			  P_m=value;
		      break;
	      case IDC_PARAMETER2:
			  P_n=value;
		      break;
	       case IDC_PARAMETER3:
		      X=value;
			  break;
		  case IDC_PARAMETER4:
		      V=value;
			  break;
		  case IDC_PARAMETER5:
		      break;
		 }
	     break;
		case 3:
    	 switch(parameterID)
		 {
  		  case IDC_PARAMETER1:
			  P_m=value;
		      break;
	      case IDC_PARAMETER2:
			  P_n=value;
		      break;
	       case IDC_PARAMETER3:
		      X=value;
			  break;
		  case IDC_PARAMETER4:
		      V=value;
			  break;
		  case IDC_PARAMETER5:
		      break;
		 }
	     break;
	}
}

void demowindow::S_GetParameter(float *parameter1, float *parameter2, float *parameter3, float *parameter4, float *parameter5)
{
	switch(S_demoID)
	{
	 case 1:
         *parameter1=P_m;	    
		 *parameter2=P_n;
	     *parameter3=X;
		 *parameter4=V;
		 		 
		 break;
	 case 2:
         *parameter1=P_m;
	     *parameter2=P_n;
	     *parameter3=X;
		 *parameter4=V;
		 break;
	case 3:
         *parameter1=P_m;
	     *parameter2=P_n;
	     *parameter3=X;
		 *parameter4=V;
		 break;
	}
}

void demowindow::S_switchpic()
{
     switch(S_demoID)
	 {
	 case 1:S_Picture1();break;
	 case 2:S_Picture2();break;
	 case 3:S_Picture3();break;
	 }
}

CString demowindow::S_OnSpecialFunction1(int inittext,int *pressed)
{
	CString str;
    str.Format("");
	if(S_special_function1_pressed)S_special_function1_pressed=0;
	else S_special_function1_pressed=1;
	if(inittext)S_special_function1_pressed=0;
    switch(S_demoID)
	{
	 case 1:
		 str.Format("球/盘");
         break;
	}
	*pressed=S_special_function1_pressed;
	return str;
}

CString demowindow::S_OnSpecialFunction2(int inittext,int *pressed)
{
	CString str;
    str.Format("");
	if(S_special_function2_pressed)S_special_function2_pressed=0;
	else S_special_function2_pressed=1;
	if(inittext)S_special_function2_pressed=0;
    switch(S_demoID)
	{
	 case 1:
//		 if(S_special_function2_pressed)str.Format("显示\n重力矩");
//		 else str.Format("关闭\n重力矩");
         break;
	 case 2:
//		 if(S_special_function2_pressed)str.Format("显示角\n动量合成");
//		 else str.Format("关闭角\n动量合成");
         break;
	}
    *pressed=S_special_function2_pressed;
	return str;
}

CString demowindow::S_OnSpecialFunction3(int inittext,int *pressed)
{
	CString str;
    str.Format("");
	if(S_special_function3_pressed)S_special_function3_pressed=0;
	else S_special_function3_pressed=1;
	if(inittext)S_special_function3_pressed=0;
    switch(S_demoID)
	{
	 case 1:
//		 if(S_special_function3_pressed)str.Format("显示\n重力矩");
//		 else str.Format("关闭\n重力矩");
         break;
	 case 2:
//		 if(S_special_function3_pressed)str.Format("显示角\n动量合成");
//		 else str.Format("关闭角\n动量合成");
         break;
	}
    *pressed=S_special_function3_pressed;
	return str;
}

CString demowindow::S_OnSpecialFunction4(int inittext,int *pressed)
{
	CString str;
    str.Format("");
	if(S_special_function4_pressed)S_special_function4_pressed=0;
	else S_special_function4_pressed=1;
	if(inittext)S_special_function4_pressed=0;
    switch(S_demoID)
	{
	 case 1:
//		 if(S_special_function4_pressed)str.Format("显示\n重力矩");
//		 else str.Format("关闭\n重力矩");
         break;
	 case 2:
//		 if(S_special_function4_pressed)str.Format("显示角\n动量合成");
//		 else str.Format("关闭角\n动量合成");
         break;
	}
    *pressed=S_special_function4_pressed;
	return str;
}

void demowindow::m_setpoint(POINT3F *p, float x, float y, float z)
{
    p[0].x=x;
	p[0].y=y;
	p[0].z=z;
}

void demowindow::m_setcolor(RGB *color, float r, float g, float b)
{
    color[0].r=r;
	color[0].g=g;
	color[0].b=b;
}
static POINT3F  p[200][40];
	
static RGB  Color,color[200][40];

void demowindow::S_Picture1()
{
	if(S_special_function1_pressed){
	int i,j;
	float h,L,Q,PI=3.1415926;
    DIRECT direction;
    h=2*P_m;L=P_n; Q=0.0001*X;
    direction.alpha =0;	direction.beta =0;
	EnableLight();
	SetLineWidth(3);
   for(i=0;i<200;i++)for(j=0;j<40;j++)    
{
 
   p[i][j].x=(i*1-100)*L/100;
   p[i][j].y=(j*1-20)*h/40;
   p[i][j].z=Q*(pow(p[i][j].x,2)+pow(p[i][j].y,2))-0.2*L;
 
 if(p[i][j].z>0)
 {color[i][j].r=255;color[i][j].g=255*exp(-0.05*p[i][j].z);color[i][j].b=255*exp(-0.05*p[i][j].z);}
 else
 {color[i][j].b=255;color[i][j].g=255*exp(0.05*p[i][j].z);color[i][j].r=255*exp(0.05*p[i][j].z);}
   }
  DisableLight();	
  CDrawColorSurface(&p[0][0],200,40,&color[0][0],255);
	}
 else  
 {
	 RGB  color;
int  i;
POINT3F  toppoint;
float  r[90],   z[90];		// 回旋曲线由90点组成
DIRECT  direction;
color.r=125;	   color.g=125;	color.b=255;
toppoint.x=0;	toppoint.y=0;	toppoint.z=0;
direction.alpha =0;			   direction.beta =0;

for(i=0;i<90;i++)     // 循环计算
	{
	z[i]=-50+0.012*i*i;   //顶点高度-50,向上伸张的抛物线
	r[i]=i;
	}
EnableLight();    //加光照
CDrawRSCurface(toppoint, direction, &r[0], &z[0], 90, 60, color);        
 }
}

void demowindow::S_Picture2()
{
POINT3F  p[2],q[4],s[4];
RGB  color,color1,color2;
DIRECT  direction,direction1;
float A,L=200,pi=3.1415926;
int i;
A=1000*P_m;
m_setcolor(&color,150,150,150);
m_setcolor(&color1,0,0,0);
m_setcolor(&color2,200,200,255);
direction.alpha =0;
direction.beta=0;
direction1.alpha =90;
direction1.beta=0;
q[0].x=s[0].x=s[1].x=-100+L/8;    q[1].x=q[0].x+L/4;
q[2].x=s[2].x=s[3].x=q[1].x+L/4;   q[3].x=-100+7*L/8;
q[0].y=q[1].y=q[2].y=q[3].y=0;
q[0].z=q[1].z=q[2].z=q[3].z=0;
s[0].y=q[0].y+3.5;     s[0].z=q[0].z;
s[1].y=q[0].y-3.5;      s[1].z=q[0].z;
s[2].y=q[2].y+3.5;      s[2].z=q[2].z;
s[3].y=q[2].y-3.5;      s[3].z=q[2].z;
EnableLight();
CDrawBall(q[0],direction,0,P_n,color,255,32,55,GLU_FILL,false);
CDrawBall(q[1],direction,0,P_n,color,255,32,55,GLU_FILL,false);
CDrawBall(q[2],direction,0,P_n,color,255,32,55,GLU_FILL,false);
CDrawBall(q[3],direction,0,P_n,color,255,32,55,GLU_FILL,false);
BeginDraw(q[1],direction1,0);
DrawCylinder(2,2,color2,255,L/4,30,50,GLU_LINE,false);
EndDraw();
BeginDraw(s[0],direction1,0);
DrawCylinder(2,2,color2,255,L/4,30,50,GLU_LINE,false);
EndDraw();
EndDraw();
BeginDraw(s[1],direction1,0);
DrawCylinder(2,2,color2,255,L/4,30,50,GLU_LINE,false);
EndDraw();
EndDraw();
BeginDraw(s[2],direction1,0);
DrawCylinder(2,2,color2,255,L/4,30,50,GLU_LINE,false);
EndDraw();
EndDraw();
BeginDraw(s[3],direction1,0);
DrawCylinder(2,2,color2,255,L/4,30,50,GLU_LINE,false);
EndDraw();

SetLineWidth(3);
p[0].z=p[1].z=0;
for(i=0;i<201;i++)    
{
p[0].x=i-100;p[1].x=p[0].x+1;
p[0].y=A*(pow(sin(i*pi/L),2)+pow(sin(2*i*pi/L),2))/L;
p[1].y=A*(pow(sin((i+1)*pi/L),2)+pow(sin(2*(i+1)*pi/L),2))/L;	
if(i<200)
CDrawCurve(&p[0],2,color1);
}
DrawHZCharacter(-80,90,-0, FCENTER, FBOTTOM,
                 word,color1);

/*  //输出回旋曲面*/


}

void demowindow::S_Picture3()
{
}

void demowindow::S_RefreshPic()
{
	Invalidate(false);
}

void demowindow::OnSize(UINT nType, int cx, int cy) 
{
	CStatic::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	DeInitWnd(0);
    
	HWND hwnd=GetSafeHwnd();
	HDC hdc=::GetDC(hwnd);
	InitGLWnd(hdc,S_ViewMode,cx,cy,10,0);
	Invalidate(false);
}

int demowindow::S_OpenFile(FILE *fp)
{
	char temp[255];
	fscanf(fp,"%s\n",temp);          // 注释行
    fscanf(fp,"%s\n",temp);          // 参数提示行
    fscanf(fp,"%f\n",&P_m);       // 参数1的数值
    fscanf(fp,"%s\n",temp);          // 参数提示行
    fscanf(fp,"%d\n",&P_direction);       // 参数2的数值
    fscanf(fp,"%s\n",temp);          // 参数提示行
    fscanf(fp,"%d",&P_n);       // 参数2的数值
    fclose(fp);
	return 1;
}

void demowindow::S_SaveFile(FILE *fp)
{
  fprintf(fp,"%%对称性演示参数文件%%\n");
  fprintf(fp,"半径\n%f\n",    P_m);
  fprintf(fp,"转动方向\n%d\n",P_direction);  // 辅助物理参数----转动方向
  fprintf(fp,"转速\n%d",      P_n);      // 辅助物理参数----转速
  fclose(fp);
}

void demowindow::S_ButtonDown()
{

}

void demowindow::S_ButtonUp()
{

}

void demowindow::S_ButtonLeft()
{
  P_direction=-1;
}

void demowindow::S_ButtonRight()
{
  P_direction=1;
}

void demowindow::S_ButtonResume()
{

}

void demowindow::S_ResumePic()
{
   S_Step=0;
   Invalidate(false);
}

CString demowindow::S_RateOfParameter1(float *rate)
{
     CString type="整数";  // 或 "小数"
	 *rate=1;

	 return type;
}

CString demowindow::S_RateOfParameter2(float *rate)
{
     CString type="整数";  
	 *rate=1;

	 return type;
}

CString demowindow::S_RateOfParameter3(float *rate)
{
     CString type="整数";
	 *rate=1;

	 return type;
}

CString demowindow::S_RateOfParameter4(float *rate)
{
     CString type="小数";
	 *rate=1;

	 return type;
}

CString demowindow::S_RateOfParameter5(float *rate)
{
     CString type="整数";
	 *rate=1;

	 return type;
}

void demowindow::S_ModifyParameter()
{
/*    modifyDlg dlg;
	dlg.m_parameter[0].Format("%.3f",P_angle);
	dlg.m_parameter[1].Format("%.3f",P_velocity0);
    dlg.m_parameter[2].Format("%.3f",P_x0);
    dlg.m_parameter[3].Format("%.3f",P_y0);
	if(dlg.DoModal()==IDOK)
	{
		P_angle    =atof(dlg.m_parameter[0]);
		P_velocity0=fabs(atof(dlg.m_parameter[1]));
        P_x0       =atof(dlg.m_parameter[2]);
        P_y0       =atof(dlg.m_parameter[3]);
	}  */
}

void demowindow::S_SaveData(char *DataFilePath)
{
/*    char filename[255];

    sprintf(filename,"%scaisoft.dat",DataPath);
    
	CFileDialog openBox(true,NULL,filename,OFN_HIDEREADONLY,"演示数据文件(*.dat)|*.dat|所有文件(*.*)|*.*||",NULL);
	
	openBox.m_ofn.lpstrTitle="保存轨迹数据对话框";

	if(openBox.DoModal()==IDOK)
	{
         FILE *fp;
		 if((fp=fopen(openBox.m_ofn.lpstrFile,"wt"))!=NULL)
		 {
  		     progressdlg *progress=new progressdlg;     // 显示保存进度
			 progress->m_mention.SetWindowText("正在保存数据,请稍候...");
			 progress->SetWindowText("保存数据进度");

			 fprintf(fp,"%%保存轨迹数据: x,y%%\n");
             fprintf(fp,"%%平面曲线数据%%\n");
			 fprintf(fp,"%%数据个数, 100%%\n");
			 float n=0;
			 int i=0;
			 POINT3F p1,p2;
			 float angle0=3.1415926*P_angle/180.;
             
		     m_setpoint(&p1,P_x0,P_y0,-1);
			 for(i=0;i<100;i++)
			 {
			   progress->m_pos=i;
			   fprintf(fp,"%f,%f\n",p1.x,p1.y);
			   m_setpoint(&p2,P_x0+P_velocity0*cos(angle0)*n,P_y0+P_velocity0*sin(angle0)*n-4.9*n*n,-1);
 	           p1=p2;
	           n+=0.05;
			 } 
			 fclose(fp);

			 progress->EndDialog(NULL);   // 关闭进度窗口
             MessageBox("数据保存完毕","提示",0);
		 }
	}  */
}

void demowindow::S_backward()
{
/*  S_Step-=5;
    if(S_Step<0)S_Step=0;  */
}

void demowindow::S_forward()
{
/*  S_Step+=5;
*/

}

void demowindow::S_LeftButtonDown(CPoint point)
{

}

void demowindow::S_LeftButtonUp(CPoint point)
{

}

void demowindow::S_LeftButtonDblClk(CPoint point)
{

}

void demowindow::S_RightButtonDown(CPoint point)
{

}

void demowindow::S_RightButtonUp(CPoint point)
{

}

void demowindow::S_RightButtonDblClk(CPoint point)
{

}

void demowindow::S_MouseMove(CPoint point)
{

}

⌨️ 快捷键说明

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