凸轮曲线作图工具
项目需求:// 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.B;//用来画横坐标
xmax=segpt.B;
CWnd* cwd={&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->GetClientRect(&rt);
CClientDC dc(cwd);
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.B-segpt.B)/step);
for(j=0;j<=curnum;j++,input+=step)
{
double output=CalcPoint(segpt.type,type,paramdata,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.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.type == T_ZHIXIAN)
{
}
else if(segpt.type == T_PAOWUXIAN)
{
}
else if(segpt.type == T_SANJIAOHANSHU)
{
}
else if(segpt.type == T_WUCIFANG)
{
double f0,f1,f2,f3,f4,f5;
double* f={f0,f1,f2,f3,f4,f5};
double BA=segpt.B,BB=segpt.B,SA=segpt.S,SB=segpt.S;
f=1;f=BA;f=BA*BA;f=BA*BA*BA;f=BA*BA*BA*BA;f=BA*BA*BA*BA*BA;f=SA;
f=1;f=BB;f=BB*BB;f=BB*BB*BB;f=BB*BB*BB*BB;f=BB*BB*BB*BB*BB;f=SB;
f=0;f=1; f=2*BA;f=3*BA*BA; f=4*BA*BA*BA; f=5*BA*BA*BA*BA; f=0;
f=0;f=1; f=2*BB;f=3*BB*BB; f=4*BB*BB*BB; f=5*BB*BB*BB*BB; f=0;
f=0;f=0; f=1; f=3*BA; f=6*BA*BA; f=10*BA*BA*BA; f=0;
f=0;f=0; f=1; f=3*BB; f=6*BB*BB; f=10*BB*BB*BB; f=0;
if(!WUCIFANG(f))
{
AfxMessageBox("参数错误!");
return;
}
GENERALDATA data;
double *ptr=data.what.WUCIFANG.param;
ptr=f;ptr=f;ptr=f;ptr=f;ptr=f;ptr=f;
paramdata.push_back(data);
}
else if(segpt.type == T_XIUZHENGSANJIAO)
{
}
}
Invalidate(FALSE);
double xmin=segpt.B;//用来画横坐标
double xmax=segpt.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.B-segpt.B)/step);
for(j=0;j<=curnum;j++,input+=step)
{
double outputs=CalcPoint(segpt.type,OT_S,paramdata,input);
double outputv=CalcPoint(segpt.type,OT_V,paramdata,input);
double outputa=CalcPoint(segpt.type,OT_A,paramdata,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.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)
{
int N=6;
int i,j,k;
bool flag=false;
for(i=0;i<N;i++)
{
if(!arr)
{
flag=false;
for(j=i;j<N;j++)
{
if(arr)
{
flag=true;
double* temp=arr;
arr=arr;
arr=temp;
}
}
if(!flag)
{
goto ERROR1;
}
}
for(j=N;j>=0;j--)
{
arr /= arr;
}
for(k=i+1;k<N;k++)
{
if(arr)
{
for(j=N;j>=i;j--)
{
arr -= arr*arr;
}
}
}
}
//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 -= arr*arr;
arr=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+r*B+r*B*B+r*B*B*B+r*B*B*B*B+r*B*B*B*B*B;
}
else if(outtype == OT_V)
{
result=r+2*r*B+3*r*B*B+4*r*B*B*B+5*r*B*B*B*B;
}
else if(outtype == OT_A)
{
result=2*r+6*r*B+12*r*B*B+20*r*B*B*B;
}
}
break;
case T_XIUZHENGSANJIAO:
if(outtype == OT_S)
{
}
else if(outtype == OT_V)
{
}
else if(outtype == OT_A)
{
}
break;
}
return result;
}
楼主学机械的? xpx 发表于 2014-3-23 22:35
楼主学机械的?
我学通信的,这是给朋友做的 顶一个。。。。。。。。。。。。。
页:
[1]