📄 shortpathview.cpp
字号:
// ShortPathView.cpp : implementation of the CShortPathView class
//
#include "stdafx.h"
#include "ShortPath.h"
#include "ShortPathDoc.h"
#include "ShortPathView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*我的预定义*/
#include "ShortestPath.h"
#include <math.h>
#define pi 3.141592653
#define L 26//箭头的长短
#define X 0
#define Y 0
#define TEXTR 6
#define R 15
COLORREF linecol = RGB(120,187,32);
COLORREF mapcol = RGB(120,151,218);
bool rectif = false;
char from = '0';
char end = '0';
//ShortestPath shortpath;
//shortpath.floyd();
/*预定义结束*/
/////////////////////////////////////////////////////////////////////////////
// CShortPathView
IMPLEMENT_DYNCREATE(CShortPathView, CView)
BEGIN_MESSAGE_MAP(CShortPathView, CView)
//{{AFX_MSG_MAP(CShortPathView)
ON_COMMAND(ID_CHOOSE, OnChoose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShortPathView construction/destruction
CShortPathView::CShortPathView()
{
// TODO: add construction code here
}
CShortPathView::~CShortPathView()
{
}
BOOL CShortPathView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CShortPathView drawing
void CShortPathView::OnDraw(CDC* pDC)
{
CShortPathDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
/*我的代码*/
CRect rect;
GetClientRect(&rect);
string sstring;
if(rectif == false)
{
rectif = true;
vpos[0].x = 5*rect.right/34;
vpos[0].y = 5*rect.bottom/22;
vpos[0].data = '0';
vpos[1].x = 17*rect.right/34;
vpos[1].y = 5*rect.bottom/22;
vpos[1].data = '1';
vpos[2].x = 29*rect.right/34;
vpos[2].y = 5*rect.bottom/22;
vpos[2].data = '2';
vpos[3].x = 11*rect.right/34;
vpos[3].y = 11*rect.bottom/22;
vpos[3].data = '3';
vpos[4].x = 23*rect.right/34;
vpos[4].y = 11*rect.bottom/22;
vpos[4].data = '4';
vpos[5].x = 5*rect.right/34;
vpos[5].y = 17*rect.bottom/22;
vpos[5].data = '5';
vpos[6].x = 17*rect.right/34;
vpos[6].y = 17*rect.bottom/22;
vpos[6].data = '6';
vpos[7].x = 29*rect.right/34;
vpos[7].y = 17*rect.bottom/22;
vpos[7].data = '7';
shortpath.floyd();
}
drawmap(pDC);
int count =shortpath.get_shortpath(from,end,sstring);
if(count)
{
for(int i = 0;i<count-1;)
{
CPoint fromxy,endxy;
int ffrom = sstring[i++] - '0';
int eend = sstring[i] - '0';
fromxy.x = vpos[ffrom].x;
fromxy.y = vpos[ffrom].y;
endxy.x = vpos[eend].x;
endxy.y = vpos[eend].y;
drawline(pDC,fromxy,endxy,linecol);
}
}
/*完毕*/
}
/////////////////////////////////////////////////////////////////////////////
// CShortPathView diagnostics
#ifdef _DEBUG
void CShortPathView::AssertValid() const
{
CView::AssertValid();
}
void CShortPathView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CShortPathDoc* CShortPathView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShortPathDoc)));
return (CShortPathDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CShortPathView message handlers
void CShortPathView::drawline(CDC* hdc,CPoint from,CPoint end,COLORREF col)
{
CPen mpen;
CPen* hOldpen;
int linewide = 3;
mpen.CreatePen(PS_SOLID,linewide,col);
hOldpen = hdc->SelectObject(&mpen);
hdc->MoveTo(from.x + X,from.y + Y);
hdc->LineTo(end.x + X,end.y + Y);
//double k ;
CPoint down,up;
double x1,y1,x2,y2;
double a = 1.0*pi/6;
if((end.x != from.x))//&&(end.y != from.y)
{
double k = abs((end.y - from.y)/(end.x - from.x));
double k0 = tan(a);
double k1 = (k - k0)/(1 + k*k0);
double k2 = (k + k0)/(1 - k*k0);
if(from.x < end.x)
{
x1 = end.x - sqrt(1.0*L*L/(k1*k1+1));
y1 = end.y - k1*(end.x - x1);
x2 = end.x - sqrt(1.0*L*L/(k2*k2+1));
y2 = end.y - k2*(end.x - x2);
}
else if(from.x > end.x)
{
x1 = end.x + sqrt(1.0*L*L/(k1*k1+1));
y1 = end.y - k1*(end.x - x1);
x2 = end.x + sqrt(1.0*L*L/(k2*k2+1));
y2 = end.y - k2*(end.x - x2);
}
/*double lk = abs((end.x - from.x)/(end.y - from.y));
double k2 = (lk - k0)/(1 + lk*k0);
if(end.y < from.y)
{
y2 = end.y - sqrt(1.0*L*L/(1+k2*k2));
x2 = end.x + k2 * (y2 - end.y);
}
else if(end.y > from.y)
{
y2 = end.y + sqrt(1.0*L*L/(1+k2*k2));
x2 = end.x + k2 * (y2 - end.y);
}*/
}
if(end.x == from.x)// && end.y != from.y
{
if(end.y < from.y)
{
y1 = y2 = end.y + L * cos(a);
}
else
{
y1 = y2 = end.y - L * cos(a);
}
x1 = end.x - L * sin(a);
x2 = end.x + L * sin(a);
}
/*if(end.y == from.y)
{
if(end.x < from.x)
{
x1 = x2 = end.y + L * cos(a);
}
else
{
x1 = x2 = end.y - L * cos(a);
}
y1 = end.x - L * sin(a);
y2 = end.x + L * sin(a);
}*/
down.x = x1;
down.y = y1;
up.x = x2;
up.y = y2;
/*if((end.x - from.x) != 0)
k = atan((end.y - from.y)/(end.x - from.x));//斜率
else
k = pi/2;
double t = pi/6;//箭头相对直线的偏移角度
double Rdown = pi/2 - k - t;//上面的直线的偏移角度
double Rup = k - t;//下面的直线的偏移角度
CPoint down;
CPoint up;
down.x = end.x - L * sin((int)Rdown);
down.y = end.y - L * cos((int)Rdown);
up.x = end.x - L * cos((int)Rup);
up.y = end.y - L * sin((int)Rup);*/
hdc->MoveTo(end.x + X,end.y + Y);
hdc->LineTo(up.x + X,up.y + Y);
hdc->MoveTo(end.x + X,end.y + Y);
hdc->LineTo(down.x + X,down.y + Y);
hdc->SelectObject(hOldpen);
mpen.DeleteObject();
}
void CShortPathView::drawmap(CDC* hdc)
{
CPoint from,end;//画节点之间的有向线段
char weight[5];
int textx,texty;
for(int i = 0;i < N ; i++)
for(int j = 0; j < N;j++)
{
if(shortpath.arcs[i][j] != MAX)
{
from.x = vpos[i].x;
from.y = vpos[i].y;
end.x = vpos[j].x;
end.y = vpos[j].y;
drawline(hdc,from,end,mapcol);
textx = (from.x + end.x)/2;
texty = (from.y + end.y)/2;
wsprintf(weight,"%d", shortpath.arcs[i][j]);
hdc->TextOut(textx,texty,weight,strlen(weight));
}
}
CBrush eiBrush;//画节点
CBrush* hOldbrush;
eiBrush.CreateSolidBrush(mapcol);
hOldbrush = hdc->SelectObject(&eiBrush);
CPen* hOldpen;
hOldpen = (CPen*)hdc->SelectStockObject(NULL_PEN);
CPoint point;
for(int k = 0 ;k < N;k++)
{
CRect rrect;
CString data(vpos[k].data);
rrect.left = X + vpos[k].x - R;
rrect.right = X + vpos[k].x + R;
rrect.top = Y + vpos[k].y - R;
rrect.bottom = Y + vpos[k].y + R;
hdc->Ellipse(rrect);
CPoint textpos;
textpos.x = X + vpos[k].x - TEXTR;
textpos.y = Y + vpos[k].y - TEXTR;
hdc->TextOut(textpos.x,textpos.y,data,1*sizeof(char));
}
}
/*定义结束*/
void CShortPathView::OnChoose()
{
CChoose dlg;
dlg.m_from = '0';
dlg.m_end = '0';
if(dlg.DoModal() == IDOK)
{
from = dlg.m_from[0];
end = dlg.m_end[0];
Invalidate(false);
}
else
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -