www.pudn.com > Fractal_.rar > FractalView.cpp


// FractalView.cpp : implementation of the CFractalView class 
// 
 
#include "stdafx.h" 
#include "Fractal.h" 
 
#include "FractalDoc.h" 
#include "FractalView.h" 
#include "stdlib.h" 
#include "math.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFractalView 
 
IMPLEMENT_DYNCREATE(CFractalView, CView) 
 
BEGIN_MESSAGE_MAP(CFractalView, CView) 
	//{{AFX_MSG_MAP(CFractalView) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	ON_WM_RBUTTONDOWN() 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFractalView construction/destruction 
 
CFractalView::CFractalView() 
{ 
	// TODO: add construction code here 
	flag=0; 
	mdown=0; 
} 
 
CFractalView::~CFractalView() 
{ 
} 
 
BOOL CFractalView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFractalView drawing 
 
void CFractalView::OnDraw(CDC* pDC) 
{ 
	CFractalDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	GetClientRect(rect); 
	width=rect.right; 
	height=rect.bottom;	 
	flag=pDoc->flag; 
	if(flag==1) 
	{ 
		x0=pDoc->x0; 
		lanbuda1=pDoc->lanbuda1; 
		lanbuda2=pDoc->lanbuda2; 
		fractal1(x0,lanbuda1,lanbuda2); 
	} 
	if(flag==2) 
	{ 
		p=pDoc->p; 
		q=pDoc->q; 
		fractal2(p,q); 
	} 
	if(flag==3) 
	{ 
		left=(double)rect.left; 
		top=(double)rect.top; 
		right=(double)rect.right; 
		bottom=(double)rect.bottom; 
		fractal3(left,top,right,bottom); 
	} 
	// TODO: add draw code for native data here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFractalView printing 
 
BOOL CFractalView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CFractalView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CFractalView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFractalView diagnostics 
 
#ifdef _DEBUG 
void CFractalView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CFractalView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CFractalDoc* CFractalView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFractalDoc))); 
	return (CFractalDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CFractalView message handlers 
 
// ------------------------------------------------------------------------- 
// 混沌分叉曲线 
// ------------------------------------------------------------------------- 
 
void CFractalView::fractal1(double x0, double lanbuda1,double lanbuda2) 
{ 
	//混沌分叉曲线 
	double xn,d_lanbuda,t_lanbuda; 
	int x,y; 
	CClientDC dc(this); 
	OnPrepareDC(&dc); 
	d_lanbuda=(lanbuda2-lanbuda1)/(double)width; 
	t_lanbuda=lanbuda1;	 
	for(int j=0;j<=width;j++) 
	{ 
		xn=x0; 
		for(int k=0;k<300;k++) 
			xn=t_lanbuda*xn*(1.0-xn); 
		for(int i=0;i<1000;i++) 
		{ 
			xn=t_lanbuda*xn*(1.0-xn);			 
			x=(int)((double)width*(t_lanbuda-lanbuda1)/(lanbuda2-lanbuda1)+0.5); 
			y=height-(int)(xn*(double)height-10.0+0.5); 
			dc.SetPixel(x,y,RGB(rand()%255,rand()%255,rand()%255)); 
			//dc.SetPixel(x,y,RGB(255,0,0)); 
		} 
		t_lanbuda+=d_lanbuda; 
	} 
	flag=0; 
} 
// ------------------------------------------------------------------------- 
// Julia集 
// ------------------------------------------------------------------------- 
 
void CFractalView::fractal2(double p,double q) 
{ 
	int x,y,k; 
	double dx,dy; 
	double xk,yk,r,temp; 
	double M=100.0; 
	int exit=0; 
	double xmin=-1.5,ymin=-1.5,xmax=1.5,ymax=1.5; 
	CClientDC dc(this); 
	dx=(xmax-xmin)/(double)(width-1.0); 
	dy=(ymax-ymin)/(double)(height-1.0); 
	for(x=0;xM) 
			{ 
				exit=1; 
			} 
			if(k==255) 
			{ 
				//dc.SetPixel(x,y,RGB(rand()%255,rand()%255,rand()%255)); 
				dc.SetPixel(x,y,RGB(255,0,0)); 
				exit=1; 
			} 
		 } 
	 } 
	 flag=0; 
} 
 
// ------------------------------------------------------------------------- 
// Mandelbrot集 
// ------------------------------------------------------------------------- 
 
void CFractalView::fractal3(double wleft,double wtop,double wright,double wbottom) 
{ 
	int x,y,k; 
	double nx,ny; 
	double dx,dy; 
	double dp,dq,p,q; 
	double xk,yk,r,temp; 
	double M=100.0; 
	int exit=0; 
	double pmin=-2.25,qmin=-1.5,pmax=0.75,qmax=1.5;	 
	CClientDC dc(this); 
	dx=(wright-wleft)/(double)width; 
	dy=(wbottom-wtop)/(double)height; 
	dp=(pmax-pmin)/(double)(width-1.0); 
	dq=(qmax-qmin)/(double)(height-1.0); 
	nx=wleft; 
	ny=wtop; 
	for(x=0;xM) 
			{ 
				exit=1; 
			} 
			if(k==128) 
			{ 
				dc.SetPixel(x,y,RGB(30,126,48)); 
				exit=1; 
			}			 
		 } 
		 ny=ny+dy;		 		  
	 } 
	nx=nx+dx; 
	} 
	 flag=0; 
} 
 
void CFractalView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
		if(flag==0) 
		{ 
			tempy=temprect.top=point.y; 
			tempx=temprect.left=point.x; 
			mdown=1; 
		} 
		CView::OnLButtonDown(nFlags, point); 
} 
 
void CFractalView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	temprect.bottom=point.y; 
	temprect.right=point.x; 
	mdown=0; 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void CFractalView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(mdown) 
	{ 
		CClientDC dc(this); 
		CGdiObject *pOldBrush = dc.SelectStockObject(NULL_BRUSH); 
		CPen newpen(PS_DASH,1,RGB(0,0,0)); 
		CPen *oldpen=dc.SelectObject(&newpen); 
		int oldmode = dc.SetROP2(R2_NOTXORPEN); 
		dc.Rectangle(temprect.left,temprect.top,tempx,tempy); 
		dc.Rectangle(temprect.left,temprect.top,point.x,point.y); 
		dc.SelectObject(pOldBrush); 
		dc.SetROP2(oldmode); 
		dc.SelectObject(oldpen); 
		tempx=point.x; 
		tempy=point.y; 
	} 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CFractalView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CClientDC dc(this); 
	dc.FillSolidRect(rect,RGB(255,255,255)); 
	double tempw,temph,templ,tempt; 
	tempw=right-left; 
	temph=bottom-top; 
	templ=left; 
	tempt=top; 
	left=templ+tempw*(double)temprect.left/(double)width; 
	top=tempt+temph*(double)temprect.top/(double)height; 
	right=templ+tempw*(double)temprect.right/(double)width; 
	bottom=tempt+temph*(double)temprect.bottom/(double)height; 
	fractal3(left,top,right,bottom); 
	CView::OnRButtonDown(nFlags, point); 
}