📄 dglsview.cpp
字号:
// DglsView.cpp : implementation of the CDglsView class
//
#include "stdafx.h"
#include "Dgls.h"
//#include "afxtempl.h"//链表头文件
#include "math.h"//数学公式
#include "DglsDoc.h"
#include "DglsView.h"
#include "Limendialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDglsView
IMPLEMENT_DYNCREATE(CDglsView, CView)
BEGIN_MESSAGE_MAP(CDglsView, CView)
//{{AFX_MSG_MAP(CDglsView)
ON_COMMAND(ID_EDIT_INPUTLIMEN, OnEditInputlimen)
ON_COMMAND(ID_COLOR, OnColor)
ON_COMMAND(ID_LXLINE, OnLxline)
ON_UPDATE_COMMAND_UI(ID_LXLINE, OnUpdateLxline)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDglsView construction/destruction
CDglsView::CDglsView()
{
// TODO: add construction code here
}
CDglsView::~CDglsView()
{
}
BOOL CDglsView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDglsView drawing
void CDglsView::OnDraw(CDC* pDC)
{
CDglsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//CPaintDC dc(this);
//dc.SetROP2(R2_NOT);
//CRect Recto;
//获取客户区尺寸
//GetClientRect(&Recto);
//pDC->SetViewportOrg(0,Recto.Height);
///pDC->SetViewportOrg(0,400);
//pDC->Ellipse(20,20,100,100);//2*Limen,2*Limen);
pDC->TextOut(20,10,"左边两条线段为阈值参考线,第一条长100,第二条长50");
pDC->TextOut(20,35,"先点击画线菜单按钮,画曲线段,然后通过道格拉斯压缩菜单设置颜色和阈值");
pDC->MoveTo(30,110);
pDC->LineTo(30,210);
pDC->MoveTo(50,160);
pDC->LineTo(50,210);
int index;
index=pDoc->m_MLine.GetSize ();
while(index--)
pDoc->GetMLine (index)->My(pDC);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CDglsView printing
BOOL CDglsView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDglsView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDglsView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDglsView diagnostics
#ifdef _DEBUG
void CDglsView::AssertValid() const
{
CView::AssertValid();
}
void CDglsView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDglsDoc* CDglsView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDglsDoc)));
return (CDglsDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDglsView message handlers
void CDglsView::OnEditInputlimen()
{
// TODO: Add your command handler code here
CLimenDialog dlg;//定义一个对话框对象
dlg.m_nLimen =50;//设置编辑框初始值
if(dlg.DoModal()==IDOK)//显示对话框
{
CClientDC pDC(this);
CDglsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen newpen(PS_SOLID,1,color);//设置颜色
CPen *old=pDC.SelectObject(&newpen);
Limen=dlg.m_nLimen ;//接受并存储编辑框数据
int first=0,last=num-1; //first为第一点,last为第二点
dgls(first,last,Limen);
}
}
void CDglsView::OnColor()
{
// TODO: Add your command handler code here
CColorDialog ColorDlg;
if(ColorDlg.DoModal())
{
color=ColorDlg.GetColor();
}
}
void CDglsView::OnLxline()
{
// TODO: Add your command handler code here
m_nlxline=true;
start=0;
flag=1;
}
void CDglsView::OnUpdateLxline(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_nlxline);
}
void CDglsView::OnLButtonDown(UINT nFlags, CPoint point) //鼠标左击时执行
{
// TODO: Add your message handler code here and/or call default
if(flag!=1)
{
MessageBox("请先选择画线按钮!","警告");
}
else
{
if(start==false)
{
m_Start=point;
m_End=point;
SetCapture();//设置鼠标
SetCursor(m_Hcursor);//光标句柄
start=true;
if(m_nlxline)//如果画连续执行,设置坐标点数组,保存坐标位置
{
points=new CPoint[100];
points[0]=point;
num=1;
}
}
else
{
m_End=point;
CClientDC pDC(this);
CDglsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(m_nlxline)
{
pDC.MoveTo(m_Start);
pDC.LineTo(m_End);
m_Start=point;
start=true;//继续执行画线第二步
points[num]=point;
num++;
}
}
}
CView::OnLButtonDown(nFlags, point);
}
void CDglsView::OnMouseMove(UINT nFlags, CPoint point) //鼠标移动时执行
{
// TODO: Add your message handler code here and/or call default
if(start==true)
{
CClientDC dc(this);
dc.SetROP2(R2_NOT);
SetCursor(m_Hcursor);
if(m_nlxline)
{
dc.MoveTo(m_Start);
dc.LineTo(m_End);//用反色擦除上条直线
dc.MoveTo(m_Start);
dc.LineTo(point);//重画直线
}
m_End=point;
}
CView::OnMouseMove(nFlags, point);
}
void CDglsView::OnRButtonDown(UINT nFlags, CPoint point) //鼠标右击时执行
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SetROP2(R2_NOT);
SetCursor(m_Hcursor);
if(m_nlxline && start==true)
{
int nChoice=AfxMessageBox("是否保留该线?",MB_YESNO|MB_ICONQUESTION);
//调用消息对话框
if(nChoice==IDYES) //回答是时
{
points[num]=point;
num++;
CDglsDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
int* pointx=new int[num];
int* pointy=new int[num];
for(int i=0;i<num;i++)
{
pointx[i]=points[i].x;
pointy[i]=points[i].y;
}
pDoc->AddMLine(pointx,pointy,num,0);//保存点
}
else //回答否时
{
for(int i=1;i<num;i++)
{
dc.MoveTo(points[i-1]);
dc.LineTo(points[i]);
}
dc.MoveTo(points[num-1]);
dc.LineTo(point);
}
}
start =false;
ReleaseCapture();//释放鼠标
ClipCursor(NULL);//还原鼠标
CView::OnRButtonDown(nFlags, point);
}
void CDglsView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MessageBox("未设置双击功能!","警告");
CView::OnLButtonDblClk(nFlags, point);
}
void CDglsView::dgls(int first, int last, float dd)//dgls,dd为阈值
{
int* pointx=new int[num];
int* pointy=new int[num];
for(int i=0;i<num;i++)
{
pointx[i]=points[i].x;
pointy[i]=points[i].y;
}
//first为第一点,last为第二点
CClientDC pDC(this);
CDglsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen newpen(PS_SOLID,1,color);//设置颜色
CPen *old=pDC.SelectObject(&newpen);
float A,B,C;
A=pointy[last]-pointy[first];//y2-y1
B=pointx[first]-pointx[last];//x1-x2
C=pointx[last]*pointy[first]-pointx[first]*pointy[last];//x2*y1-x1*y2
float d[100];//距离矩阵
float maxd=0;
int k=0;//标记最大距离点
for(int j=first+1;j<last;j++)
{
d[j]=abs(A*pointx[j]+B*pointy[j]+C)/sqrt(A*A+B*B);
if(maxd<d[j])
{
maxd=d[j];
k=j;
}
}
if(maxd<=dd)
{
pDC.MoveTo(pointx[first],pointy[first]);
pDC.LineTo(pointx[last],pointy[last]);
}
else
{
dgls(first,k,Limen);
dgls(k,last,Limen);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -