?? zywview.cpp
字號:
// zywView.cpp : implementation of the CZywView class
//
#include "stdafx.h"
#include "zyw.h"
#include "zywDoc.h"
#include "zywView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZywView
IMPLEMENT_DYNCREATE(CZywView, CView)
BEGIN_MESSAGE_MAP(CZywView, CView)
//{{AFX_MSG_MAP(CZywView)
ON_COMMAND(ID_DDALINE, OnDdaline)
ON_COMMAND(ID_BRESENHAMLINE, OnBresenhamline)
ON_COMMAND(ID_MIDPOINTLINE, OnMidpointline)
ON_WM_SIZE()
ON_COMMAND(ID_CIRCLEB, OnCircleb)
ON_COMMAND(ID_CIRCLEANY, OnCircleany)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CZywView construction/destruction
CZywView::CZywView()
{
// TODO: add construction code here
}
CZywView::~CZywView()
{
}
BOOL CZywView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CZywView drawing
void CZywView::OnDraw(CDC* pDC)
{
CZywDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
char sizeStr[64];
int i;
CString str;
sprintf(sizeStr, "(%d,%d)", m_winSizeX, m_winSizeY);
pDC->TextOut(m_winSizeX/2, m_winSizeY/2, sizeStr);
pDC->MoveTo(m_winSizeX/2, 0);
pDC->LineTo(m_winSizeX/2, m_winSizeY);
pDC->MoveTo(0, m_winSizeY/2);
pDC->LineTo(m_winSizeX, m_winSizeY/2);
// m_disPlay=-1;
switch(m_disPlay)
{
case 0:
begin = GetTickCount();
for(i=0;i<100;i++)
// sleep();
DDAline(0, 0, 140, 50, pDC);
DDAline(0, 0, 50, 140, pDC);
DDAline(0, 0, -140, 50, pDC);
DDAline(0, 0, -50, 140, pDC);
DDAline(0, 0, 140, -50, pDC);
DDAline(0, 0, 50, -140, pDC);
DDAline(0, 0, -140, -50, pDC);
DDAline(0, 0, -50, -140, pDC);
end = GetTickCount();
timeUse1 = end - begin;
str.Format("Time: % d ms",timeUse1);
AfxMessageBox(str);
break;
case 1:
begin = GetTickCount();
for(i=0;i<100;i++)
Bline(0, 0, 140, 50, pDC);
Bline(0, 0, 50, 140, pDC);
Bline(0, 0, -140, 50, pDC);
Bline(0, 0, -50, 140, pDC);
Bline(0, 0, 140, -50, pDC);
Bline(0, 0, 50, -140, pDC);
Bline(0, 0, -140, -50, pDC);
Bline(0, 0, -50, -140, pDC);
end = GetTickCount();
timeUse2 = end - begin;
str.Format("Time: % d ms",timeUse2);
AfxMessageBox(str);
break;
case 2:
begin = GetTickCount();
for(i=0;i<100;i++)
MidPointline(0, 0, 140, 50, pDC);
MidPointline(0, 0, -140, 50, pDC);
MidPointline(0, 0, 140, -50, pDC);
MidPointline(0, 0, -140, -50, pDC);
MidPointline(0, 0, 50, 140, pDC);
MidPointline(0, 0, 50, -140, pDC);
MidPointline(0, 0, -50, 140, pDC);
MidPointline(0, 0, -50, -140, pDC);
end = GetTickCount();
timeUse3 = end - begin;
str.Format("Time: % d ms",timeUse3);
AfxMessageBox(str);
break;
case 3:
BresenhamCircle(100,pDC);
break;
case 4:
BresenhamCircleAny(60,60,90,pDC);
BresenhamCircleAny(80,80,120,pDC);
BresenhamCircleAny(100,100,150,pDC);
BresenhamCircleAny(120,120,180,pDC);
BresenhamCircleAny(140,140,210,pDC);
BresenhamCircleAny(160,160,240,pDC);
BresenhamCircleAny(180,180,270,pDC);
BresenhamCircleAny(200,200,300,pDC);
BresenhamCircleAny(220,220,330,pDC);
break;
default:
break;
}
}
/////////////////////////////////////////////////////////////////////////////
// CZywView printing
BOOL CZywView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CZywView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CZywView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CZywView diagnostics
#ifdef _DEBUG
void CZywView::AssertValid() const
{
CView::AssertValid();
}
void CZywView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CZywDoc* CZywView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CZywDoc)));
return (CZywDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CZywView message handlers
void CZywView::DDAline(int x1, int y1, int x2, int y2, CDC* pDC)
{
int i,steps;
float x,y,dx,dy;
CString str;
steps = abs(x2-x1) > abs(y2-y1) ? abs(x2-x1) : abs(y2-y1);
dx = (float)(x2-x1) / steps;
dy = (float)(y2-y1) / steps;
x = x1 + 0.5;
y = y1 + 0.5;
for (i = 0; i <= steps; i++) {
pDC->SetPixel(m_winSizeX/2+x, m_winSizeY/2-y, 500);
x += dx; y += dy;
}
}
void CZywView::Bline(int x1, int y1, int x2, int y2, CDC* pDC)
{
int i,t1,t2,interchange;
float x,y,dx,dy,f,temp;
x = x1;
y = y1;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
if(x2-x1>=0) t1 = 1;
else t1 = -1;
if(y2-y1>=0) t2 = 1;
else t2=-1;
if(dy>dx){
temp = dx;
dx = dy;
dy = temp;
interchange = 1;
}
else interchange = 0;
f=2*dy - dx;
for(i=1;i<=dx;i++){
if(f>=0){
if(interchange==1) x += t1;
else y += t2;
f = f - 2*dx;
}
else{
if(interchange==1) y += t2;
else x += t1;
f = f + 2*dy;
}
pDC->SetPixel(m_winSizeX/2+x, m_winSizeY/2-y,50);
}
}
/*void CZywView::MidPointline(int x1, int y1, int x2, int y2, CDC* pDC)
{
if(abs(x1-x2) <= 1 && abs(y1-y2) <= 1) return;
else pDC->SetPixel(m_winSizeX/2 + (x1+x2)/2 + 0.5, m_winSizeY/2 - (y1+y2)/2 - 0.5,100);
MidPointline(x1,y1,(x1+x2) / 2 + 0.5,(y1+y2) / 2 + 0.5,pDC);
MidPointline((x1+x2) / 2 + 0.5,(y1+y2) / 2 + 0.5,x2,y2,pDC);
}*/
void CZywView::MidPointline(int x1, int y1, int x2, int y2, CDC* pDC)
{
int x = (x1+x2)/2,y = (y1+y2)/2;
pDC->SetPixel(m_winSizeX/2+x, m_winSizeY/2-y,200);
if(abs(x1-x2) > 1 || abs(y1-y2) > 1){
MidPointline(x1,y1,x,y,pDC);
MidPointline(x,y,x2,y2,pDC);
}
}
void CZywView::BresenhamCircle(int r,CDC *pDC)
{
int x,y,delta,d1,d2,direction;
x = 0; y = r;
delta = 2 * (1-r);
while(y>=0){
pDC->SetPixel(m_winSizeX/2+x, m_winSizeY/2-y,200);
pDC->SetPixel(m_winSizeX/2+x, m_winSizeY/2+y,200);
pDC->SetPixel(m_winSizeX/2-x, m_winSizeY/2-y,200);
pDC->SetPixel(m_winSizeX/2-x, m_winSizeY/2+y,200);
if(delta<0){
d1=2*(delta+y)-1;
if(d1 <= 0)
direction = 1;
else
direction = 2;
}
else if(delta>0){
d2 = 2*(delta-x)-1;
if(d2 <= 0)
direction = 2;
else
direction = 3;
}
else
direction = 2;
switch(direction)
{
case 1:
x++;
delta += 2*x+1;
break;
case 2:
x++;
y--;
delta += 2*(x-y+1);
break;
case 3:
y--;
delta += (-2*y+1);
break;
}
}
}
void CZywView::quadrant(int x,int y,int *flag)
{
if(x>0 && y>0)
{flag[0] = 1; flag[1] = 1;}
else if(x>0 && y<0)
{flag[0] = -1; flag[1] = 1;}
else if(x<0 && y<0)
{flag[0] = 1; flag[1] = -1;}
else if(x<0 && y>0)
{flag[0] = -1; flag[1] = -1;}
}
void CZywView::BresenhamCircleAny(int r,int R,int Q,CDC *pDC)
{
int x,y,delta,d1,d2,direction;
x = r*sin((R*PI)/180); y = r*cos((R*PI)/180);
quadrant(x,y,s);
delta = (x+s[0]*s[1])*(x+s[0]*s[1])+(y-s[1])*(y-s[1])-r*r;
// while(x != r*sin((Q*PI)/180) || y != r*cos((Q*PI)/180) )
for(int i=0;i<r*sqrt(2)*Q/90;i++)
{
pDC->SetPixel(m_winSizeX/2+x, m_winSizeY/2-y,200);
quadrant(x,y,s);
if(s[0]*delta < 0){
d1 = 2*s[0]*(delta+s[1]*y)-s[0];
if(d1 <= 0)
direction = 1;
else
direction = 2;
}
else if(s[0]*delta > 0){
d2 = 2*s[0]*(delta-s[0]*s[1]*x)-s[0];
if(d2 <= 0)
direction = 2;
else
direction = 3;
}
else
direction = 2;
switch(direction)
{
case 1:
x += s[0]*s[1];
delta += (2*s[0]*s[1]*x+1);
break;
case 2:
x += s[0]*s[1];
y -= s[1];
delta += (2*s[0]*s[1]*x-2*s[1]*y+2);
break;
case 3:
y -= s[1];
delta -= (2*s[1]*y+1);
break;
}
}
}
void CZywView::OnDdaline()
{
// TODO: Add your command handler code here
CZywDoc* pDoc = GetDocument();
m_disPlay = 0;
pDoc->UpdateAllViews(NULL);
}
void CZywView::OnBresenhamline()
{
// TODO: Add your command handler code here
CZywDoc* pDoc = GetDocument();
m_disPlay = 1;
pDoc->UpdateAllViews(NULL);
}
void CZywView::OnMidpointline()
{
// TODO: Add your command handler code here
CZywDoc* pDoc = GetDocument();
m_disPlay = 2;
pDoc->UpdateAllViews(NULL);
}
void CZywView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
m_winSizeX = cx;
m_winSizeY = cy;
}
void CZywView::OnCircleb()
{
// TODO: Add your command handler code here
CZywDoc* pDoc = GetDocument();
m_disPlay = 3;
pDoc->UpdateAllViews(NULL);
}
void CZywView::OnCircleany()
{
// TODO: Add your command handler code here
CZywDoc* pDoc = GetDocument();
m_disPlay = 4;
pDoc->UpdateAllViews(NULL);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -