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

📄 graphicview.cpp

📁 计算机图形学课程设计源码 这是大二的时候学计算机图形学时做的课程设计
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// graphicView.cpp : implementation of the CGraphicView class
//

#include "stdafx.h"
#include "graphic.h"
#include "math.h"
#include "graphicDoc.h"
#include "graphicView.h"
#include<iostream.h>
#include "cjp.h"
#include "fill.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int cntL=0,cntS=0,cntB=0,cntQ=0,cnt;	//+++
bool bn; double sishu1,sishu2,cosA,sinA;

/////////////////////////////////////////////////////////////////////////////
// CGraphicView

IMPLEMENT_DYNCREATE(CGraphicView, CView)

BEGIN_MESSAGE_MAP(CGraphicView, CView)
	//{{AFX_MSG_MAP(CGraphicView)
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_CANCELMODE()
	ON_COMMAND(ID_MENU_AREA, OnMenuArea)
	ON_COMMAND(ID_MENU_BEZIER, OnMenuBezier)
	ON_COMMAND(ID_MENU_SIRCLE, OnMenuSircle)
	ON_COMMAND(ID_MENU_FREEHAND, OnMenuFreehand)
	ON_COMMAND(ID_MENU_LINE, OnMenuLine)
	ON_COMMAND(ID_BUTTON_Bezier, OnBUTTONBezier)
	ON_COMMAND(ID_BUTTON_Clear, OnBUTTONClear)
	ON_COMMAND(ID_BUTTON_Line, OnBUTTONLine)
	ON_COMMAND(ID_BUTTON_Sircle, OnBUTTONSircle)
	ON_COMMAND(ID_BUTTON_Sphere, OnBUTTONSphere)
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_BUTTON_SEEABLE, OnButtonSeeable)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_BUTTON_SUNLIGHT, OnButtonSunlight)
	ON_COMMAND(ID_BUTTON_RIGHT, OnButtonRight)
	ON_COMMAND(ID_BUTTON_UP, OnButtonUp)
	ON_COMMAND(ID_FILE_FILL, OnFileFill)
	//}}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()

//////////////////////////////////////////////////////////////////
//自己加入的函数
void computecoefficients(int n,int *c)
{int k,i;
	for(k=0;k<=n;k++)
	{	c[k]=1;
		for(i=n;i>=k+1;i--) c[k]*=i;
		for(i=n-k;i>=2;i--) c[k]/=i;
	}//for
}//computecoefficients(int n,int *c)
void computepoint(float u,CPoint *pt,int ncontrols,CPoint *controls,int *c)
{int k,n=ncontrols-1;
	float blend;
	pt->x=(long)0.00;pt->y=(long)0.00;
	for(k=0;k<ncontrols;k++)
	{ blend=float(c[k]*pow(u,k)*pow(1-u,n-k));
	  pt->x+=long(controls[k].x*blend);
	  pt->y+=long(controls[k].y*blend);
	}//for
}//computepoint()
void bezier(CPoint *controls,int ncontrols,int m,CPoint *curve)
{	int *c=(int*)malloc(ncontrols*sizeof(int));
	int i;
	computecoefficients(ncontrols-1,c);
	for(i=0;i<=m;i++)
	computepoint(i/(float)m,&curve[i],ncontrols,controls,c);
	free(c);
}//bezier()

template <class TInpt> void input(int cnt,TInpt *pts){
}//input()
void CGraphicView::circleMidPoint(int xCenter,int yCenter,int radius){
	int x=0;
	int y=radius;
	int p=1-radius;

	circlePlotPoints(xCenter,yCenter,x,y);
	while(x<y)
	{	x++;
		if(p<0)p+=2*x+1;
		else
			{ 	y--;	p+=2*(x-y)+1;	}//else
		circlePlotPoints(xCenter,yCenter,x,y);
		}//while
	}//
/////////////////////////////////////////////////////////////////////////////
// CGraphicView construction/destruction

CGraphicView::CGraphicView()
{	 
  	 m_pDC		=NULL;
	 m_lineStyle=SOLID;
	 m_lineMask =0xffff;
	 m_lineWidth=1;
	 m_penID	=0 ;
	 m_penStyle =0 ;
	 m_maskwidth=16;
	 m_penWidth = 1 ;
	 m_bLine	=false;
	 m_bBezier	=false;
	 m_bSircle	=false;
	 m_bArea	=false;
	 m_bFreehand=false;
	 m_bSphere	=false;
	 m_bSeeable =false;
	 m_bSunlight=false;
	 m_bUp		=false;
	 m_bDown	=false;
	 m_bAnydrect=false;
	 m_bFill	=false;

	 TemPoint=NULL;
	 pEnd	 =NULL;
	 m_StartPoint.x	=0;		
	 m_StartPoint.y	=0;
	 m_LastEndPoint.x=0;	
	 m_LastEndPoint.y=0;
	 m_ForeColor=RGB(0,0,0);
	 m_BackColor=RGB(3,6,9);


}

CGraphicView::~CGraphicView()
{	
	 while(TemPoint){
		dcpt *p=TemPoint;
		TemPoint=TemPoint->next;
		delete p;
	 }//while()
	 pEnd=NULL;
/**/
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicView drawing

void CGraphicView::OnDraw(CDC* pDC)
{
	CGraphicDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(!m_pDC) m_pDC=new CClientDC(this);
	m_ForeColor=RGB(255,0,0);

}

/////////////////////////////////////////////////////////////////////////////
// CGraphicView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicView message handlers

void CGraphicView::lineany(int xa,int ya,int xb,int yb)
{
//	int i;
	int x,y,xEnd,yEnd;
	if(xa==xb){
	if(ya==yb)setPixel(xa,ya);
	else	 if (ya<yb){yEnd=yb;y=ya;}
		else{yEnd=ya;y=yb;}
	while(y<=yEnd)setPixline(xa,y++,0);
	return ;
	}//if1
if(ya==yb){
	if(xa<=xb){xEnd=xb;x=xa;}
	else{xEnd=xa;x=xb;}
	while(x<=xEnd)setPixline(x++,ya,1);
	return ;
}//if2
	float dx,dy;
	dx=(float)abs(xa-xb);
	dy=(float)abs(ya-yb);
	int k;
	if(ya>yb&&xa>xb||ya<yb&&xa<xb)k=1;
	else k=0;   //tell m>0 or m<0
	float p=2*dy-dx;
	float twoDy=2*dy,twoDyDx=2*(dy-dx);
	if(dx>=dy){
		if(xa>xb){x=xb;y=yb;xEnd=xa;}//if3..
		else{x=xa;y=ya;xEnd=xb;}//else3..
		setPixline(x,y,1);
    if(k)//if4...
	 while(x<xEnd){
		x++;
		if(p<0)p+=twoDy;
		else{y++;p+=twoDyDx;}
		setPixline(x,y,1);
		}//while

    else {
       //	p=-2*dy+dx;
	//twoDyDx=2*(-dy+dx);
 //	dy=-dy;
	while(x<xEnd){x++;
		if(p>=0)p-=twoDy;
		else {y--;p-=twoDyDx;}
		setPixline(x,y,0);
		}//while..
	}//else4...
	}//if3
else {
    p=2*dx-dy;
    twoDy=2*dx;
    twoDyDx=2*(dx-dy);
    if(ya>yb){y=yb;x=xb;yEnd=ya;}//if3..
    else{y=ya;x=xa;yEnd=yb;}//else3..
    if(k)//if5...
	while(y<yEnd){y++;
		if(p<0)p+=twoDy;
		else{x++;p+=twoDyDx;}
		setPixline(x,y,0);

	}//while..
     else{
	p=-2*dx+dy;
	twoDy=2*dx;
	twoDyDx=2*(dx-dy);
	while(y<yEnd){y++;
		if(p>=0)p-=twoDy;
		else {x--;p-=twoDyDx;}
		setPixline(x,y,0);
		}//while..
     }//else5...
  }//else3
/**/
return ;
}//::lineany()
void CGraphicView::setPixel(int x, int y)
{
	if(m_lineStyle==SOLID)m_pDC->SetPixel(x,y,m_ForeColor);
//	else
//		if(m_lineMask&(1<<(m_maskwidth-1)-m_k%m_maskwidth)) 
//			 m_pDC->SetPixel(x,y,m_ForeColor);
//		else m_pDC->SetPixel(x,y,m_BackColor);
}//::setPixel()

void CGraphicView::setPixline(int x, int y, int m)
{
	
	if(m==1){m=y; while(y<m+m_lineWidth) setPixel(x,y++) ; }
		else
		{m=x;while(x<m+m_lineWidth) setPixel(x++,y) ;}
		m_k++;
}//::setPixline()


void CGraphicView::lineany(CPoint p1, CPoint p2)
{
	lineany(p1.x,p1.y,p2.x,p2.y);
}
/**/

void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point) 
{	
	if(m_bSircle&&(GetCapture()==this)){
		ReleaseCapture();
		circleMidPoint(m_StartPoint,point);
		COLORREF color=m_ForeColor;
		m_ForeColor=RGB(250,250,250);
		lineany(m_StartPoint.x,m_StartPoint.y,point.x,point.y);
		m_ForeColor=color;

	}//if sircle

	if(m_bLine&&(GetCapture()==this)){
		ReleaseCapture();
		lineany(m_StartPoint.x,m_StartPoint.y,point.x,point.y);	
	}//if line
	if(m_bFreehand&&(GetCapture()==this)){
		ReleaseCapture();
	}//if freehand
	if(m_bBezier&&(GetCapture()==this)){
		ReleaseCapture();
		if(cnt<=3) setPixel(m_StartPoint.x,m_StartPoint.y);
		if(!TemPoint){ TemPoint=new dcpt;//[m_StartPoint,NULL];
			TemPoint->next=NULL;
			TemPoint->point=m_StartPoint;
			pEnd=TemPoint;
			cnt++;
		}//if
		else 
			if(cnt<=3)
			{pEnd->next=new dcpt;
				pEnd->next->next=NULL;
				pEnd->next->point=m_StartPoint;
				pEnd=pEnd->next;
				cnt++;
			}//else
	}//if bezier
	if((m_bArea||m_bFill)&&(GetCapture()==this)){
		ReleaseCapture();
		setPixel(m_StartPoint.x,m_StartPoint.y);
		if(!TemPoint){ TemPoint=new dcpt;//[m_StartPoint,NULL];
			TemPoint->next=NULL;
			TemPoint->point=m_StartPoint;
			pEnd=TemPoint;}
		else 
		{pEnd->next=new dcpt;
		pEnd->next->next=NULL;
		pEnd->next->point=m_StartPoint;
		lineany(pEnd->point.x,pEnd->point.y,pEnd->next->point.x,pEnd->next->point.y);
		 pEnd=pEnd->next;}//else
	}//if area


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

void CGraphicView::OnMouseMove(UINT nFlags, CPoint point) 

⌨️ 快捷键说明

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