1、用 MFC 作 VRP 配送路线图以下示例代码是车辆调度路线图形表示,用 MFC 实现。说明:画 VRP 路线图:数字 0 代表配送中心,其它非 0 数字代表各个客户,圆点代表客户所需客户量,圆点越大,表示该客户所需货物越多,反之,越少;实线代表车辆非空载配送线路,虚线代表车辆本次为若干个客户配送货物完毕,空车返回配送中心void CvrpView:DrawCordTrend()CWnd* pWnd;POINT ps2;CString strFlag,str;CRect rect,rectempty;CDC* pDC;CPen pen,*pOldPen,pen2,penEmpty;CBrush
2、 brush,*pOldBrush;POINT ptOrig;COLORREF crRef;int i,iXNum,iYNum,iXStep,iYStep;long iXCenter,iYCenter,X,Y,xsize,ysize;double dAltiMax,dAltiMin,dLongMax,dLongMin,dLatiMax,dLatiMin;pWnd = (CStatic*)GetDlgItem( IDC_STATIC_TREND );pWnd-Invalidate( );pWnd-GetWindowRect(rect.left += 6;/18rect.top += 6; /15
3、rect.right -= 6;/10rect.bottom -= 6; /10pWnd-ScreenToClient(ptOrig.x = rect.left;ptOrig.y = ( rect.bottom + rect.top ) / 2 ;iXCenter = ( rect.left + rect.right ) / 2 ;iYCenter = ( rect.bottom + rect.top ) / 2 ;/iYCenter = rect.bottom;m_strTest.Format(“X=%d,Y=%d“,iXCenter,iYCenter);pDC = pWnd-GetDC()
4、;/背景透明pDC-SetBkMode( TRANSPARENT );/通用笔,灰白色/pen2.CreatePen(PS_SOLID,1,RGB(128,128,128) );pen2.CreatePen(PS_SOLID,1,RGB(0,0,255) );/pen.CreatePen(PS_SOLID,1,RGB(0,126,0) );/蓝色/画空载返回的笔penEmpty.CreatePen(PS_DOT,1,RGB(0,0,0) );/1 点宽pOldPen = pDC-SelectObject(pDC-SelectObject(iXNum = 40;/40,20iYNum = 40;
5、/40,15,40iXStep = ( rect.right - rect.left ) / (2*iXNum);iYStep = ( rect.bottom - rect.top ) / (2*iYNum);/画所有点m_clientrelcord00=iXCenter;m_clientrelcord01=iYCenter;xsize=ysize=0;/CBrush b(RGB(0,0,255);/蓝色CBrush b(RGB(255,0,0);/红色CBrush *pOldbBrush = pDC-SelectObject(for(i=0;i=1)xsize = m_clientreqi
6、/ 4;ysize = m_clientreqi / 4;X=m_clientcordi0*iXStep;Y=-m_clientcordi1*iYStep;X+=iXCenter;Y+=iYCenter;pDC-MoveTo(X,Y);if(i=0)m_strTest=“0“;/pDC-TextOut(X-4,Y+3,m_strTest);pDC-TextOut(X,Y,m_strTest);elsem_strTest.Format(“%d“,i);if(i=1)pDC-TextOut(X-xsize-12,Y-4,m_strTest);elseif(i=7 | i=11 | i=8)pDC-
7、TextOut(X+xsize+2,Y-4,m_strTest);else if(i=14)pDC-TextOut(X-10,Y-ysize-16,m_strTest);else if(i=4)pDC-TextOut(X-16,Y-ysize,m_strTest);elsepDC-TextOut(X-xsize,Y+ysize,m_strTest);pDC-MoveTo(X,Y);m_clientrelcordi0=X;m_clientrelcordi1=Y;/改为根据客户所需货物大小画圆形pDC-Ellipse(X-3,Y-3,X+3,Y+3);if(i)pDC-Ellipse(X-3-xs
8、ize,Y-3-ysize,X+3+xsize,Y+3+ysize);elsepDC-Ellipse(X-3,Y-3,X+3,Y+3);pDC-SelectObject(pOldbBrush);/画路线图float fload,fcurrcost =0;int j=0;int iSubIndex=0;BOOL bCenter = TRUE;fload=0;chromo chro=m_chromo0;/m_chromo0是最好结果,m_chromo1是次好结果,依次类推chro.fCost = 0;/染色体(路线图)格式:0 123 0 456 0 789 0 1011 0 1213 0for(
9、iSubIndex = 0; iSubIndex SelectObject(X=m_clientrelcordchro.nRoutej-10;Y=m_clientrelcordchro.nRoutej-11;if(chro.nRoutej-2=0)/只有一个节点ps0.x=X;ps0.y=Y;ps1.x=iXCenter;ps1.y=iYCenter;pDC-MoveTo(X+3 , Y-1);/pDC-Chord(/pDC-PolyBezier(ps,2);/pDC-Ellipse(X,Y,iXCenter,iYCenter);pDC-LineTo(iXCenter , iYCenter);
10、elsepDC-LineTo(iXCenter , iYCenter);/空车返回时, 增画箭头/pDC-MoveTo( iXCenter , iYCenter );/pDC-LineTo(iXCenter-3 , iYCenter-3);j+;break;/fcurrcost *= m_distance0m_chromoiIndex.nRoutej;else /中间客户if(chro.nRoutej-1 = 0) /从中心出来的第一个节点pDC-SelectObject(pDC-MoveTo( iXCenter , iYCenter );pDC-LineTo(m_clientrelcordc
11、hro.nRoutej0,m_clientrelcordchro.nRoutej1);else /非第一个节点fload -= m_clientreqchro.nRoutej-1;fcurrcost += fload*m_distancechro.nRoutej-1chro.nRoutej;pDC-SelectObject(X=m_clientrelcordchro.nRoutej-10;Y=m_clientrelcordchro.nRoutej-11;pDC-MoveTo( X , Y );pDC-LineTo(m_clientrelcordchro.nRoutej0,m_clientrelcordchro.nRoutej1);j+;/end of: while(true)/end of:for(iSubIndex = 0; iSubIndex SelectObject( pOldPen );pDC-SelectObject( pOldbBrush );ReleaseDC( pDC );pDC = NULL;运行结果示意图: