方格网程序及算法_第1页
方格网程序及算法_第2页
方格网程序及算法_第3页
方格网程序及算法_第4页
方格网程序及算法_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、西 南 交 通 大 学 线 路 工 程 信 息 技 术 编 程 作 业部分代码:/Doc类,主要处理文件读写,以及运算; 25 void CDTMDoc:OnFileOpen() /响应函数,响应“打开”这一命令消息CMainFrame *pMainFrm = new CMainFrame;pMainFrm = (CMainFrame *)AfxGetApp()->m_pMainWnd;pMainFrm->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_OPEN,MF_BYPOSITION | MF_GRAYED);/以

2、下利用CFileDialog类创建一个打开文件对话框CFileDialog OpenFileDlg(TRUE, _T("DEM Files (*DEM)"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,_T("Digital Terrain Data Files (*.txt;*.DTD;*.dat)|*.txt;*.DTD;*.dat)|"),NULL);CString path;/DoModal()函数成功则返回IDOK或ID

3、CANCEL,其返回值对应于用来关闭对话框的控件的IDif(OpenFileDlg.DoModal() = IDOK )/点击对话框确定按钮得到路径v_OrigPoint.clear();XYLine.clear();poOnLine.clear();path = OpenFileDlg.GetPathName();/得到打开文件的路径elseAfxMessageBox(_T("Cancel!");return;ifstream file;setlocale(LC_CTYPE, ".936");/处理中文路径问题file.open(path);/setl

4、ocale(LC_CTYPE, " ");/成对使if (!file)AfxMessageBox(_T("Reading file failed! Maybe the file does not exist!");return;/Open file failed,return.OrigPoint *temp_OrigPoint = new OrigPoint;AfxMessageBox(_T("读取DTM数据时间可能较长,n请耐心等待!");OnStatusBar(_T("Reading.");char c;whi

5、le(TRUE)/ /利用Break跳出file>>temp_OrigPoint->Mark1; /第一列12,22for (int i = 0;i<4;i+) /ATTRfile>>c;file>>temp_OrigPoint->x;file>>temp_OrigPoint->y;file>>temp_OrigPoint->z;file>>temp_OrigPoint->Mark2; /最后一列if (file)v_OrigPoint.push_back(*temp_OrigPoint

6、);elsefile.close(); break;m_PointNUM=v_OrigPoint.size();if (m_PointNUM = 0)/不支持文件格式AfxMessageBox(_T("不支持文件格式!n打开失败!");return;FileIsOpen = TRUE;Analysis();pMainFrm->m_wndStatusBar.SetPaneInfo(1,ID_State_Doing,SBPS_NORMAL,150);/100为宽度可调整pMainFrm->m_wndStatusBar.SetPaneText(1,_T("W

7、aitting Location!"),true);/ TODO: Add your command handler code herevoid CDTMDoc:OnStatusBar(CString str)/状态栏进度CMainFrame *pMainFrm = new CMainFrame;pMainFrm = (CMainFrame *)AfxGetApp()->m_pMainWnd;CStatusBar* StatusBar = (CStatusBar*)pMainFrm->GetStatusBar();int index =StatusBar->Comm

8、andToIndex(ID_State_Doing);StatusBar->SetPaneInfo(index,ID_State_Doing,SBPS_NORMAL,100);/100为宽度可调整StatusBar->SetPaneText(index,str,true);BOOL CDTMDoc:Analysis()/analysis the regionm_left = v_OrigPoint.at(0).x;m_right = v_OrigPoint.at(0).x;m_bottom = v_OrigPoint.at(0).y;m_top = v_OrigPoint.at(0

9、).y;m_lowest = v_OrigPoint.at(0).z;m_highest = v_OrigPoint.at(0).z;for (int i = 0; i < m_PointNUM; i+)/分析边缘if (m_left > v_OrigPoint.at(i).x)m_left = v_OrigPoint.at(i).x;if (m_right < v_OrigPoint.at(i).x)m_right = v_OrigPoint.at(i).x;if (m_bottom > v_OrigPoint.at(i).y)m_bottom = v_OrigPoi

10、nt.at(i).y;if (m_top < v_OrigPoint.at(i).y)m_top = v_OrigPoint.at(i).y;if (m_lowest > v_OrigPoint.at(i).z)m_lowest = v_OrigPoint.at(i).z;if (m_highest < v_OrigPoint.at(i).z)m_highest = v_OrigPoint.at(i).z;Mesh();/划分网格hh = (m_highest - m_lowest)/10;CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGe

11、tApp()->m_pMainWnd;CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();CDTMView *pView = (CDTMView *) pChild->GetActiveView();pView->Invalidate();/更新viewreturn TRUE;bool comparex(const OrigPoint& lhs, const OrigPoint& rhs)if (lhs.x = rhs.x)return lhs.y < rhs.y;els

12、ereturn lhs.x < rhs.x;bool comparey(const OrigPoint& lhs, const OrigPoint&rhs)if (lhs.y = rhs.y)return lhs.x < rhs.x;elsereturn lhs.y < rhs.y;void CDTMDoc:Mesh()M = (m_right- m_left)/DTL+1;/设置网格N = (m_top - m_bottom)/DTL+1;if (M*N = 0)AfxMessageBox(_T("先读取数据");return;Point

13、inPane *PoinPa;/二维数组PoinPa= new PointinPane*M;for(int i=0; i<M; +i)PoinPai = new PointinPaneN;m_PointNUM = v_OrigPoint.size();int i = 0;int j = 0;for (int k=0;k<m_PointNUM;k+)i = int(v_OrigPoint.at(k).x-m_left)/DTL);j = int(v_OrigPoint.at(k).y-m_bottom)/DTL);PoinPaij.point.push_back(v_OrigPoin

14、t.at(k);Node= new Point3D*M+1;for(int i=0; i<M+1; +i)Nodei = new Point3DN+1;for (int i=0;i<M+1;i+)for (int j=0;j<N+1;j+)Nodeij.x = i*200+m_left;Nodeij.y = j*200+m_bottom;Nodeij.z = 0;for (int i=1;i<M;i+)for (int j=1;j<N;j+)double x = Nodeij.x;double y = Nodeij.y;double l1,l2,l3,l4;dou

15、ble p1,p2,p3,p4;double z1,z2,z3,z4;PointinPane V;V = PoinPai-1j-1;int pid1 = FindNearest(V,x,y);if (pid1 = -1)z1 = 0;p1 = 0;z1 = 0;elsel1 = (V.point.at(pid1).x-x)*(V.point.at(pid1).x-x)+(V.point.at(pid1).y-y)*(V.point.at(pid1).y-y);p1 = 1/l1;z1 = V.point.at(pid1).z;V = PoinPai-1j;int pid2 = FindNear

16、est(V,x,y);if (pid2 = -1)z2 = 0;p2 = 0;z2 = 0;elsel2 = (V.point.at(pid2).x-x)*(V.point.at(pid2).x-x)+(V.point.at(pid2).y-y)*(V.point.at(pid2).y-y);p2 = 1/l2;z2 = V.point.at(pid2).z;V = PoinPaij-1;int pid3 = FindNearest(V,x,y);if (pid3 =-1)z3 = 0;p3 = 0;z3 = 0;elsel3 = (V.point.at(pid3).x-x)*(V.point

17、.at(pid3).x-x)+(V.point.at(pid3).y-y)*(V.point.at(pid3).y-y);p3 = 1/l3;z3 = V.point.at(pid3).z;V = PoinPaij;int pid4 = FindNearest(V,x,y);if (pid4 = -1)z4 = 0;p4 = 0;z4 = 0;elsel4 = (V.point.at(pid4).x-x)*(V.point.at(pid4).x-x)+(V.point.at(pid4).y-y)*(V.point.at(pid4).y-y);p4 = 1/l4;z4 = V.point.at(

18、pid4).z;if (p1+p2+p3+p4 =0)/相邻的四个网格中没有找到点Nodeij.z = -200;elsedouble z = (z1*p1+z2*p2+z3*p3+z4*p4)/(p1+p2+p3+p4);Nodeij.z = z;int CDTMDoc:FindNearest(PointinPane V,double x,double y)double mm=200*200*2;double l=0;int pid=-1;if (V.point.size()=0)return -1;for (int i=0;i<V.point.size();i+)l=(V.point

19、.at(i).x-x)*(V.point.at(i).x-x)+(V.point.at(i).y-y)*(V.point.at(i).y-y);if (l<mm)mm = l;pid = i;return pid;void CDTMDoc:OnProfileLongitudinal()CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();CDTMView *pView = (CD

20、TMView *) pChild->GetActiveView();/pView->m_locationState = FALSE;/关闭定线功能poOnLine.clear();if (XYLine.size()>1)double SBase = 0;for (int i=1;i<XYLine.size();i+)double xx = XYLine.at(i).x-XYLine.at(i-1).x;double yy = XYLine.at(i).y-XYLine.at(i-1).y;double x0 = XYLine.at(i-1).x;double y0 =

21、XYLine.at(i-1).y;double ss =sqrt(xx*xx+yy*yy);PointOnLine point;for (int j=0;j*div<ss;j+)point.x = x0 + (j*div/ss)*xx;point.y = y0 + (j*div/ss)*yy;point.s =SBase+j*div;poOnLine.push_back(point);point.x = XYLine.at(i).x;point.y = XYLine.at(i).y;point.s = SBase+ss;poOnLine.push_back(point);SBase +=

22、ss;elsereturn;/内插int J;int I;lHeighest = m_lowest-5000;lLowest = m_highest+5000;for (int i = 0;i<poOnLine.size();i+)J = (poOnLine.at(i).x)/DTL;I = (poOnLine.at(i).y)/DTL;if (J<1|I<1|J>M-1|I>N-1)poOnLine.at(i).z = -200;continue;double x = poOnLine.at(i).x-DTL*J;double y = poOnLine.at(i

23、).y-DTL*I;double z =NodeJI.z*(1-x/DTL)*(1-y/DTL)+NodeJ+1I.z*(x/DTL)*(1-y/DTL)+NodeJ+1I+1.z*(x/DTL)*(y/DTL)+NodeJI+1.z*(1-x/DTL)*(y/DTL);poOnLine.at(i).z = z;if (z>lHeighest)lHeighest = z;if (z<lLowest)lLowest = z;pView_LS->Invalidate();/ TODO: Add your command handler code herevoid CDTMDoc:

24、OnUpdateProfileLongitudinal(CCmdUI *pCmdUI)if (XYLine.size()>1)pCmdUI->Enable(TRUE);elsepCmdUI->Enable(FALSE);/ TODO: Add your command update UI handler code here/View类主要负责图像显示、响应鼠标消息等void CDTMView:OnDraw(CDC* pDC)CDTMDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;OldPdc = pD

25、C;GetClientRect(clientRect);/得到窗口大小CBrush brush(RGB(0, 0, 0);pDC->SelectObject(brush);windowx =pDoc->m_right-pDoc->m_left;/逻辑坐标宽度范围windowy =pDoc->m_top-pDoc->m_bottom;if (scal <=1 )scal = 1;trueRect = CRect(0,0,scal*clientRect.Width(),scal*clientRect.Height();pDC->FillSolidRect(

26、trueRect,RGB(0,0,0);if (windowx = 0|windowy = 0)windowx = clientRect.right;windowy = clientRect.bottom;elsesizeTotal.cx = clientRect.Width()*scal;sizeTotal.cy = clientRect.Height()*scal; SetScrollSizes(MM_TEXT, sizeTotal); /调节滚动条trueScale = max(windowx/(scal*clientRect.Width(),windowy/(scal*clientRe

27、ct.Height();LOGFONT lf;memset( &lf, 0, sizeof( LOGFONT ) );pDC->GetCurrentFont()->GetLogFont(&lf);/获取系统当前字体CFont font1;lf.lfCharSet=GB2312_CHARSET;/定义字符lf.lfHeight = MyFontHeight*trueScale*0.8;lf.lfWidth=0;lstrcpy(lf.lfFaceName,Myscript);font1.CreateFontIndirect(&lf);pDC->Select

28、Object(&font1);pDC->SetBkMode(TRANSPARENT);/设置字体输出背景为透明pDC->SetTextColor(RGB(0,255,255); /设定文本颜色/设置映射关系CPoint pos=GetScrollPosition();pDC->SetMapMode(MM_ISOTROPIC);pDC->SetWindowExt(windowx,windowy);pDC->SetViewportExt(trueRect.Width()-40,-trueRect.Height()+40);pDC->SetViewport

29、Org(0-pos.x+20,trueRect.Height()-pos.y-20);CString strelev;/用于高程显示字符串;offx = int(pDoc->m_left);offy = int(pDoc->m_bottom)+1;for (int i = 0; pDoc->m_PointNUM!=0&&i<pDoc->m_PointNUM;i+)int pennum = int(pDoc->v_OrigPoint.at(i).z-pDoc->m_lowest+0.5*pDoc->hh)/(pDoc->hh)

30、;/计算画笔颜色pDC->SelectObject(&colorPenpennum);if (pDoc->v_OrigPoint.at(i).Mark2 = 31)pDC->MoveTo(pDoc->v_OrigPoint.at(i).x-offx,pDoc->v_OrigPoint.at(i).y-offy);strelev.Format(_T("%.2lf"),pDoc->v_OrigPoint.at(i).z);if (pDoc->v_OrigPoint.at(i).Mark2 = 32)if (i>1&

31、&(pDoc->v_OrigPoint.at(i-1).Mark2 =33)pDC->MoveTo(pDoc->v_OrigPoint.at(i).x-offx,pDoc->v_OrigPoint.at(i).y-offy);elsepDC->LineTo(pDoc->v_OrigPoint.at(i).x-offx,pDoc->v_OrigPoint.at(i).y-offy);if (IsShowHeight = TRUE&&pDoc->v_OrigPoint.at(i).Mark1 = 12&&i%2

32、0 = 0)pDC->TextOut(pDoc->v_OrigPoint.at(i).x-offx,pDoc->v_OrigPoint.at(i).y-offy,strelev);if (pDoc->v_OrigPoint.at(i).Mark2 = 33)/pDC->LineTo(pDoc->v_OrigPoint.at(i).x-offx,pDoc->v_OrigPoint.at(i).y-offy); if (IsGrid = TRUE)/是否绘制网格DrawGrid(pDC);CPen redPen;redPen.CreatePen(PS_SO

33、LID,5,RGB(255,0,0);pDC->SelectObject(redPen);CString str;for (int i=0;i<pdocument->XYLine.size();i+)if (i = 0)pDC->MoveTo(pdocument->XYLine.at(0);pDC->SetTextColor(RGB(255,255,0);pDC->TextOut(pdocument->XYLine.at(0).x,pdocument->XYLine.at(0).y,_T("起点");else if(i&

34、gt;=2)str.Format(_T("JD %d"),i-1);pDC->LineTo(pdocument->XYLine.at(i);pDC->TextOut(pdocument->XYLine.at(i-1).x,pdocument->XYLine.at(i-1).y,str);elsepDC->LineTo(pdocument->XYLine.at(i);int Index = pdocument->XYLine.size()-1;if (Index>0&&m_locationState = T

35、RUE)int nOldMode=pDC->GetROP2();pDC->SetROP2(R2_NOT);pDC->MoveTo(pdocument->XYLine.at(Index).x,pdocument->XYLine.at(Index).y);pDC->LineTo(OldPoint.x,OldPoint.y);pDC->SetROP2(nOldMode);oldscal = scal;if (m_locationState=FALSE&&Index>0)pDC->TextOut(pdocument->XYLi

36、ne.at(Index).x,pdocument->XYLine.at(Index).y,_T("终点");/ TODO: add draw code for native data here/ CDTMView printingBOOL CDTMView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CDTMView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ T

37、ODO: add extra initialization before printingvoid CDTMView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CDTMView diagnostics/ CDTMView message handlersBOOL CDTMView:OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)/ TODO: Add your message handler code here a

38、nd/or call defaultint Index = pdocument->XYLine.size()-1;if (Index>=0)CDC *pDC=GetDC();CPoint pos=GetScrollPosition();pDC->SetMapMode(MM_ISOTROPIC);pDC->SetWindowExt(windowx,windowy);pDC->SetViewportExt(trueRect.Width()-40,-trueRect.Height()+40);pDC->SetViewportOrg(0-pos.x+20,trueR

39、ect.Height()-pos.y-20);int nOldMode=pDC->GetROP2();pDC->SetROP2(R2_NOT);pDC->MoveTo(pdocument->XYLine.at(Index).x,pdocument->XYLine.at(Index).y);pDC->LineTo(OldPoint.x,OldPoint.y);pDC->MoveTo(pdocument->XYLine.at(Index).x,pdocument->XYLine.at(Index).y);CPoint point1 = pt;p

40、DC->DPtoLP(&point1);pDC->LineTo(point1.x,point1.y);OldPoint = point1;pDC->SetROP2(nOldMode);oldscal = scal;scal += zDelta/500.00;Invalidate();/return CScrollView:OnMouseWheel(nFlags, zDelta, pt);return TRUE;BOOL CDTMView:OnEraseBkgnd(CDC* pDC)/ TODO: Add your message handler code here a

41、nd/or call defaultreturn TRUE;/return CScrollView:OnEraseBkgnd(pDC);void CDTMView:OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)/ TODO: Add your message handler code here and/or call defaultInvalidate();CScrollView:OnVScroll(nSBCode, nPos, pScrollBar);void CDTMView:OnSize(UINT nType, int

42、 cx, int cy)Invalidate();CScrollView:OnSize(nType, cx, cy);/ TODO: Add your message handler code herevoid CDTMView:OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)/ TODO: Add your message handler code here and/or call defaultInvalidate();CScrollView:OnHScroll(nSBCode, nPos, pScrollBar);voi

43、d CDTMView:DrawGrid(CDC *pDC)pDC->SetBkMode(TRANSPARENT);/设置字体输出背景为透明pDC->SetTextColor(RGB(0,255,255); /设定文本颜色LOGFONT lf;memset( &lf, 0, sizeof( LOGFONT ) );pDC->GetCurrentFont()->GetLogFont(&lf);/获取系统当前字体CFont font1;lf.lfCharSet=GB2312_CHARSET;/定义字符lf.lfHeight = MyFontHeight*tru

44、eScale;lf.lfWidth=0;lstrcpy(lf.lfFaceName,Myscript);font1.CreateFontIndirect(&lf);pDC->SelectObject(&font1);CDTMDoc* pDoc = GetDocument();CPen pen;pen.CreatePen(PS_DOT,0,RGB(0,60,60);pDC->SelectObject(pen);int m = pDoc->M;int n = pDoc->N;double dtl = pDoc->DTL;double x0 = 0;do

45、uble y0 = 0;double xSpan = pDoc->m_right-pDoc->m_left;double ySpan = pDoc->m_top-pDoc->m_bottom;for (int i=0;i<=n;i+)pDC->MoveTo(x0,y0+i*dtl);pDC->LineTo(x0+dtl*m,y0+i*dtl);for (int i=0;i<=m;i+)pDC->MoveTo(x0+i*dtl,y0);pDC->LineTo(x0+i*dtl,y0+dtl*n);for (int i=1;i<m;

46、i+ )for (int j=1;j<n;j+)double xx = i*dtl;double yy = j*dtl;if (pDoc->Nodeij.z !=-200)CString str;str.Format(_T("%.2lf"),pDoc->Nodeij.z);pDC->TextOut(xx,yy,str);void CDTMView:OnLinelocation()CMainFrame *pMainFrm = (CMainFrame *)AfxGetApp()->m_pMainWnd;pMainFrm->m_wndStatu

47、sBar.SetPaneInfo(1,ID_State_Doing,SBPS_NORMAL,80);/100为宽度可调整CString str;if (m_locationState = FALSE)m_locationState = TRUE;str.Format(_T("Locating.");pMainFrm->m_wndStatusBar.SetPaneText(1,str,true);elsem_locationState = FALSE;str.Format(_T("Waitting Location!");pMainFrm->m

48、_wndStatusBar.SetPaneText(1,str,true);int Index = pdocument->XYLine.size()-1;if (Index>=0)OldPoint = pdocument->XYLine.at(Index);Invalidate();/ TODO: Add your command handler code herevoid CDTMView:OnMButtonDown(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or cal

49、l defaultCScrollView:OnMButtonDown(nFlags, point);void CDTMView:OnLButtonDown(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultif (m_locationState = TRUE)CDC *pDC=GetDC();CPoint pointtemp = point;CPoint pos=GetScrollPosition();pDC->SetMapMode(MM_ISOTROPIC);p

50、DC->SetWindowExt(windowx,windowy);pDC->SetViewportExt(trueRect.Width()-40,-trueRect.Height()+40);pDC->SetViewportOrg(0-pos.x+20,trueRect.Height()-pos.y-20);CPen redPen;redPen.CreatePen(PS_SOLID,5,RGB(255,0,0);pDC->SelectObject(redPen);int m =pdocument->M;int n =pdocument->N;double

51、dtl =pdocument->DTL;pDC->DPtoLP(&pointtemp);if ( pointtemp.x>(m-1)*dtl|pointtemp.x<dtl|pointtemp.y>(n-1)*dtl|pointtemp.y<dtl)AfxMessageBox(_T("所选择点超界!n请重新选择!");return;elsepdocument->XYLine.push_back(pointtemp);OldPoint = pointtemp;for (int i=0;i<pdocument->XY

52、Line.size();i+)LOGFONT lf;memset( &lf, 0, sizeof( LOGFONT ) );pDC->GetCurrentFont()->GetLogFont(&lf);/获取系统当前字体CFont font1;lf.lfCharSet=GB2312_CHARSET;/定义字符lf.lfHeight = MyFontHeight*trueScale*0.8;lf.lfWidth=0;lstrcpy(lf.lfFaceName,Myscript);font1.CreateFontIndirect(&lf);pDC->SetBkMode(TRANSPARENT);/设置字体输出背景为透明pDC->SetTextColor(RGB(255,255,0); /设定文本颜色pDC->SelectObject(&font1);CString str;if (i = 0)pDC->MoveTo(pdocument->XYLine.at(0);pDC->TextOut(pdocument->XYLine.at(0).x,pdocument->XYLine.at(0).y,_T("起点");else if(i>=2)str.Format(_T(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论