📄 graphview.cpp
字号:
// GraphView.cpp : implementation of the CGraphView class
//
#include "stdafx.h"
#include "Graph.h"
#include "math.h"
#include "GraphDoc.h"
#include "GraphView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGraphView
IMPLEMENT_DYNCREATE(CGraphView, CView)
BEGIN_MESSAGE_MAP(CGraphView, CView)
//{{AFX_MSG_MAP(CGraphView)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_Line, OnLine)
ON_COMMAND(ID_FillColour, OnFillColour)
ON_COMMAND(ID_Circle, OnCircle)
ON_COMMAND(ID_Ellipse, OnEllipse)
ON_COMMAND(ID_AColour, OnAColour)
ON_COMMAND(ID_Triangle, OnTriangle)
ON_COMMAND(ID_Translate, OnTranslate)
ON_COMMAND(ID_Scale, OnScale)
ON_COMMAND(ID_Rotate, OnRotate)
ON_COMMAND(ID_BUTTON32788, OnButton32788Ellipse)
ON_COMMAND(ID_BUTTON32789, OnButton32789Circle)
ON_COMMAND(ID_BUTTON32791, OnButton32791Triangle)
ON_COMMAND(ID_BUTTON32792, OnButton32792Line)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CGraphView construction/destruction
CGraphView::CGraphView()
{
m_bBegin=Fill=false;
m_ept=NULL;
iR=iG=iB=count=0;
int i=1;
for(;i<=20;i++)
m_ep[i]=NULL;
m_type=dtNone;//默认情况什么都不做
}
CGraphView::~CGraphView()
{//delete m_ep;
//delete m_ept;
}
BOOL CGraphView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGraphView drawing
typedef float Matrix3x3[3][3];
Matrix3x3 theMatrix;
void matrix3x3SetIdentity(Matrix3x3 m)
{int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m[i][j]=(i==j);
}
void transformPoints2(int npts, CPoint *pts)
{int k;
float tmp;
for(k=0;k<npts;k++)
{tmp=theMatrix[0][0]*pts[k].x+theMatrix[0][1]*pts[k].y+theMatrix[0][2];
pts[k].y=theMatrix[1][0]*pts[k].x+theMatrix[1][1]*pts[k].y+theMatrix[1][2];
pts[k].x=tmp;
}
}
void matrix3x3PreMultiply(Matrix3x3 a,Matrix3x3 b){
int r,c;
Matrix3x3 tmp;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b[1][c]+a[r][2]*b[2][c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r][c]=tmp[r][c];
}
void translate(int tx, int ty)
{
Matrix3x3 m;
matrix3x3SetIdentity(m);
m[0][2]=tx,
m[1][2]=ty;
matrix3x3PreMultiply(m,theMatrix);
}
void scale(float sx, float sy, CPoint refpt)
{
Matrix3x3 m;
matrix3x3SetIdentity(m);
m[0][0]=sx;
m[0][2]=(1-sx)*refpt.x;
m[1][1]=sy;
m[1][2]=(1-sy)*refpt.y;
matrix3x3PreMultiply(m,theMatrix);
}
void rotate(float a, CPoint refpt)
{
Matrix3x3 m;
matrix3x3SetIdentity(m);
a=a*3.1415926/180;
m[0][0]=cosf(a);
m[0][1]=-sinf(a);
m[0][2]=refpt.x*(1-cosf(a))+refpt.y*sinf(a);
m[1][0]=sinf(a);
m[1][1]=cosf(a);
m[1][2]=refpt.y*(1-cosf(a))-refpt.x*sinf(a);
matrix3x3PreMultiply(m,theMatrix);
}
void BoundaryFill4(CDC *pDC, int x, int y, COLORREF crFill, COLORREF crBoundary)
{
COLORREF crCur;
crCur=pDC->GetPixel(x,y);
if(crCur!=crFill&&crCur!=crBoundary)
{pDC->SetPixel(x,y,crFill);
BoundaryFill4(pDC,x,y+1,crFill,crBoundary);
BoundaryFill4(pDC,x-1,y,crFill,crBoundary);
BoundaryFill4(pDC,x+1,y,crFill,crBoundary);
BoundaryFill4(pDC,x,y-1,crFill,crBoundary);
}
}
void CGraphView::OnDraw(CDC* pDC)
{
CGraphDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//m_line.draw(pDC);
//if(SetColor)
// m_color=RGB(iR,iG,iB);
//下面做的图元的重绘保存工作
if(m_ept) {m_ept->m_color=RGB(iR,iG,iB);m_ept->draw(pDC);}
int i;
for(i=0;i<count;i++)
{ m_ep[i]->draw(pDC);
if(m_ep[i]->Fill==true)
//m_ep[i]->BoundaryFill4(pDC,(m_ep[i]->m_ptBegin.x+m_ep[i]->m_ptEnd.x)/2,(m_ep[i]->m_ptBegin.y+m_ep[i]->m_ptEnd.y)/2,m_ep[i]->m_Fillcolor,m_ep[i]->m_color);
m_ep[i]->BoundaryFill4(pDC,m_ep[i]->m_innerPoint.x,m_ep[i]->m_innerPoint.y,m_ep[i]->m_Fillcolor,m_ep[i]->m_color);
}
//填充
//if(Fill) m_ept->BoundaryFill4(pDC,m_ept->m_innerPoint.x,m_ept->m_innerPoint.y,m_ept->m_Fillcolor,m_ept->m_color);
//if(Fill) m_ept->BoundaryFill4(pDC,(m_ept->m_ptBegin.x+m_ept->m_ptEnd.x)/2,(m_ept->m_ptBegin.y+m_ept->m_ptEnd.y)/2,m_ept->m_Fillcolor,m_ept->m_color);
//pDC->Ellipse(25,25,55,55);
//BoundaryFill4(pDC, 40, 40, RGB(0,0,0), RGB(0,0,0));
//画三角形
//if(m_type==dtTriangle&&m_ept!=NULL)
//{//if(m_ept->iPeak)
// switch(m_ept->iPeak){
// case 1:
// pDC->Polyline(m_ept->m_cpPeak,m_ept->iPeak+1); break;
// case 2:
// pDC->Polyline(m_ept->m_cpPeak,m_ept->iPeak+1); break;
// case 3:
// m_ept->m_cpPeak[m_ept->iPeak]=m_ept->m_cpPeak[m_ept->iPeak-3];
// pDC->Polyline(m_ept->m_cpPeak,m_ept->iPeak+1); break;
// default:;
//}//switch
//}//if
//下面要做的是三种基本变换
if(m_ept&&m_transform!=tfNone){
CPoint pts[6];
pts[0]=m_ept->m_cpPeak[0];
pts[1]=m_ept->m_cpPeak[1];
pts[2]=m_ept->m_cpPeak[2];
pts[3]=m_ept->m_ptBegin;
pts[4]=m_ept->m_ptEnd;
pts[5]=m_ept->m_innerPoint;
matrix3x3SetIdentity(theMatrix);
switch(m_transform){
case tfTranslate:
translate(tx,ty);break;
case tfScale:scale(sx,sy,refpt);break;
case tfRotate:rotate(a,refpt);break;
default:;
}//switch
transformPoints2(5,pts);
m_ept->m_cpPeak[0]=pts[0];
m_ept->m_cpPeak[1]=pts[1];
m_ept->m_cpPeak[2]=pts[2];
m_ept->m_ptBegin=pts[3];
m_ept->m_ptEnd=pts[4];
m_ept->m_innerPoint=pts[5];
m_transform=tfNone;
}//if
}
/////////////////////////////////////////////////////////////////////////////
// CGraphView printing
BOOL CGraphView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGraphView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGraphView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGraphView diagnostics
#ifdef _DEBUG
void CGraphView::AssertValid() const
{
CView::AssertValid();
}
void CGraphView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGraphDoc* CGraphView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphDoc)));
return (CGraphDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGraphView message handlers
void CGraphView::OnLButtonDown(UINT nFlags, CPoint point)
{
//m_bBegin=true;m_ept=new Line;
//m_ept->m_ptBegin=m_ept->m_ptEnd=point;
//m_line.m_ptBegin=m_line.m_ptEnd=point;
m_bBegin=true;
switch(m_type)
{case dtLine:m_ept=new Line;m_ept->m_ptBegin=m_ept->m_ptEnd=point;break;
case dtCircle:m_ept=new CCircle;m_ept->m_ptBegin=m_ept->m_ptEnd=point;break;
case dtEllipse: m_ept=new class Ellipse;m_ept->m_ptBegin=m_ept->m_ptEnd=point;break;
// case dtTriangle:if(m_ept->iPeak==3||(m_type!=dtNone&&m_type!=dtTriangle)) m_ept=new Triangle;
case dtTriangle:if(m_ept==NULL) m_ept=new Triangle;
else if(m_ept->iPeak==3||m_ept->iPeak==0) m_ept=new Triangle;
m_ept->m_cpPeak[m_ept->iPeak]=point;m_ept->iPeak++;
if(m_ept->iPeak==3) {//iPeak=0;
m_bBegin=false;
m_ep[count]=m_ept;//m_ept->m_bBegin=true;
count++;
}break;
default:m_bBegin=false;
}
//if(Fill) m_ept->innerFill=point;//FillConfirm=true;
CView::OnLButtonDown(nFlags, point);
}
void CGraphView::OnMouseMove(UINT nFlags, CPoint point)
{
switch(m_type){case dtLine:
case dtCircle:
case dtEllipse:
if(m_bBegin){m_ept->m_ptEnd=point;}break;
case dtTriangle:if(m_ept) m_ept->m_cpPeak[m_ept->iPeak]=point;//if(m_ept->iPeak==3) {//iPeak=0;
// m_bBegin=false;
// m_ep[count]=m_ept;//m_ept->m_bBegin=true;
// count++;
// }
break;
default:;
}
Invalidate();
CView::OnMouseMove(nFlags, point);
}
void CGraphView::OnLButtonUp(UINT nFlags, CPoint point)
{switch(m_type){
case dtLine:
case dtCircle:
case dtEllipse:
if(m_bBegin){m_ep[count]=m_ept;
count++;}m_bBegin=Fill=false; break;
case dtTriangle:Fill=false;break;
default: ;
}
CView::OnLButtonUp(nFlags, point);
}
void CGraphView::OnLine()
{
m_type=dtLine;
}
void CGraphView::OnFillColour()
{
// Fill=true;
if(m_ept) m_ept->Fill=true;
if(m_ept->iPeak==3) {
m_ept->m_innerPoint.x=(m_ept->m_cpPeak[0].x+m_ept->m_cpPeak[1].x+m_ept->m_cpPeak[2].x)/3;
m_ept->m_innerPoint.y=(m_ept->m_cpPeak[0].y+m_ept->m_cpPeak[1].y+m_ept->m_cpPeak[2].y)/3;
}
if(m_ept->iPeak==0){ m_ept->m_innerPoint.x=(m_ept->m_ptBegin.x+m_ept->m_ptEnd.x)/2;
m_ept->m_innerPoint.y=(m_ept->m_ptBegin.y+m_ept->m_ptEnd.y)/2;}
m_type=dtNone;
CGraphDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
CRGBDialog dlg;
if(dlg.DoModal()!=IDOK)
return;
else m_ept->m_Fillcolor=RGB(dlg.m_R,dlg.m_G,dlg.m_B);
}
void CGraphView::OnCircle()
{
m_type=dtCircle;
}
void CGraphView::OnEllipse()
{
m_type=dtEllipse;
}
void CGraphView::OnAColour()
{//SetColor=true;
CGraphDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
CRGBDialog dlg;
if(dlg.DoModal()!=IDOK)
return;
else{
iR=dlg.m_R;
iG=dlg.m_G;
iB=dlg.m_B;
}
Invalidate();
//SetColor=false;
}
void CGraphView::OnTriangle()
{
m_type=dtTriangle;
}
void CGraphView::OnTranslate()
{
m_transform=tfTranslate;
CGraphDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
CTranslate dlg;
if(dlg.DoModal()!=IDOK)
return;
else{
tx=dlg.m_tx;
ty=dlg.m_ty;}
}
void CGraphView::OnScale()
{
m_transform=tfScale;
CGraphDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
CScale dlg;
if(dlg.DoModal()!=IDOK)
return;
else{
sx=dlg.m_sx;
sy=dlg.m_sy;
refpt.x=dlg.m_refptx;
refpt.y=dlg.m_refpty;
}
}
void CGraphView::OnRotate()
{
m_transform=tfRotate;
CGraphDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
CRotate dlg;
if(dlg.DoModal()!=IDOK)
return;
else{
a=dlg.m_a;
refpt.x=dlg.m_refptx;
refpt.y=dlg.m_refpty;
}
}
void CGraphView::OnButton32788Ellipse()
{
m_type=dtEllipse;
}
void CGraphView::OnButton32789Circle()
{
m_type=dtCircle;
}
void CGraphView::OnButton32791Triangle()
{
m_type=dtTriangle;
}
void CGraphView::OnButton32792Line()
{
m_type=dtLine;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -