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

📄 lichaodlg.cpp

📁 用来画直线 画原 裁减 的 图形学作业
💻 CPP
字号:
// lichaoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "lichao.h"
#include "lichaoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLichaoDlg dialog

CLichaoDlg::CLichaoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLichaoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLichaoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLichaoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLichaoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLichaoDlg, CDialog)
	//{{AFX_MSG_MAP(CLichaoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_LINE1, OnLine1)
	ON_COMMAND(ID_LINE2, OnLine2)
	ON_COMMAND(ID_LINE3, OnLine3)
	ON_COMMAND(ID_CIRCLE1, OnCircle1)
	ON_COMMAND(ID_CIRCLE2, OnCircle2)
	ON_COMMAND(ID_FILL1, OnFill1)
	ON_COMMAND(ID_LIANG, OnLiang)
	ON_COMMAND(ID_SUTH, OnSuth)
	ON_COMMAND(ID_IN, OnIn)
	ON_COMMAND(ID_TRANSLATE, OnTranslate)
	ON_UPDATE_COMMAND_UI(ID_TRANSLATE, OnUpdateTranslate)
	ON_COMMAND(ID_SCALE, OnScale)
	ON_COMMAND(ID_ROTATE, OnRotate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLichaoDlg message handlers

BOOL CLichaoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLichaoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLichaoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLichaoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLichaoDlg::OnLine1() 
{
	// TODO: Add your command handler code here
	line1();
}

void CLichaoDlg::OnLine2() 
{
	// TODO: Add your command handler code here
	line2();
}

void CLichaoDlg::OnLine3() 
{
	// TODO: Add your command handler code here
		line3();
}

void CLichaoDlg::OnCircle1() 
{
	// TODO: Add your command handler code here
	Circle1(100) ;
}

void CLichaoDlg::OnCircle2() 
{
	// TODO: Add your command handler code here
	Circle2(101) ;
}

void CLichaoDlg::line1()
{
	CDC *pDC = GetDC();
	int x1=10,y1=10,x2=300,y2=300;
	double dx,dy,e,x,y;
	dx= x2-x1;
	dy=y2-y1;
	e = (fabs(dx)>fabs(dy) ? fabs(dx):fabs(dy));
	dx/=e;
	dy/=e;
	x=x1;
	y=y1;
	for(int i = 1; i<=e ; i++)
	{
		pDC->SetPixel((int)(x+0.5),(int)(y+0.5),RGB(0,0,255));
		x+=dx;
		y+=dy;
	}
}

void CLichaoDlg::line2()
{
	CDC *pDC = GetDC();
	int x0=10,y0=19,x1=300,y1=309;
	int a,b,delta1,delta2,d,x,y;
	a = y0-y1;
	b = x1-x0;
	d = 2*a+b;
	delta1 = 2*a;
	delta2 = 2*(a+b);
	x = x0;
	y = y0;
	pDC->SetPixel(x,y,RGB(0,255,0));
	while(x < x1)
	{
		if(d<0)
		{
			x++;
			y++;
			d+=delta2;
		}
		else {
			x++;
			d+=delta1;
		}
		pDC->SetPixel(x,y,RGB(0,255,0));
	}
}

void CLichaoDlg::line3()
{
	CDC *pDC = GetDC();	
	int x1=10,y1=29,x2=300,y2=319;
	int x,y ,dx,dy,p;
	x = x1;
	y = y1;
	dx= x2 - x1;
	dy = y2- y1;
	p = 2*dy - dx;
	for(; x<x2;x++)
	{
		pDC->SetPixel(x,y,RGB(255,0,0));
		if(p >= 0)
		{
		y++;
		p+= 2*(dy - dx);
		}
		else
		{
			p+=2*dy;
		}
	}
}

void CLichaoDlg::Circle1(int r)
{
	int m,n;
	m = 150;
	n = 150;
	CDC *pDC = GetDC();	
	int x,y;
	double  d;
	x = 0;
	y = r;
	d = 1.25 -r;

	pDC->SetPixel(x,y,RGB(0,255,0));
	while(x < y)
	{
		if(d<0)
		{
			d+=2*x+3;
			x++;
		}
		else
		{
			d+=2*(x-y) + 5;
			x++;
			y--;
			
		}
		pDC->SetPixel(x+n,y+m,RGB(0,255,0));
		pDC->SetPixel(-x+n,y+m,RGB(0,255,0));
		pDC->SetPixel(x+n,-y+m,RGB(0,255,0));
		pDC->SetPixel(-x+n,-y+m,RGB(0,255,0));
		pDC->SetPixel(y+n,x+m,RGB(0,255,0));
		pDC->SetPixel(-y+n,x+m,RGB(0,255,0));
		pDC->SetPixel(y+n,-x+m,RGB(0,255,0));
		pDC->SetPixel(-y+n,-x+m,RGB(0,255,0));
	}
}

void CLichaoDlg::Circle2(int r)
{
	CDC *pDC = GetDC();	
	int x,y,p,n,m;
	n = 150;
	m = 150;
	x=0;
	y = r;
	p = 3-2*r;
	for(;x<=y;x++)
	{
		pDC->SetPixel(x+n,y+m,RGB(255,0,0));
		pDC->SetPixel(-x+n,y+m,RGB(255,0,0));
		pDC->SetPixel(x+n,-y+m,RGB(255,0,0));
		pDC->SetPixel(-x+n,-y+m,RGB(255,0,0));
		pDC->SetPixel(y+n,x+m,RGB(255,0,0));
		pDC->SetPixel(-y+n,x+m,RGB(255,0,0));
		pDC->SetPixel(y+n,-x+m,RGB(255,0,0));
		pDC->SetPixel(-y+n,-x+m,RGB(255,0,0));
		if(p>=0)
		{
			p+=4*(x - y)+10;
			y --;
		}
		else
		{
			p+=4*x+6;
		}
	}
}

void CLichaoDlg::OnFill1() 
{
	// TODO: Add your command handler code here
	CDC *dc = GetDC();
	dc->Draw3dRect(20,10,300,60,RGB(0,0,255),RGB(0,0,255));
	dc->Draw3dRect(38,32,30,20,RGB(0,0,255),RGB(0,0,255));
	dc->Draw3dRect(98,42,30,20,RGB(0,0,255),RGB(0,0,255));
	ScanlineSeedfill(250,20,RGB(0,0,255),RGB(0,255,255));
}

void CLichaoDlg::OnLiang() 
{
	// TODO: Add your command handler code here
	xl =10 ;
	xr =210 ;
	yb =20 ;
	yt = 120;
	CDC *pDC = GetDC();	
	pDC->Draw3dRect(xl,yb,xr-xl,yt-yb,RGB(10,10,10),RGB(10,10,10));
	liang(0,0,500,100);
}

void CLichaoDlg::OnSuth() 
{
	// TODO: Add your command handler code here
	xl =10 +10 ;
	xr =210+10  ;
	yb =20 +10 ;
	yt = 120+10 ;
	CDC *pDC = GetDC();	
	pDC->Draw3dRect(xl,yb,xr-xl,yt-yb,RGB(10,10,10),RGB(10,10,10));
	suth(0+10 ,0+10 ,500+10 ,100+10 );
}

void CLichaoDlg::liang(double x0,double y0,double x2,double y2)
{
	CDC *pDC = GetDC();	
	pDC->MoveTo((int)x0,(int)y0);
	pDC->LineTo((int)x2,(int)y2);

	double t0,t1,deltax,deltay;
	t0 = 0.0;
	t1 = 1.0;
	deltax = x2 - x0;

	if(!cansee(-deltax,x0-xl,&t0,&t1))return;
	if(!cansee(deltax,xr-x0,&t0,&t1))return;
	deltay = y2-y0;
	if(!cansee(-deltay,y0-yb,&t0,&t1))return;
	if(!cansee(deltay,yt-y0,&t0,&t1))return;
	
	x2 = x0 + t1 * deltax;
	y2 = y0 + t1 * deltay;
	x0 = x0 + t0 * deltax;
	y0 = y0 + t0 * deltay;

	CPen b ;
	b.CreatePen(1,1,RGB(255,0,0));
	pDC->SelectObject(&b);
	pDC->MoveTo((int)x0,(int)y0);
	pDC->LineTo((int)x2,(int)y2);


}

bool CLichaoDlg::cansee(double q, double d, double *t0, double *t1)
{
	
	double r;
	if(q<0)
	{
		r = d / q;
		if(r > *t1){return false;}
		else if(r > *t0)*t0 = r;		
	}
	else if(q > 0)
	{
		r = d / q;
		if(r < *t0)return false;
		else if(r < *t1)*t1 = r;
	}
	else if(d < 0)return false;
	return true;
	
}

void CLichaoDlg::suth(double x0,double y0,double x2,double y2)
{
	CDC *pDC = GetDC();	
	pDC->MoveTo((int)x0,(int)y0);
	pDC->LineTo((int)x2,(int)y2);
	int c,c1,c2;
	double x,y;
	makecode(x0,y0,&c1);
	makecode(x2,y2,&c2);
	while(c1 !=0||c2!=0)
	{
	
		if((c1&c2)==1)return;
		c = c1;
		if(c==0)
			c= c2;
		if(c&1 ==1){y = y0+(y2-y0)*(xl-x0)/(x2-x0);x = xl;}
		else if(c&2){y = y0+(y2-y0)*(xr-x0)/(x2-x0);x = xr;}
		else if(c&4){x = x0+(x2-x0)*(yb-y0)/(y2-y0);y = yb;}
		else if(c&48){x = x0+(x2-x0)*(yt-y0)/(y2-y0);y = yt;}
		if(c==c1)
		{
			x0 = x;y0 = y;makecode(x,y,&c1);
		}
		else
		{
			x2 = x;y2 = y ;makecode(x,y,&c2);
		
		}

	}
	CPen b ;
	b.CreatePen(1,1,RGB(255,0,0));
	pDC->SelectObject(&b);
	pDC->MoveTo((int)x0,(int)y0);
	pDC->LineTo((int)x2,(int)y2);

}

void CLichaoDlg::makecode(double x, double y, int * c)
{
	*c=0;
	if(x<xl)*c=1;
	else if(x>xr)*c=2;
	if(y<yb)*c=*c+4;
	else if(y>yt)*c=*c+8;

}

void CLichaoDlg::OnIn() 
{
	// TODO: Add your command handler code here
	Invalidate();
}

void CLichaoDlg::ScanlineSeedfill(int x, int y, COLORREF boundaryvalue, COLORREF newvalue)
{

	 int x0,xl,xr,y0,xid;
	 int flag,xnextspan;
	 Stack s;
	 CDC *pDC = GetDC();
	 CPoint p ;
	 CString str,str_y;
	 s.push(CPoint(x,y));//种子像素入栈
		
	 while(!s.empty())
	 {
		 p=s.top(); //栈顶象素出栈
		 s.pop();
		 pDC->SetPixel(p.x,p.y,newvalue);
		 
		 
		 x=p.x;
		 y=p.y;
		 x0=x+1;
		 while(pDC->GetPixel(x0,y)!=boundaryvalue)
		 {
			 pDC->SetPixel(x0,y,newvalue);
			 x0++;
		 }

		 xr=x0-1;
		 x0=x-1;
		 while(pDC->GetPixel(x0,y)!=boundaryvalue)
		 {
			 pDC->SetPixel(x0,y,newvalue);
			 x0--;
		 }
		 xl=x0+1;
		 y0=y;
		 for(int i=1;i>=-1;i-=2)
		 {
			 x0=xr;
			 y=y0+i;
			 while(x0>=xl)
			 {
				 flag=0;
				 while((pDC->GetPixel(x0,y)!=boundaryvalue)
					 && (pDC->GetPixel(x0,y)!=newvalue)
					 && (x0>xl))
				 {
					if(flag==0)
					{
						flag=1;
						xid=x0;
					}
					x0--;
				 }
					if(flag==1)
					{
						s.push(CPoint(xid,y));
						flag=0;
					}
					xnextspan=x0;
					while((pDC->GetPixel(x0,y)==boundaryvalue)
						|| (pDC->GetPixel(x0,y)==newvalue)
						&& (x0>=xl))
						x0--;
					if(xnextspan==x0) x0--;
				 }//while(x0>==xl)
			 }//for(int i=1;i>=-1;i-2)
		 }//while(!s.isempty())
}

void CLichaoDlg::OnTranslate() 
{
	// TODO: Add your command handler code here
	int x,y;
	CDC *p = GetDC();
	p->Ellipse(1,1,30,30);

	theMatrix[0][0]=30;
	theMatrix[0][1]=30;
	theMatrix[0][2]=1;
	translate(100,100);
	x = theMatrix[1][0];
	y = theMatrix[1][0];
	theMatrix[0][0]=1;
	theMatrix[0][1]=1;
	theMatrix[0][2]=1;
	translate(100,100);
	p->Ellipse(theMatrix[1][0],theMatrix[1][0],x,y);

}

void CLichaoDlg::translate(int tx, int ty)
{
	Matrix3x3 m;
	int i,j;
	for(i=0;i<3;i++)
	for(j=0;j<3;j++)
		if(i==j)m[i][j]=1;
	else m[i][j]=0;

	m[0][2]=tx;
	m[1][2]=ty;

	matrix3x3PreMultiply(m,theMatrix);
//	CString str;

//	str.Format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",m[0][0],m[0][1],m[0][2],m[1][0],m[1][1],m[1][2],m[2][0],m[2][1],m[2][2]);
//	MessageBox(str);
}

void CLichaoDlg::matrix3x3PreMultiply(Matrix3x3 a, Matrix3x3 b)
{
	int r;

	for(r=0;r<3;r++)
	
			b[1][r]=a[r][0]*b[0][0]+a[r][1]*b[0][1]+a[r][2]*b[0][2];
	
}



void CLichaoDlg::OnUpdateTranslate(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here




}

void CLichaoDlg::OnScale() 
{
	// TODO: Add your command handler code here
	
	int x,y;
	CDC *p = GetDC();
	p->Ellipse(1,1,30,30);
	theMatrix[0][0]=30;
	theMatrix[0][1]=30;
	theMatrix[0][2]=1;
	scale(5,5,CPoint(0,0));
	x = theMatrix[1][0];
	y = theMatrix[1][1];
	theMatrix[0][0]=1;
	theMatrix[0][1]=1;
	theMatrix[0][2]=1;
	scale(5,5,CPoint(0,0));
	p->Ellipse(theMatrix[1][0],theMatrix[1][1],x,y);

}

void CLichaoDlg::scale(float sx, float sy, CPoint refpt)
{
	Matrix3x3 m;
	int i,j;
	for(i=0;i<3;i++)
	for(j=0;j<3;j++)
		if(i==j)m[i][j]=1;
	else m[i][j]=0;

	 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 CLichaoDlg::OnRotate() 
{
	// TODO: Add your command handler code here
	int x,y;
	CDC *p = GetDC();
	p->Ellipse(130,130,160,160);
	theMatrix[0][0]=160;
	theMatrix[0][1]=160;
	theMatrix[0][2]=1;
	rotate(5,CPoint(0,0));
	x = theMatrix[1][0];
	y = theMatrix[1][1];
	theMatrix[0][0]=130;
	theMatrix[0][1]=130;
	theMatrix[0][2]=1;
	rotate(5,CPoint(0,0));
	p->Ellipse(theMatrix[1][0],theMatrix[1][1],x,y);
}

void CLichaoDlg::rotate(float a, CPoint refpt)
{
	 Matrix3x3 m;
	int i,j;
	for(i=0;i<3;i++)
	for(j=0;j<3;j++)
		if(i==j)m[i][j]=1;
	else m[i][j]=0;

	 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);
}

⌨️ 快捷键说明

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