📄 bresenhamview.cpp
字号:
// BresenhamView.cpp : implementation of the CBresenhamView class
//
#include "stdafx.h"
#include "Bresenham.h"
#include "math.h"
#include "BresenhamDoc.h"
#include "BresenhamView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBresenhamView
IMPLEMENT_DYNCREATE(CBresenhamView, CView)
BEGIN_MESSAGE_MAP(CBresenhamView, CView)
//{{AFX_MSG_MAP(CBresenhamView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBresenhamView construction/destruction
CBresenhamView::CBresenhamView()
{
// TODO: add construction code here
}
CBresenhamView::~CBresenhamView()
{
}
BOOL CBresenhamView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CBresenhamView drawing
void CBresenhamView::OnDraw(CDC* pDC)
{
pDC->TextOut(0,0,"Press the left mouse button please!!!");
int Dx=abs(EndPoint.x-StartPoint.x),Dy=abs(EndPoint.y-StartPoint.y);
int p= 2*Dy-Dx;
int p0= 2*Dx-Dy;
int AddDy = 2*Dy, AddDyMinusDx =2*(Dy-Dx);
int AddDx = 2*Dx, AddDxMinusDy =2*(Dx-Dy);
CPoint Point;
//斜率
double k;
k = ((EndPoint.y -StartPoint.y)*0.1)/((EndPoint.x -StartPoint.x)*0.1);
//当 斜率大于0小于1.0的时候。
if(k<1.0 &&k >=0)
{
if(StartPoint.x>EndPoint.x)
{
Point=EndPoint;
EndPoint=StartPoint;
}
else
Point=StartPoint;
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
while(Point.x<EndPoint.x)
{
Point.x++;
if(p<0)
p+=AddDy;
else if(p>=0)
{
Point.y++;
p+=AddDyMinusDx;
}
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
}
}
//斜率大于1.0
else if(k>=1.0)
{
if(StartPoint.x>EndPoint.x)
{
Point=EndPoint;
EndPoint=StartPoint;
}
else
Point=StartPoint;
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
while(Point.y<EndPoint.y)
{
Point.y++;
if(p0<0)
p0+=AddDx;
else
{
Point.x++;
p0+=AddDxMinusDy;
}
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
}
}
//斜率在0到-1.0之间
else if(k>-1.0&&k<0)
{
if(StartPoint.x>EndPoint.x)
{
Point=EndPoint;
EndPoint=StartPoint;
StartPoint=Point;
}
else
{
Point=StartPoint;
}
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
while(Point.x<EndPoint.x)
{
Point.x++;
if(p<0)
p+=AddDy;
else if(p>=0)
{
Point.y--;
p+=AddDyMinusDx;
}
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
}
}
//斜率小于-1.0
else if(k<-1.0)
{
if(StartPoint.x>EndPoint.x)
{
Point=EndPoint;
EndPoint=StartPoint;
}
else
{
Point=StartPoint;
}
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
while(Point.y>EndPoint.y)
{
Point.y--;
if(p0<0)
p0+=AddDx;
else
{
Point.x++;
p0+=AddDxMinusDy;
}
pDC->SetPixel(Point.x,Point.y,RGB(255,0,0));
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CBresenhamView diagnostics
#ifdef _DEBUG
void CBresenhamView::AssertValid() const
{
CView::AssertValid();
}
void CBresenhamView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CBresenhamDoc* CBresenhamView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBresenhamDoc)));
return (CBresenhamDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBresenhamView message handlers
void CBresenhamView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
CDC *pDC=GetDC(); //CDC类是专门用于画图
static int m=1;
if(m%2==0)
{
EndPoint=point;
str2.Format("%d %d",EndPoint.x,EndPoint.y);
pDC->SetPixel(EndPoint,RGB(255,0,0));
OnDraw(pDC);
//DrawLines();
}
else
{
StartPoint = point;
Str1.Format("%d %d",StartPoint.x,StartPoint.y);
pDC->SetPixel(StartPoint,RGB(255,0,0));
}
m++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -