www.pudn.com > 44757463.rar > BezierCurve.cpp


// BezierCurve.cpp: implementation of the CBezierCurve class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "GraphSoft.h" 
#include "BezierCurve.h" 
 
#include "math.h" 
#include "GlobalFunction.h" 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
/////////////////////////////////// 
IMPLEMENT_SERIAL( CBezierCurve, CShape, 0 ) 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////// 
CBezierCurve::CBezierCurve():CShape() 
{ 
} 
CBezierCurve::CBezierCurve(COLORREF color,int penWidth,float angle):CShape(color,penWidth,angle) 
{ 
} 
CBezierCurve::CBezierCurve(CBezierCurve * const pBezierCurve):CShape(pBezierCurve) 
{ 
	int i=pBezierCurve->m_flArrayxTemp.GetSize(); 
	int j=0; 
	float fx,fy; 
	m_flArrayxTemp.RemoveAll(); 
	m_flArrayyTemp.RemoveAll(); 
	while(j++m_flArrayxTemp.GetAt(j-1); 
		fy=pBezierCurve->m_flArrayyTemp.GetAt(j-1); 
		m_flArrayxTemp.Add(fx); 
		m_flArrayyTemp.Add(fy); 
	}	 
	i=pBezierCurve->m_flArrayx.GetSize(); 
	j=0;	 
	m_flArrayx.RemoveAll(); 
	m_flArrayy.RemoveAll(); 
	while(j++m_flArrayx.GetAt(j-1); 
		fy=pBezierCurve->m_flArrayy.GetAt(j-1); 
		m_flArrayx.Add(fx); 
		m_flArrayy.Add(fy); 
	}	 
} 
CBezierCurve::~CBezierCurve() 
{ 
} 
//////////////////////////////////////////////////////////////////////////  
void CBezierCurve::Serialize( CArchive& ar) 
{ 
	CShape::Serialize( ar ) ;	 
	int i=0,j;	 
	if ( ar.IsLoading() ){	 
		float fx,fy; 
		ar>>i; 
		j=0;		 
		while(j++ < i){ 
			ar>>fx; 
			ar>>fy; 
			m_flArrayx.Add(fx); 
			m_flArrayy.Add(fy); 
			//temp 
			m_flArrayxTemp.Add(fx); 
			m_flArrayyTemp.Add(fy); 
		}			 	 
		 
	} else {	 
		RefreshData(m_bModified); 
		i=m_flArrayx.GetSize(); 
		j=0; 
		ar<SelectObject(&pen);	 
	deCasteljau(pDC,m_flArrayxTemp,m_flArrayyTemp); 
	 
	if(GetDrawPointsFlag()==1){		 
		DrawPoints(pDC,color);			 
	} 
 
	pDC->SelectObject(poldPen);	  
} 
/////////////////////////////////////////// 
void CBezierCurve::DrawPointsCutTo(CDC *pDC,COLORREF color,CFlRect m_rectFrom,CRect m_rectTo) 
{ 
//	if(!IsInRect(m_rectFrom)){ 
//		return; 
//	} 
	//得到移动扩缩后的关键数据 
	CArray flArrayx; 
	CArray flArrayy;; 
	//移动 
	int i=m_flArrayxTemp.GetSize(),j; 
	float stepx=m_rectTo.left-m_rectFrom.left; 
	float stepy=m_rectTo.top-m_rectFrom.top; 
	for(j=0;jSelectObject(&pen); 
	i=flArrayx.GetSize(); 
	int nSX,nSY; 
	nSX=flArrayx.GetAt(0); 
	nSY=flArrayy.GetAt(0); 
	for(j=0;jMoveTo(flArrayx.GetAt(j),flArrayy.GetAt(j)-1-GetPenWidth()); 
		pDC->LineTo(flArrayx.GetAt(j),flArrayy.GetAt(j)+2+GetPenWidth()); 
		pDC->MoveTo(flArrayx.GetAt(j)-1-GetPenWidth(),flArrayy.GetAt(j)); 
		pDC->LineTo(flArrayx.GetAt(j)+2+GetPenWidth(),flArrayy.GetAt(j)); 
         
		//画线 
		pen.DeleteObject();		 
		CShape::DotLine(pDC,nSX,nSY,flArrayx.GetAt(j),flArrayy.GetAt(j),color); 
		nSX=flArrayx.GetAt(j); 
		nSY=flArrayy.GetAt(j); 
	} 
	pDC->SelectObject(poldPen);       
	return; 
} 
void CBezierCurve::DrawPoints(CDC *pDC,COLORREF color) 
{ 
	CPen pen,*poldPen;		 
    pen.CreatePen(PS_SOLID,1, color);	 
	poldPen=pDC->SelectObject(&pen); 
	int i=m_flArrayxTemp.GetSize(); 
	int nSX,nSY; 
	nSX=m_flArrayxTemp.GetAt(0); 
	nSY=m_flArrayyTemp.GetAt(0); 
	for(int j=0;jMoveTo(m_flArrayxTemp.GetAt(j),m_flArrayyTemp.GetAt(j)-1-GetPenWidth()); 
		pDC->LineTo(m_flArrayxTemp.GetAt(j),m_flArrayyTemp.GetAt(j)+2+GetPenWidth()); 
		pDC->MoveTo(m_flArrayxTemp.GetAt(j)-1-GetPenWidth(),m_flArrayyTemp.GetAt(j)); 
		pDC->LineTo(m_flArrayxTemp.GetAt(j)+2+GetPenWidth(),m_flArrayyTemp.GetAt(j)); 
         
		//画线 
		pen.DeleteObject(); 
	//	pen.CreatePen(PS_DOT,1,color); 
	//	pDC->SelectObject(&pen); 
		//pDC->MoveTo(nSX,nSY); 
		//pDC->LineTo(m_flArrayxTemp.GetAt(j),m_flArrayyTemp.GetAt(j)); 
		CShape::DotLine(pDC,nSX,nSY,m_flArrayxTemp.GetAt(j),m_flArrayyTemp.GetAt(j),color); 
		nSX=m_flArrayxTemp.GetAt(j); 
		nSY=m_flArrayyTemp.GetAt(j); 
	}	 
	pDC->SelectObject(poldPen);       
	return; 
} 
//////////////////////////// 
void CBezierCurve::DrawCutToRect(CDC *pDC,COLORREF color,COLORREF fillColor,int penWidth,CFlRect m_rectFrom,CRect m_rectTo) 
{ 
//	if(!IsInRect(m_rectFrom)){ 
//		return; 
//	} 
	//得到移动扩缩后的关键数据 
	CArray flArrayx; 
	CArray flArrayy;; 
	//移动 
	int i=m_flArrayxTemp.GetSize(),j; 
	float stepx=m_rectTo.left-m_rectFrom.left; 
	float stepy=m_rectTo.top-m_rectFrom.top; 
	for(j=0;jSelectObject(&pen);	 
	deCasteljau(pDC,flArrayx,flArrayy);	 
	 
	 
	if(GetDrawPointsFlag()==1){		 
		DrawPointsCutTo(pDC,color,m_rectFrom,m_rectTo);			 
	} 
	 
	pDC->SelectObject(poldPen); 
} 
/*int CBezierCurve::deCasteljau(CDC *pDC,CArray& flArrayx,CArray& flArrayy) 
{ 
	if(flArrayx.GetSize()!=flArrayy.GetSize()) 
		return 0;	 
	CArray flLx,flLy,flRx,flRy;//(flLx,flLy)-----0〈=u〈=1/2内的控制点 
	                                         //(flRx,flRy)-----1/2〈=u〈=1内的控制点 
	float flTemp1x,flTemp1y,flTemp2x,flTemp2y,flU=0.5;//flu-------u参数 
	int i,n,j;   
	n=flArrayx.GetSize();	 
	if(n<2) return 0;  
 
	for(i=0;iMoveTo(m_flArrayx.GetAt(0),m_flArrayy.GetAt(0)); 
		pDC->LineTo(m_flArrayx.GetAt(n-1),m_flArrayy.GetAt(n-1)); 
	}else{ 
		deCasteljau(pDC,flLx,flLy); 
		deCasteljau(pDC,flRx,flRy); 
	}	 
	return 1; 
} 
*/ 
int CBezierCurve::deCasteljau(CDC *pDC,CArray& flArrayx,CArray& flArrayy) 
{ 
	if(flArrayx.GetSize()!=flArrayy.GetSize()) 
		return 0;	 
	float *pflX,*pflY;  
	float flTempx,flTempy,flU;//flu-------u参数	 
	int i,n,j;   
 
	n=flArrayx.GetSize();	 
	if(n<2) return 0;  
 
	pflX=new float[n]; 
	pflY=new float[n]; 
 
	flTempx=flArrayx.GetAt(0); 
	flTempy=flArrayy.GetAt(0); 
	for(i=0;iMoveTo(flTempx,flTempy); 
		pDC->LineTo(pflX[0],pflY[0]); 
		flTempx=pflX[0]; 
		flTempy=pflY[0]; 
	} 
 
	delete[] pflX; 
	delete[] pflY; 
 
	return 1; 
} 
//////////////////////////////////////////////////////////////////////////// 
//////////////////////////////////////////////////////////////////////////// 
void CBezierCurve::RefreshData(bool bRefresh) 
{ 
	if(bRefresh){	 
		 
		int i=m_flArrayxTemp.GetSize(); 
		int j=0; 
		float fx,fy; 
		m_flArrayx.RemoveAll(); 
		m_flArrayy.RemoveAll(); 
		while(j++& flArrayX,CArray& flArrayY,int nPenWidth,COLORREF color,COLORREF fillColor,SHAPE_FILLSTYLE nStyle) 
{ 
	//if(ptArray.GetSize()<2){ 
	//	return; 
	//} 
	CShape::CreatGraph(flArrayX,flArrayY,nPenWidth,color,fillColor,nStyle); 
	int i=flArrayX.GetSize(); 
	int j=0; 
	float flx,fly; 
	m_flArrayx.RemoveAll(); 
	m_flArrayy.RemoveAll(); 
	m_flArrayxTemp.RemoveAll(); 
	m_flArrayyTemp.RemoveAll(); 
	 
	while(j++& flArrayX,CArray& flArrayY) 
{ 
	m_flArrayx.Copy(flArrayX); 
	m_flArrayxTemp.Copy(flArrayX); 
	m_flArrayy.Copy(flArrayY); 
	m_flArrayyTemp.Copy(flArrayY); 
} 
void CBezierCurve::GetPtData(CArray& flArrayX,CArray& flArrayY) 
{ 
	flArrayX.Copy(m_flArrayxTemp);	 
	flArrayY.Copy(m_flArrayyTemp); 
} 
void CBezierCurve::CreatGraph(CArray& ptArray,int nPenWidth,COLORREF color,COLORREF fillColor,SHAPE_FILLSTYLE nStyle) 
{ 
	//if(ptArray.GetSize()<2){ 
	//	return; 
	//} 
	CShape::CreatGraph(ptArray,nPenWidth,color,fillColor,nStyle); 
	int i=ptArray.GetSize(); 
	int j=0; 
	CPoint pt; 
	m_flArrayx.RemoveAll(); 
	m_flArrayy.RemoveAll(); 
	m_flArrayxTemp.RemoveAll(); 
	m_flArrayyTemp.RemoveAll(); 
	 
	while(j++-1){ 
		CShape::Draw(pDC,RGB(255,255,255),RGB(255,255,255)); 
		m_flArrayxTemp.SetAt(m_nSelectPtNum,CurX); 
		m_flArrayyTemp.SetAt(m_nSelectPtNum,CurY); 
		m_flArrayx.SetAt(m_nSelectPtNum,CurX); 
		m_flArrayy.SetAt(m_nSelectPtNum,CurY); 
	//	CShape::Draw(pDC); 
    }	 
} 
void CBezierCurve::Rotate(CDC *pDC,float CX,float CY,float flAngle) 
{ 
		 
	if(m_flScaleTemp!=1){ 
		RefreshData(TRUE); 
	} 
	 
//	CShape::Draw(pDC,RGB(255,255,255),RGB(255,255,255));	 
	 
	m_ptRotateCenterTemp=CPoint(CX,CY); 
	m_flAngleTemp=flAngle; 
	m_bModified=TRUE;	 
	 
	int i=m_flArrayx.GetSize(); 
	int j=0; 
	float fx,fy,fxTemp,fyTemp; 
	while(j++rect.right){ 
			rect.right=1000*fx; 
		} 
		if(1000*fyrect.bottom){ 
			rect.bottom=1000*fy; 
		}		 
	}	 
	 
	return rect; 
} 
CPoint CBezierCurve::GetCenterPoint() 
{ 
	CRect rect=GetBoundaryRect(); 
	return rect.CenterPoint(); 
} 
int CBezierCurve::IsPointinRegion(POINT point) 
{ 
	int nRltVal=0; 
	int i=m_flArrayx.GetSize(); 
	int j=0; 
	CPoint pt1,pt2; 
	CRect rect; 
	while(++j < i){ 
		pt1.x=m_flArrayxTemp.GetAt(j-1); 
		pt1.y=m_flArrayyTemp.GetAt(j-1); 
		pt2.x=m_flArrayxTemp.GetAt(j); 
		pt2.y=m_flArrayyTemp.GetAt(j);		 
		rect=GetRectFromPoint(pt1,pt2);         
		if(rect.PtInRect(point)){ 
			nRltVal|=1; 
			break; 
		}		 
	}		 
	return nRltVal;	 
} 
////// 
int CBezierCurve::GetPtState(float flx,float fly,float flRate) 
{ 
	int nRltVal=0; 
	int i=m_flArrayx.GetSize(); 
	int j=0;	 
	CRect rect; 
	float tmpx,tmpy; 
	while(j++Export... 
// NOTE     : 
// RETURN   : 
// ARGUMENTS: 
//              I/O           TYPE      NAME       EXPLANATION 
//               O            FILE*    outStream    Out put File 
// CREATE   :  FNST)handwolf  2004-4-14 
// UPDATE   :   
//          :  
////////////////////////////////////////////////////////////////////// 
 
void CBezierCurve::ExPort(FILE* outStream)//增加导出txt功能时用 
{	 
	fprintf(outStream, " CBezierCurve \n"); 
	CShape::ExPort(outStream); 
	 
	int n=m_flArrayxTemp.GetSize(); 
	int i=0; 
	fprintf(outStream, "     %d   ",n); 
	while(i++ < n){ 
		fprintf( outStream, " %f %f",(float)m_flArrayxTemp.GetAt(i-1),(float)m_flArrayyTemp.GetAt(i-1)); 
	}			  
	fprintf( outStream, "\n");	 
} 
 
////////////////////////////////////////////////////////////////////// 
// MODULE   :ImPort 
// ABSTRACT :ImPort from a txt file 
// FUNCTION :File->ImPort... 
// NOTE     : 
// RETURN   : 
// ARGUMENTS: 
//              I/O           TYPE      NAME       EXPLANATION 
//               I            FILE*    inStream    in put File 
// CREATE   :  FNST)handwolf  2004-4-14 
// UPDATE   :   
//          :  
////////////////////////////////////////////////////////////////////// 
void CBezierCurve::ImPort(FILE* inStream) 
{ 
	CShape::ImPort(inStream); 
	float fx,fy; 
	int n; 
	fscanf(inStream, "%d",&n); 
	int i=0;		 
	while(i++ < n){ 
		fscanf(inStream, "%f%f",&fx,&fy); 
		m_flArrayx.Add(fx); 
		m_flArrayy.Add(fy); 
		//temp 
		m_flArrayxTemp.Add(fx); 
		m_flArrayyTemp.Add(fy); 
	}	 
} 
CString CBezierCurve::GetNameString() 
{ 
	CString str; 
	str.LoadString(IDS_BEZIERCURVE); 
	return str; 
} 
////////////////////////////////////////////////////////////////////// 
//End of File//////////////// 
//////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////