⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphicsalgorithmview.cpp

📁 用vc++编写的程序
💻 CPP
字号:
// GraphicsAlgorithmView.cpp : implementation of the CGraphicsAlgorithmView class
//

#include "stdafx.h"
#include "afxwin.h"
#include "GraphicsAlgorithm.h"

#include "GraphicsAlgorithmDoc.h"
#include "GraphicsAlgorithmView.h"
#include "LWidth.h"

#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ROUND(a) ((int)(a+0.5))
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView

IMPLEMENT_DYNCREATE(CGraphicsAlgorithmView, CView)

BEGIN_MESSAGE_MAP(CGraphicsAlgorithmView, CView)
	//{{AFX_MSG_MAP(CGraphicsAlgorithmView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(idCurColor, OnidCurColor)
	ON_COMMAND(idClear, OnidClear)
	ON_COMMAND(idPolygon, OnidPolygon)
	ON_COMMAND(idSetPolygon, OnidSetPolygon)
	ON_COMMAND(IDCLOSE, OnClose)
	ON_COMMAND(idFillColor, OnidFillColor)
	ON_COMMAND(idLineWidth, OnidLineWidth)
	ON_COMMAND(idabout, Onidabout)
	ON_COMMAND(IDM_Breseham_Circle, OnBresehamCircle)
	ON_COMMAND(idrectanguar, Onidrectanguar)
	ON_COMMAND(ID_C_S_Line, OnCSLine)
	ON_COMMAND(ID_Sline, OnSline)
	ON_WM_CREATE()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView construction/destruction

CGraphicsAlgorithmView::CGraphicsAlgorithmView()
{
	// TODO: add construction code here
//	tBreLine=0;
//	tDDALine=1;

}

CGraphicsAlgorithmView::~CGraphicsAlgorithmView()
{
}

BOOL CGraphicsAlgorithmView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView drawing

void CGraphicsAlgorithmView::OnDraw(CDC* Gra)
{
	CGraphicsAlgorithmDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
		Gra->SaveDC();

}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView printing

BOOL CGraphicsAlgorithmView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGraphicsAlgorithmView::OnBeginPrinting(CDC* /*Gra*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGraphicsAlgorithmView::OnEndPrinting(CDC* /*Gra*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView diagnostics

#ifdef _DEBUG
void CGraphicsAlgorithmView::AssertValid() const
{
	CView::AssertValid();
}

void CGraphicsAlgorithmView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGraphicsAlgorithmDoc* CGraphicsAlgorithmView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicsAlgorithmDoc)));
	return (CGraphicsAlgorithmDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView message handlers



void CGraphicsAlgorithmView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	Gra=new CClientDC(this);
	Curcolor=RGB(255,0,255);
	FillColor=RGB(255,0,255);
	CurPen.CreatePen(PS_SOLID,3,Curcolor);
	OrgPen.CreatePen(PS_SOLID,3,RGB(255,0,255));
	MouDown=false;
	FunSel=-1;
}



void CGraphicsAlgorithmView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	MouDown=true;
	switch(FunSel)
	{
	case 0:
			break;
	case 1:
	BegP=point;
	if(PNum==0)
	{
		ContP[PNum++]=point;
	}else{
		BegP=ContP[PNum-1];
	}
	PreP=BegP;
//	pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
	Gra->SetROP2(R2_XORPEN);
	Gra->SelectObject(&OrgPen);  
	Gra->MoveTo(BegP.x-10,BegP.y);
	Gra->LineTo(BegP.x+10,BegP.y);
	Gra->MoveTo(BegP.x,BegP.y-10);
	Gra->LineTo(BegP.x,BegP.y+10);
	Gra->MoveTo(BegP);
	Gra->LineTo(PreP);
			break;
	case 2:
			break;
	case 3:
  
		//	pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
			Gra->SetROP2(R2_XORPEN);
			Gra->SelectObject(&CurPen);  
			BegP=point;PreP=point;
			Gra->MoveTo(BegP.x-10,BegP.y);
			Gra->LineTo(BegP.x+10,BegP.y);
			Gra->MoveTo(BegP.x,BegP.y-10);
			Gra->LineTo(BegP.x,BegP.y+10);
			Gra->MoveTo(BegP);
			Gra->LineTo(PreP);
			break;
	case 4:
		Gra->SetROP2(R2_XORPEN);
		Gra->SelectObject(&CurPen);
		BegP=point;PreP=point;
		Gra->MoveTo(BegP.x-10,BegP.y);
		Gra->LineTo(BegP.x+10,BegP.y);
		Gra->MoveTo(BegP.x,BegP.y-10);
		Gra->LineTo(BegP.x,BegP.y+10);
	case 5:
    case 6:
	//case 7:

		Gra->SetROP2(R2_XORPEN);
		Gra->SelectObject(&CurPen);
		BegP=point;PreP=point;
		break;
	//case 8:
		//Gra->SetROP2(R2_XORPEN);
	//	Gra->SelectObject(&CurPen);
	//	BegP=point;PreP=point;
		//break;

	

	}
	CView::OnLButtonDown(nFlags, point);
}

void CGraphicsAlgorithmView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	MouDown=false;
	CPoint p1;
	CPoint p2;

	switch(FunSel)
	{
	case 0:
			break;
	case 1:
		Gra->SetROP2(R2_XORPEN);
		Gra->MoveTo(BegP.x-10,BegP.y);
		Gra->LineTo(BegP.x+10,BegP.y);
		Gra->MoveTo(BegP.x,BegP.y-10);
		Gra->LineTo(BegP.x,BegP.y+10);
		Gra->MoveTo(BegP);
		Gra->LineTo(point);
		//	pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
		Gra->SetROP2(R2_COPYPEN);
		Gra->SelectObject(&CurPen);  
		Gra->MoveTo(BegP);
		Gra->LineTo(point);
	 	ContP[PNum++]=point;
		if(select==1){ 
			 Gra->SetROP2(R2_XORPEN);
			Gra->SelectObject(&OrgPen);begin=m;end=n;
			C_S_Line(ContP[0].x,ContP[0].y,ContP[1].x,ContP[1].y,begin.x,end.x,begin.y,end.y,&p1,&p2,(RGB(0,0,255)));
			PNum=0;}
		if(select==2){ 
			 Gra->SetROP2(R2_XORPEN);
			Gra->SelectObject(&OrgPen);begin=m;end=n;
			Sline(ContP[0].x,ContP[1].x,ContP[0].y,ContP[1].y,begin.x,end.x,begin.y,end.y);
			PNum=0;}


		//	Gra->Ellipse(point.x-10,point.y-10,point.x+10,point.y+10);
			break;

	case 2:
			FloodFill4(point,FillColor,Curcolor);
			break;
	case 3:
			Gra->SetROP2(R2_XORPEN);
			Gra->MoveTo(BegP.x-10,BegP.y);
			Gra->LineTo(BegP.x+10,BegP.y);
			Gra->MoveTo(BegP.x,BegP.y-10);
			Gra->LineTo(BegP.x,BegP.y+10);
			Gra->MoveTo(BegP);
			Gra->LineTo(PreP);
		//	pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
			Gra->SetROP2(R2_COPYPEN);
			Gra->SelectObject(&OrgPen);  
			Gra->MoveTo(BegP);
			Gra->LineTo(point);
			break;
    case 6: 
			Gra->SetROP2(R2_COPYPEN);
			Gra->SelectObject(&OrgPen);
			rectangular(BegP,point);
			begin=BegP;end=PreP;
	        select=0;
		break;
	//case 8:
			//Gra->SetROP2(R2_COPYPEN);
		//	Gra->SelectObject(&OrgPen);
		    //rectangular(BegP,point);
		   // Gra->SetROP2(R2_XORPEN);
		//	Gra->SelectObject(&OrgPen);
		//Sline(ContP[0].x,ContP[1].x,ContP[0].y,ContP[1].y,begin.x,end.x,begin.y,end.y);
	         
		break;

	//case 7:
		   // Gra->SetROP2(R2_COPYPEN);
			//Gra->SelectObject(&CurPen);
		    //rectangular(BegP,point);
            //CPoint p1,p2;
		    //Gra->SetROP2(R2_XORPEN);
			//Gra->SelectObject(&OrgPen);

			 //begin=m;end=n;
			//C_S_Line(ContP[0].x,ContP[0].y,ContP[1].x,ContP[1].y,begin.x,end.x,begin.y,end.y,&p1,&p2,(RGB(0,0,255)));
			



			//	break;
	
	
//break;
	}
	CView::OnLButtonUp(nFlags, point);
}

void CGraphicsAlgorithmView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	
	if(MouDown){
	switch(FunSel)
	{
	case 0:
			break;
	case 1:
		Gra->MoveTo(BegP);
		Gra->LineTo(PreP);
		PreP=point;		
		Gra->MoveTo(BegP);

		Gra->LineTo(PreP);
			break;
	case 2:
			break;
	case 3:
			break;
	case 6:     begin=BegP;end=PreP;
				rectangular(BegP,PreP);
                PreP=point;	
				rectangular(BegP,point);
                m=BegP;n=point;select=0;
					break;
//	case 7:
     
		        //begin=BegP;end=PreP;
				
		        //rectangular(BegP,PreP);
                //PreP=point;	end=PreP;
				//rectangular(BegP,point);
			
               // break;
	//case 8:     
		        //begin=BegP;end=PreP;
				
		        //rectangular(BegP,PreP);
               // PreP=point;	
				//rectangular(BegP,point);
			    //break;
	}
	}
	
	CView::OnMouseMove(nFlags, point);
}




void CGraphicsAlgorithmView::OnidFloodFilll4() 
{
	// TODO: Add your command handler code here
	FunSel=1;//四连通区域填充

}

void CGraphicsAlgorithmView::FloodFill4(CPoint SeedP, COLORREF FillColor, COLORREF BdColor)
{
	if((Gra->GetPixel(SeedP)!=FillColor)&&(Gra->GetPixel(SeedP)!=BdColor))
	{
		Gra->SetPixel(SeedP,FillColor);
		FloodFill4(SeedP+CPoint(1,0),  FillColor, BdColor);
	 	FloodFill4(SeedP-CPoint(1,0),  FillColor, BdColor);
		FloodFill4(SeedP+CPoint(0,1),  FillColor, BdColor);
		FloodFill4(SeedP-CPoint(0,1),  FillColor, BdColor);
	}
}

void CGraphicsAlgorithmView::OnidCurColor() 
{
	// TODO: Add your command handler code here
	CColorDialog cdlg;
	if(cdlg.DoModal()==IDOK)
	{
		Curcolor=cdlg.GetColor();
		CurPen.DeleteObject();
		CurPen.CreatePen(PS_SOLID,3,Curcolor);
	}
	
}

void CGraphicsAlgorithmView::OnidClear() 
{
	// TODO: Add your command handler code here
	FunSel=0;
	select=0;
	CRect rc;
	GetClientRect(rc);
	Gra->FillSolidRect(rc,Gra->GetBkColor());
}

void CGraphicsAlgorithmView::OnidPolygon() 
{
	// TODO: Add your command handler code here
	FunSel=1;PNum=0;
}

void CGraphicsAlgorithmView::OnidSetPolygon() 
{
	// TODO: Add your command handler code here
	FunSel=2;
	static bool First=1;
	if(First)
	{
		First=0;
		MessageBox("提示: 这个直接递归调用的填充程序在填充稍大的区域时容易出现意外,终止运行!");
	}

}

void CGraphicsAlgorithmView::OnClose() 
{
	// TODO: Add your command handler code here
	FunSel=5;
	Gra->MoveTo(ContP[PNum-1]);
 	Gra->LineTo(ContP[0]);
	
}

void CGraphicsAlgorithmView::OnidFillColor() 
{
	// TODO: Add your command handler code here
		CColorDialog cdlg;
	if(cdlg.DoModal()==IDOK)
	{
		FillColor=cdlg.GetColor();

	}

}

void CGraphicsAlgorithmView::OnidLineWidth() 
{
	// TODO: Add your command handler code here
		LWidth ldlg;
		ldlg.DoModal();
		CurPen.DeleteObject();
		CurPen.CreatePen(PS_SOLID,ldlg.m_LWidth,Curcolor);

}

void CGraphicsAlgorithmView::Onidabout() 
{
	// TODO: Add your command handler code here
	MessageBox(" 为保证获得正确的结果,请安菜单的顺序从左到右调用各项功能!");

}




void CGraphicsAlgorithmView::OnBresehamCircle() 
{
   FunSel=5;
	// TODO: Add your command handler code here
	
}

void CGraphicsAlgorithmView::Cohen_Sutherland()
{

}
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8


void CGraphicsAlgorithmView::encode(int x,int y,int*code,int XL,int XR,int YB,int YT)
{
      int c=0;
	  if(x<XL) c=c|LEFT;
	  else if(x>XR) c=c|RIGHT;
	  if(y<YB) c=c|BOTTOM;
	  else if(y>YT) c=c|TOP;
	  *code=c;


}

void CGraphicsAlgorithmView::C_S_Line(int x1,int y1,int x2,int y2,int XL,int XR,int YB,int YT,CPoint* p1,CPoint* p2,COLORREF color)
{
       int code1,code2,code;
	   int x,y;
	   encode(x1,y1,&code1,XL,XR,YB,YT);
       encode(x2,y2,&code2,XL,XR,YB,YT);
	   while(code1!=0||code2!=0)
	   {
		   if((code1&code2)!=0) return;
		      code=code1;
		   if(code1==0) code=code2;
		       if((LEFT&code)!=0)
			   {x=XL;
			   y=y1+(y2-y1)*(XL-x1)/(x2-x1);
			   }
			   else if((RIGHT&code)!=0)
			   {
				 x=XR;
				 y=y1+(y2-y1)*(XR-x1)/(x2-x1);
			   }
			   else if((BOTTOM&code)!=0)
			   {
				   y=YB;
				   x=x1+(x2-x1)*(YB-y1)/(y2-y1);
			   }
			   else if((TOP&code)!=0)
			   {
				   y=YT;
				   x=x1+(x2-x1)*(YT-y1)/(y2-y1);
			   }
           if(code=code1)
		   {
			   x1=x;y1=y;
			   encode(x,y,&code1,XL,XR,YB,YT);
		   }
		   else
		   {
			   x2=x;y2=y;
			   encode(x,y,&code2,XL,XR,YB,YT);
		   }
	   }
    
    p1->x=x1;
	p1->y=y1;
	p2->x=x2;
	p2->y=y2;

     		Gra->MoveTo(*p1);
		    Gra->LineTo(*p2);

}

void CGraphicsAlgorithmView::rectangular(CPoint begin,CPoint end)
{  	Gra->MoveTo(begin);
 	Gra->LineTo(end.x,begin.y);
	Gra->MoveTo(begin);
	Gra->LineTo(begin.x,end.y);
		Gra->MoveTo(end);
	Gra->LineTo(end.x,begin.y);
			Gra->MoveTo(end);
	Gra->LineTo(begin.x,end.y);


}

void CGraphicsAlgorithmView::Onidrectanguar() 
{
	// TODO: Add your command handler code here
	 FunSel=6;
}

void CGraphicsAlgorithmView::OnCSLine() 
{
	// TODO: Add your command handler code here
	select=1;
}



int CGraphicsAlgorithmView::mmax(int x,int y)
{if(x>y) return(x);
 else return(y);

}

int CGraphicsAlgorithmView::mmin(int x,int y)
{if(x<y) return(x);
 else return(y);

}

void CGraphicsAlgorithmView::Sline(int x1,int x2,int y1,int y2,int xl,int xr,int yd,int yu)
{
		int xs,ys,xe,ye;
	if(mmax(x1,x2)<xl) return;
	     else if(mmin(x1,x2)>xr) return;
		   else if(mmax(y1,y2)<yd) return;
		     else if(mmin(y1,y2)>yu) return;
	if((x1>=xl)&&(x1<=xr))
	{if((y1>=yd)&&(y1<=yu)) {xs=x1;ys=y1;}
	else{if(y1<yd){xs=x1+(yd-y1)*(x2-x1)/(y2-y1);ys=yd;}
	    else{xs=x1+(yu-y1)*(x2-x1)/(y2-y1);ys=yu;}
		if((xs<xl)||(xs>xr)) return;}}
	else{if(x1<xl){xs=xl;ys=y1+(xl-x1)*(y2-y1)/(x2-x1);}
	         else{xs=xr;ys=y1+(xr-x1)*(y2-y1)/(x2-x1);}
       if((ys<yd)||(ys>yu))
		     if((y1>yd)&&(y1<=yu)) return;
			 else if(y1<yd){if(ys>yu) return;
			          else{xs=x1+(yd-y1)*(x2-x1)/(y2-y1);ys=yd;}}
			 else if(ys<yd) return;
			 else{xs=x1+(yu-y1)*(x2-x1)/(y2-y1);ys=yu;}
			 if((xs<xl)||(xs>xr)) return;}
	if((x2>=xl)&&(x2<=xr))
	{if((y2>=yd)&&(y2<=yu)) {xe=x2;ye=y2;}
	else {if(y2<yd){xe=x1+(yd-y1)*(x2-x1)/(y2-y1);ye=yd;}
	else{xe=x1+(yu-y1)*(x2-x1)/(y2-y1);ye=yu;}
	if((xe<xl)&&(xe>xr)) return;}}
	else{if(x2<xl) {xe=xl;ye=y1+(xl-x1)*(y2-y1)/(x2-x1);}
	else{xe=xr;ye=y1+(xr-x1)*(y2-y1)/(x2-x1);
	    if((ye<yd)||(ye>yu))
			if((y2>=yd)&&(y2<=yu)) return;
			else if(y2<yd) {if(ys>yu) return;
			else{xe=x1+(yd-y1)*(x2-x1)/(y2-y1);ye=yd;}}
			else if(ys<yd) return;
			else {xe=x1+(yu-y1)*(x2-x1)/(y2-y1);ye=yu;}
			if((xs<xl)||(xs>xr)) return;}

}
Gra->MoveTo(xs,ys);
Gra->LineTo(xe,ye);

}

void CGraphicsAlgorithmView::OnSline() 
{
	// TODO: Add your command handler code here
	select=2;
}



int CGraphicsAlgorithmView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO: Add your specialized creation code here
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -