- UID
- 2
- 精华
- 积分
- 7736
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
项目需求:
凸轮曲线计算工具.docx
(352.26 KB, 下载次数: 9)
- // tuluncalcDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "tuluncalc.h"
- #include "tuluncalcDlg.h"
- #include "afxdialogex.h"
- #include <float.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialogEx
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
- END_MESSAGE_MAP()
- // CtuluncalcDlg 对话框
- CtuluncalcDlg::CtuluncalcDlg(CWnd* pParent /*=NULL*/)
- : CDialogEx(CtuluncalcDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- // m_value = _T("");
- // m_angle = _T("");
- m_angle = 0.0;
- m_value = 0.0;
- }
- void CtuluncalcDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- // DDX_Radio(pDX, IDC_ZHIXIAN, m_zhixian);
- // DDX_Text(pDX, IDC_VALUE, m_value);
- DDX_Control(pDX, IDC_PTLIST, m_ptlist);
- // DDX_Text(pDX, IDC_ANGLE, m_angle);
- DDX_Control(pDX, IDC_DRAWA, m_drawa);
- DDX_Control(pDX, IDC_DRAWS, m_draws);
- DDX_Control(pDX, IDC_DRAWV, m_drawv);
- DDX_Text(pDX, IDC_ANGLE, m_angle);
- DDX_Text(pDX, IDC_VALUE, m_value);
- DDX_Control(pDX, IDC_DATALIST, m_datalist);
- }
- BEGIN_MESSAGE_MAP(CtuluncalcDlg, CDialogEx)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_ADDPT, &CtuluncalcDlg::OnBnClickedAddpt)
- ON_BN_CLICKED(IDC_DELPT, &CtuluncalcDlg::OnBnClickedDelpt)
- ON_BN_CLICKED(IDC_DRAW, &CtuluncalcDlg::OnBnClickedDraw)
- ON_BN_CLICKED(IDC_ZHIXIAN, &CtuluncalcDlg::OnBnClickedZhixian)
- ON_BN_CLICKED(IDC_PAOWUXIAN, &CtuluncalcDlg::OnBnClickedPaowuxian)
- ON_BN_CLICKED(IDC_SANJIAOHANSHU, &CtuluncalcDlg::OnBnClickedSanjiaohanshu)
- ON_BN_CLICKED(IDC_WUCIFANG, &CtuluncalcDlg::OnBnClickedWucifang)
- ON_BN_CLICKED(IDC_XIUZHENGSANJIAO, &CtuluncalcDlg::OnBnClickedXiuzhengsanjiao)
- END_MESSAGE_MAP()
- // CtuluncalcDlg 消息处理程序
- BOOL CtuluncalcDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- ((CButton*)GetDlgItem(IDC_WUCIFANG))->SetCheck(TRUE);
- DRAWTYPE=T_WUCIFANG;
- m_datalist.InsertColumn(0,"序号",LVCFMT_CENTER,60);
- m_datalist.InsertColumn(1,"β",LVCFMT_CENTER,60);
- m_datalist.InsertColumn(2,"s",LVCFMT_CENTER,60);
- m_datalist.InsertColumn(3,"v",LVCFMT_CENTER,60);
- m_datalist.InsertColumn(4,"a",LVCFMT_CENTER,60);
- m_datalist.InsertColumn(5,"类型",LVCFMT_CENTER,60);
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- void CtuluncalcDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialogEx::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CtuluncalcDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作区矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- RECT rt;
- CBrush clear(RGB(255,255,255));
- vector<globalPT> pts;
- double xmin,xmax,ymin,ymax;
- int segnum=segpt.size()-1;
- int i,j;
- if(segnum <= 0)
- return;
- CString labelStr;
- int index=0;
- CFont font1,font2;
- CPen redpen(PS_SOLID,1,RGB(255,0,0));
- font1.CreateFont(11,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
- DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"Arial");
- font2.CreateFont(9,0,-900,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
- DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"Arial");
- xmin=segpt[0].B;//用来画横坐标
- xmax=segpt[segnum].B;
- CWnd* cwd[3]={&m_draws,&m_drawv,&m_drawa};
- int type[]={OT_S,OT_V,OT_A};
- for(int k=0;k<3;k++)
- {
- ymin=DBL_MAX;//用来画纵坐标
- ymax=DBL_MIN;
- pts.clear();
- cwd[k]->GetClientRect(&rt);
- CClientDC dc(cwd[k]);
- dc.FillRect(&rt,&clear);
- dc.Rectangle(&rt);
- double step=(xmax-xmin)/(rt.right-2*MARGIN);
- double input=xmin;
- for(i=0;i<segnum;i++)
- {
- int curnum=int((segpt[i+1].B-segpt[i].B)/step);
- for(j=0;j<=curnum;j++,input+=step)
- {
- double output=CalcPoint(segpt[i].type,type[k],paramdata[i],input);
- if(output < ymin)
- ymin=GetOutLineNumber(output,FALSE);
- if(output > ymax)
- ymax=GetOutLineNumber(output,TRUE);
- pts.push_back(globalPT(input,output));
- }
- }
- dc.MoveTo(MARGIN,rt.bottom-MARGIN);
- dc.LineTo(rt.right-MARGIN,rt.bottom-MARGIN);
- dc.MoveTo(MARGIN,rt.bottom-MARGIN);
- dc.LineTo(MARGIN,MARGIN);
- //纵横各分为10段
- int dl;
- dl=int((rt.right-2*MARGIN)/10);
- for(i=0;i<=10;i++)
- {
- dc.MoveTo(MARGIN+dl*i,rt.bottom-MARGIN);
- dc.LineTo(MARGIN+dl*i,rt.bottom-MARGIN-5);
- float label=float(xmin+i*(xmax-xmin)/10);
- labelStr.Format("%4.3f",label);
- dc.SelectObject(font1);
- dc.TextOut(MARGIN+dl*i-5,rt.bottom-MARGIN+5,labelStr);
- }
- dl=int((rt.bottom-2*MARGIN)/10);
- for(i=0;i<=10;i++)
- {
- dc.MoveTo(MARGIN,rt.bottom-MARGIN-dl*i);
- dc.LineTo(MARGIN+5,rt.bottom-MARGIN-dl*i);
- float label=float(ymin+i*(ymax-ymin)/10);
- labelStr.Format("%4.3f",label);
- dc.SelectObject(font2);
- dc.TextOut(MARGIN-10,rt.bottom-MARGIN-dl*i-5,labelStr);
- }
- dc.SelectObject(redpen);
- vector<globalPT>::iterator itor=pts.begin();
- dc.MoveTo(MARGIN,MARGIN);
- i=MARGIN;
- while(itor != pts.end())
- {
-
- int yy=rt.bottom-int(((*itor).y-ymin)/(ymax-ymin)*(rt.bottom-2*MARGIN)+MARGIN);
- dc.LineTo(i,yy);
- ++itor;
- ++i;
- }
- }
- }
- }
- double CtuluncalcDlg::GetOutLineNumber(double input,BOOL isUpperLimit)
- {
- int count=0;
- if(input == (int)input)
- {
- return input;
- }
- BOOL sigflag=FALSE;//负数为TRUE
- BOOL zoomflag=FALSE;//缩小为TRUE
- if(input<0)
- {
- sigflag=TRUE;
- input=-input;
- }
- if(input>10)//22.2222
- {
- while(input>10)
- {
- input /=10;
- count++;
- }
- input=(int)input;
- if(sigflag != isUpperLimit)
- {
- input++;
- }
- if(sigflag)
- {
- input=-input;
- }
- }
- else if(input>1)//2.2222
- {
- input=(int)input;
- if(sigflag != isUpperLimit)
- {
- input++;
- }
- if(sigflag)
- {
- input=-input;
- }
- return input;
- }
- else//0.22222
- {
- zoomflag=TRUE;
- while(input<1)
- {
- input *= 10;
- count++;
- }
- input=(int)input;
- if(sigflag != isUpperLimit)
- {
- input++;
- }
- if(sigflag)
- {
- input=-input;
- }
- }
-
- if(zoomflag)
- {
- while(count--)
- {
- input /= 10;
- }
- }
- else
- {
- while(count--)
- {
- input *= 10;
- }
- }
-
- return input;
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CtuluncalcDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CtuluncalcDlg::OnBnClickedAddpt()
- {
- // TODO: 在此添加控件通知处理程序代码
- UpdateData(TRUE);
- if(!segpt.empty() && segpt[segpt.size()-1].B >= m_angle)
- {
- AfxMessageBox("输入不合法,需要比上一个β值大");
- return;
- }
- CString str;
- str.Format("β=%lf,s(β)=%lf",m_angle,m_value);
- m_ptlist.AddString(str);
- segpt.push_back(segPT(DRAWTYPE,m_angle,m_value));
- }
- void CtuluncalcDlg::OnBnClickedDelpt()
- {
- // TODO: 在此添加控件通知处理程序代码
- int num=m_ptlist.GetCount();
- if(num)
- {
- m_ptlist.DeleteString(num-1);
- segpt.pop_back();
- }
- }
- void CtuluncalcDlg::OnBnClickedDraw()
- {
- // TODO: 在此添加控件通知处理程序代码
- UpdateData(TRUE);
- int segnum=segpt.size()-1;
- if(segnum <= 0)
- {
- AfxMessageBox("参数有误");
- }
- paramdata.clear();
- for(int i=0;i<segnum;i++)
- {
- if(segpt[i].type == T_ZHIXIAN)
- {
- }
- else if(segpt[i].type == T_PAOWUXIAN)
- {
- }
- else if(segpt[i].type == T_SANJIAOHANSHU)
- {
- }
- else if(segpt[i].type == T_WUCIFANG)
- {
- double f0[7],f1[7],f2[7],f3[7],f4[7],f5[7];
- double* f[6]={f0,f1,f2,f3,f4,f5};
- double BA=segpt[i].B,BB=segpt[i+1].B,SA=segpt[i].S,SB=segpt[i+1].S;
- f[0][0]=1;f[0][1]=BA;f[0][2]=BA*BA;f[0][3]=BA*BA*BA;f[0][4]=BA*BA*BA*BA;f[0][5]=BA*BA*BA*BA*BA;f[0][6]=SA;
- f[1][0]=1;f[1][1]=BB;f[1][2]=BB*BB;f[1][3]=BB*BB*BB;f[1][4]=BB*BB*BB*BB;f[1][5]=BB*BB*BB*BB*BB;f[1][6]=SB;
- f[2][0]=0;f[2][1]=1; f[2][2]=2*BA; f[2][3]=3*BA*BA; f[2][4]=4*BA*BA*BA; f[2][5]=5*BA*BA*BA*BA; f[2][6]=0;
- f[3][0]=0;f[3][1]=1; f[3][2]=2*BB; f[3][3]=3*BB*BB; f[3][4]=4*BB*BB*BB; f[3][5]=5*BB*BB*BB*BB; f[3][6]=0;
- f[4][0]=0;f[4][1]=0; f[4][2]=1; f[4][3]=3*BA; f[4][4]=6*BA*BA; f[4][5]=10*BA*BA*BA; f[4][6]=0;
- f[5][0]=0;f[5][1]=0; f[5][2]=1; f[5][3]=3*BB; f[5][4]=6*BB*BB; f[5][5]=10*BB*BB*BB; f[5][6]=0;
- if(!WUCIFANG(f))
- {
- AfxMessageBox("参数错误!");
- return;
- }
- GENERALDATA data;
- double *ptr=data.what.WUCIFANG.param;
- ptr[0]=f[0][6];ptr[1]=f[1][6];ptr[2]=f[2][6];ptr[3]=f[3][6];ptr[4]=f[4][6];ptr[5]=f[5][6];
- paramdata.push_back(data);
- }
- else if(segpt[i].type == T_XIUZHENGSANJIAO)
- {
- }
- }
- Invalidate(FALSE);
- double xmin=segpt[0].B;//用来画横坐标
- double xmax=segpt[segnum].B;
- double step=(xmax-xmin)/100;
- double input=xmin;
- int i,j,index=0;
- CString str;
- for(i=0;i<segnum;i++)
- {
- int curnum=int((segpt[i+1].B-segpt[i].B)/step);
- for(j=0;j<=curnum;j++,input+=step)
- {
- double outputs=CalcPoint(segpt[i].type,OT_S,paramdata[i],input);
- double outputv=CalcPoint(segpt[i].type,OT_V,paramdata[i],input);
- double outputa=CalcPoint(segpt[i].type,OT_A,paramdata[i],input);
- str.Format("%d",index);
- m_datalist.InsertItem(index,str);
- str.Format("%lf",input);
- m_datalist.SetItemText(index,1,str);
- str.Format("%lf",outputs);
- m_datalist.SetItemText(index,2,str);
- str.Format("%lf",outputv);
- m_datalist.SetItemText(index,3,str);
- str.Format("%lf",outputa);
- m_datalist.SetItemText(index,4,str);
- switch(segpt[i].type)
- {
- case T_ZHIXIAN:
- str="直线";
- break;
- case T_PAOWUXIAN:
- str="抛物线";
- break;
- case T_SANJIAOHANSHU:
- str="三角函数";
- break;
- case T_WUCIFANG:
- str="五次方";
- break;
- case T_XIUZHENGSANJIAO:
- str="修正三角函数";
- break;
- }
- m_datalist.SetItemText(index,5,str);
- index++;
- }
- }
- }
- BOOL CtuluncalcDlg::WUCIFANG(double* arr[6])
- {
- int N=6;
- int i,j,k;
- bool flag=false;
- for(i=0;i<N;i++)
- {
- if(!arr[i][i])
- {
- flag=false;
- for(j=i;j<N;j++)
- {
- if(arr[j][i])
- {
- flag=true;
- double* temp=arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
- }
- if(!flag)
- {
- goto ERROR1;
- }
- }
- for(j=N;j>=0;j--)
- {
- arr[i][j] /= arr[i][i];
- }
- for(k=i+1;k<N;k++)
- {
- if(arr[k][i])
- {
- for(j=N;j>=i;j--)
- {
- arr[k][j] -= arr[k][i]*arr[i][j];
- }
- }
- }
- }
- //1 ? ? ? ? ? ?
- //0 1 ? ? ? ? ?
- //0 0 1 ? ? ? ?
- //0 0 0 1 ? ? ?
- //0 0 0 0 1 ? ?
- //0 0 0 0 0 1 ?
- //回带
- for(j=N-1;j>0;j--)
- {
- for(i=0;i<j;i++)
- {
- arr[i][N] -= arr[i][j]*arr[j][N];
- arr[i][j]=0;
- }
- }
- return TRUE;
- ERROR1:
- return FALSE;
- }
- void CtuluncalcDlg::OnBnClickedZhixian()
- {
- // TODO: 在此添加控件通知处理程序代码
- DRAWTYPE=T_ZHIXIAN;
- }
- void CtuluncalcDlg::OnBnClickedPaowuxian()
- {
- // TODO: 在此添加控件通知处理程序代码
- DRAWTYPE=T_PAOWUXIAN;
- }
- void CtuluncalcDlg::OnBnClickedSanjiaohanshu()
- {
- // TODO: 在此添加控件通知处理程序代码
- DRAWTYPE=T_SANJIAOHANSHU;
- }
- void CtuluncalcDlg::OnBnClickedWucifang()
- {
- // TODO: 在此添加控件通知处理程序代码
- DRAWTYPE=T_WUCIFANG;
- }
- void CtuluncalcDlg::OnBnClickedXiuzhengsanjiao()
- {
- // TODO: 在此添加控件通知处理程序代码
- DRAWTYPE=T_XIUZHENGSANJIAO;
- }
- double CtuluncalcDlg::CalcPoint(int type,int outtype,GENERALDATA& param,double input)
- {
- double result=0.0;
- switch(type)
- {
- case T_ZHIXIAN:
- if(outtype == OT_S)
- {
- }
- else if(outtype == OT_V)
- {
- }
- else if(outtype == OT_A)
- {
- }
- break;
- case T_PAOWUXIAN:
- if(outtype == OT_S)
- {
- }
- else if(outtype == OT_V)
- {
- }
- else if(outtype == OT_A)
- {
- }
- break;
- case T_SANJIAOHANSHU:
- if(outtype == OT_S)
- {
- }
- else if(outtype == OT_V)
- {
- }
- else if(outtype == OT_A)
- {
- }
- break;
- case T_WUCIFANG:
- {
- double B=input;
- double* r=param.what.WUCIFANG.param;
- if(outtype == OT_S)
- {
- result=r[0]+r[1]*B+r[2]*B*B+r[3]*B*B*B+r[4]*B*B*B*B+r[5]*B*B*B*B*B;
- }
- else if(outtype == OT_V)
- {
- result=r[1]+2*r[2]*B+3*r[3]*B*B+4*r[4]*B*B*B+5*r[5]*B*B*B*B;
- }
- else if(outtype == OT_A)
- {
- result=2*r[2]+6*r[3]*B+12*r[4]*B*B+20*r[5]*B*B*B;
- }
- }
- break;
- case T_XIUZHENGSANJIAO:
- if(outtype == OT_S)
- {
- }
- else if(outtype == OT_V)
- {
- }
- else if(outtype == OT_A)
- {
- }
- break;
- }
- return result;
- }
复制代码
|
|