GlobalFunction.cpp
资源名称:44757463.rar [点击查看]
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:14k
源码类别:
绘图程序
开发平台:
Visual C++
- #include "GlobalFunction.h"
- #include "stdafx.h"
- #include "math.h"
- #include "GraphDefines.h"
- /////////////////////////////////////////////////////////
- CRect GetRectFromPoint(CPoint pt1,CPoint pt2)
- {
- CRect rect;
- rect.left=(pt1.x<=pt2.x?pt1.x:pt2.x);
- rect.right=(pt1.x<=pt2.x?pt2.x:pt1.x);
- rect.top=(pt1.y<=pt2.y?pt1.y:pt2.y);
- rect.bottom=(pt1.y<=pt2.y?pt2.y:pt1.y);
- //TRACE("%d,%d|%d,%dn",rect.left,rect.right,rect.top,rect.bottom);
- return rect;
- }
- ///////////////////////////////////////////////////////////
- //function GetAngleFromPoints(...)
- //
- // ptCenter
- // ----/--------------------------|ptStart
- // /flRlt
- // /
- //
- //
- //
- //
- // |ptEnd
- //
- //////////////////////////////////////////////////////////////
- float GetAngleFromPoints(CPoint ptCenter,CPoint ptStart,CPoint ptEnd)
- {
- float flRlt,flAtanA,flAtanB;
- if(ptStart.x==ptCenter.x){
- if(ptStart.x>ptCenter.x){
- flAtanA=PI/2;
- }else if(ptStart.x<ptCenter.x){
- flAtanA=-PI/2;
- }else{
- flAtanA=0;
- }
- }else if(ptEnd.x==ptCenter.x){
- if(ptEnd.x>ptCenter.x){
- flAtanB=PI/2;
- }else if(ptEnd.x<ptCenter.x){
- flAtanB=-PI/2;
- }else{
- flAtanB=0;
- }
- }else{
- flAtanA=atan((ptStart.y-ptCenter.y)/(ptStart.x-ptCenter.x));
- flAtanB=atan((ptEnd.y-ptCenter.y)/(ptEnd.x-ptCenter.x));
- if(ptStart.y-ptCenter.y>0&&ptStart.x-ptCenter.x<0){
- flAtanA+=PI;
- }
- if(ptStart.y-ptCenter.y<=0&&ptStart.x-ptCenter.x<0){
- flAtanA+=PI;
- }
- if(ptEnd.y-ptCenter.y>0&&ptEnd.x-ptCenter.x<0){
- flAtanB+=PI;
- }
- if(ptEnd.y-ptCenter.y<=0&&ptEnd.x-ptCenter.x<0){
- flAtanB+=PI;
- }
- }
- flRlt=flAtanB-flAtanA;
- TRACE("%fn",flRlt);
- return flRlt;
- }
- float GetAngleFromPoints(float x2,float y2,float x1,float y1,float x3,float y3)
- {
- float flRlt,flAtanA,flAtanB;
- if(x1==x2){
- if(x1>x2){
- flAtanA=PI/2;
- }else if(x1<x2){
- flAtanA=-PI/2;
- }else{
- flAtanA=0;
- }
- }else if(x3==x2){
- if(x3>x2){
- flAtanB=PI/2;
- }else if(x3<x2){
- flAtanB=-PI/2;
- }else{
- flAtanB=0;
- }
- }else{
- flAtanA=atan((y1-y2)/(x1-x2));
- flAtanB=atan((y3-y2)/(x3-x2));
- if(y1-y2>0&&x1-x2<0){
- flAtanA+=PI;
- }
- if(y1-y2<=0&&x1-x2<0){
- flAtanA+=PI;
- }
- if(y3-y2>0&&x3-x2<0){
- flAtanB+=PI;
- }
- if(y3-y2<=0&&x3-x2<0){
- flAtanB+=PI;
- }
- }
- flRlt=flAtanB-flAtanA;
- TRACE("%fn",flRlt);
- return flRlt;
- }
- ///////////////////////////////////////////////////////////////////////
- float GetDistance(CPoint ptStart, CPoint ptEnd)
- {
- float flRlt;
- flRlt=(ptStart.x-ptEnd.x)*(ptStart.x-ptEnd.x)+(ptStart.y-ptEnd.y)*(ptStart.y-ptEnd.y);
- flRlt=sqrt(flRlt);
- return flRlt;
- }
- float GetDistance(float flStartx,float flStarty,float flEndx,float flEndy)
- {
- float flRlt;
- flRlt=(flStartx-flEndx)*(flStartx-flEndx)+(flStarty-flEndy)*(flStarty-flEndy);
- flRlt=sqrt(flRlt);
- return flRlt;
- }
- ///////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////
- //function-----GetPointStateToRect
- //return value rtval
- //reval = 0------------in
- //reval & 1 != 0-------left
- //reval & 2 != 0-------right
- //reval & 4 != 0-------top
- //reval & 8 != 0-------bottom
- //////////////////////////////////////////////////////
- int GetPointStateToRect(float x,float y,CRect rectRes)
- {
- int val=0;
- if(x<rectRes.left) val=1;
- else if(x>rectRes.right) val=2;
- if(y<rectRes.top) val=val+4;
- else if(y>rectRes.bottom) val=val+8;
- return val;
- }
- //////////////////////////////////////////////////////
- /////////////////////////////////////////////
- //function------GetLineStateToRect
- //return value
- //0----------out
- //1----------cut
- //2----------in
- /////////////////////////////////////////////
- ////////
- int GetLineStateToRect(float flStartx,float flStarty,float flEndx,float flEndy,CRect rect)
- {
- int nRtVal=0;
- int nPtStartState,nPtEndState;
- nPtStartState = GetPointStateToRect(flStartx,flStarty, rect);
- nPtEndState = GetPointStateToRect(flEndx,flEndy,rect);
- if(nPtStartState == 0 && nPtEndState == 0){//in
- nRtVal=2;
- }
- else if(nPtStartState == 0 || nPtEndState == 0){//cut
- nRtVal=1;
- }
- else if((nPtStartState & nPtEndState) != 0){ //out
- nRtVal=0;
- }else{
- //中点对分法
- int nTemp;
- float Startx,Starty,Endx,Endy,Tempx,Tempy;
- Startx=flStartx;
- Starty=flStarty;
- Endx=flEndx;
- Endy=flEndy;
- Tempx=(flStartx+flEndx)/2;
- Tempy=(flStarty+flEndy)/2;
- nTemp=GetPointStateToRect(Tempx,Tempy,rect);
- while(1){
- if(nTemp == 0){
- nRtVal=1;
- break;
- }
- if(nTemp&nPtStartState){
- Startx=Tempx;
- Starty=Tempy;
- nPtStartState=nTemp;
- Tempx=(Startx+Endx)/2;
- Tempy=(Starty+Endy)/2;
- nTemp=GetPointStateToRect(Tempx,Tempy,rect);
- }
- if(nTemp&nPtEndState){
- Endx=Tempx;
- Endy=Tempy;
- nPtEndState=nTemp;
- Tempx=(Startx+Endx)/2;
- Tempy=(Starty+Endy)/2;
- nTemp=GetPointStateToRect(Tempx,Tempy,rect);
- }
- if(abs(Startx-Endx)<1 && abs(Starty-Endy)<1){
- nRtVal=0;
- break;
- }
- }//while(1)
- }//else
- return nRtVal;
- }
- /////////
- int GetLineStateToRect(CPoint ptStart,CPoint ptEnd,CRect rect)
- {
- return GetLineStateToRect(ptStart.x,ptStart.y,ptEnd.x,ptEnd.y,rect);
- }
- ///////////////////
- void DrawLine(CDC* pDC,CPoint ptStart,CPoint ptEnd,COLORREF color,int penWidth,UINT nStyle )
- {
- CPen pen,*poldPen;
- LOGBRUSH logbr;
- //logbr.lbStyle=BS_SOLID;
- logbr.lbStyle=nStyle;
- logbr.lbColor=color;
- pen.CreatePen(PS_GEOMETRIC,penWidth, &logbr);
- poldPen=pDC->SelectObject(&pen);
- pDC->MoveTo(ptStart);
- pDC->LineTo(ptEnd);
- pDC->SelectObject(poldPen);
- }
- void DrawLine(CDC* pDC,float flSX,float flSY,float flEX,float flEY,COLORREF color,int penWidth,UINT nStyle )
- {
- CPoint ptS(flSX,flSY);
- CPoint ptE(flEX,flEY);
- DrawLine(pDC,ptS,ptE,color,penWidth,nStyle);
- }
- //////////////////////////////////////////
- //////////////////////////////////////////////////////////////
- //
- //
- // o o
- // ptCenter ----------->o
- // o o
- //
- //////////////////////////////////////////////////////////////
- void DrawOutRectPoint(CDC *pDC,CPoint ptCenter,COLORREF color)
- {
- pDC->SetPixel(ptCenter.x-1,ptCenter.y-1,color);
- pDC->SetPixel(ptCenter.x-1,ptCenter.y+1,color);
- pDC->SetPixel(ptCenter.x+1,ptCenter.y-1,color);
- pDC->SetPixel(ptCenter.x+1,ptCenter.y+1,color);
- return;
- }
- void DrawOutRectPoint(CDC *pDC,float flx,float fly,COLORREF color)
- {
- CPoint pt(flx,fly);
- DrawOutRectPoint(pDC,pt,color);
- }
- ///////////////////////////////////////////////////////////////
- int BezierCurve(CDC *pDC,CPoint ptArray[],int n)
- {
- float *pflX,*pflY;
- float flTempx,flTempy,flU;//flu-------u参数
- int i,j;
- if(n<2) return 0;
- pflX=new float[n];
- pflY=new float[n];
- flTempx=ptArray[0].x;
- flTempy=ptArray[0].y;
- for(i=0;i<n;i++){
- pflX[i]=ptArray[i].x;
- pflY[i]=ptArray[i].y;
- }
- for(flU=0;flU<=1;flU+=0.1/n){
- for(i=1;i<n;i++){
- for(j=0;j<n-i;j++){
- pflX[j]=(1-flU)*pflX[j]+flU*pflX[j+1];
- pflY[j]=(1-flU)*pflY[j]+flU*pflY[j+1];
- }
- }
- pDC->MoveTo(flTempx,flTempy);
- pDC->LineTo(pflX[0],pflY[0]);
- flTempx=pflX[0];
- flTempy=pflY[0];
- }
- delete[] pflX;
- delete[] pflY;
- return 1;
- }
- /////////////////////////////
- ////////////////
- //辅助函数
- //return value
- //0------out
- //1------on line
- ///////////////
- int GetPtStateToBigLine(float flSx,float flSy,float flEx,float flEy,float flEnlarge,float x,float y)
- {
- //on the outline
- float flFirstx,flFirsty,flSecondx,flSecondy,flThirdx,flThirdy,flForthx,flForthy;
- flFirstx=flSx-flEnlarge;
- flFirsty=flSy;
- flSecondx=flSx+flEnlarge;
- flSecondy=flSy;
- flThirdx=flEx+flEnlarge;
- flThirdy=flEy;
- flForthx=flEx-flEnlarge;
- flForthy=flEy;
- if(fabs(flSy-flEy)<15){
- flFirsty+=2*flEnlarge;
- flSecondy-=2*flEnlarge;
- flThirdy-=2*flEnlarge;
- flForthy+=2*flEnlarge;
- }
- int nRltVal=0;
- if(y>min(flFirsty,flSecondy)&&y<max(flFirsty,flSecondy))
- {
- if((flSecondx-(flSecondx-flFirstx)*(flSecondy-y)/(flSecondy-flFirsty))>=x){
- nRltVal+=1;
- }
- }
- if(y>min(flThirdy,flSecondy)&&y<max(flThirdy,flSecondy))
- {
- if((flSecondx-(flSecondx-flThirdx)*(flSecondy-y)/(flSecondy-flThirdy))>=x){
- nRltVal+=1;
- }
- }
- if(y>min(flThirdy,flForthy)&&y<max(flThirdy,flForthy))
- {
- if((flForthx-(flForthx-flThirdx)*(flForthy-y)/(flForthy-flThirdy))>=x){
- nRltVal+=1;
- }
- }
- if(y>min(flFirsty,flForthy)&&y<max(flFirsty,flForthy))
- {
- if((flForthx-(flForthx-flFirstx)*(flForthy-y)/(flForthy-flFirsty))>=x){
- nRltVal+=1;
- }
- }
- if(nRltVal==1){
- return 1;
- }
- return 0;
- }
- /////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
- // MODULE :GetCutPtFrom2Line
- // ABSTRACT :取得两相交直线的交点
- // FUNCTION :在编辑图形时,组成边的移动
- // NOTE :
- // RETURN :返回是否成功得到交点,1--成功
- // ARGUMENTS:
- // I/O TYPE NAME EXPLANATION
- // O CPoint* pPtRtn 返回交点
- // CREATE : FNST)handwolf 2004-4-13
- // UPDATE :
- // : Modify reason
- //////////////////////////////////////////////////////////////////////
- int GetCutPtFrom2Line(CPoint pt1,CPoint pt2,CPoint pt3,CPoint pt4,CPoint* pPtRtn)
- {
- float x1,x2,x3,x4,y1,y2,y3,y4;
- float a1,b1,a2,b2;
- x1=pt1.x;
- x2=pt2.x;
- x3=pt3.x;
- x4=pt4.x;
- y1=pt1.y;
- y2=pt2.y;
- y3=pt3.y;
- y4=pt4.y;
- if(x1==x2&&x3==x4){
- return 0;
- }
- if(x1 == x2 ){
- a2=(y3-y4)/(x3-x4);
- b2=y3-a2*x3;
- ((CPoint*)pPtRtn)->x=x1;
- ((CPoint*)pPtRtn)->y=a2*x1+b2;
- return 1;
- }
- if(x3 == x4 ){
- a1=(y1-y2)/(x1-x2);
- b1=y1-a1*x1;
- ((CPoint*)pPtRtn)->x=x3;
- ((CPoint*)pPtRtn)->y=a1*x3+b1;
- return 1;
- }
- a1=(y1-y2)/(x1-x2);
- b1=y1-a1*x1;
- a2=(y3-y4)/(x3-x4);
- b2=y3-a2*x3;
- if(a1==a2){
- return 0;
- }
- ((CPoint*)pPtRtn)->x=(b2-b1)/(a1-a2);
- ((CPoint*)pPtRtn)->y=a1*(b2-b1)/(a1-a2)+b1;
- return 1;
- }
- int GetCutPtFrom2Line(float x[],float y[],float *xR,float *yR)
- {
- float a1,b1,a2,b2;
- if(fabs(x[0]-x[1])<0.001&&abs(x[2]-x[3])<0.001){
- return 0;
- }
- if(fabs(x[0]-x[1])<0.001 ){
- a2=(y[2]-y[3])/(x[2]-x[3]);
- b2=y[2]-a2*x[2];
- *xR=x[0];
- *yR=a2*x[1]+b2;
- return 1;
- }
- if(fabs(x[2]-x[3])<0.001 ){
- a1=(y[0]-y[1])/(x[0]-x[1]);
- b1=y[0]-a1*x[0];
- *xR=x[2];
- *yR=a1*x[2]+b1;
- return 1;
- }
- a1=(y[0]-y[1])/(x[0]-x[1]);
- b1=y[0]-a1*x[0];
- a2=(y[2]-y[3])/(x[2]-x[3]);
- b2=y[2]-a2*x[2];
- if(a1==a2){
- return 0;
- }
- *xR=(b2-b1)/(a1-a2);
- *yR=a1*(b2-b1)/(a1-a2)+b1;
- return 1;
- }
- //////////////////////////////////////////////////////////////////////
- // MODULE :GetDisFromPtToLine
- // ABSTRACT :取得点到直线的距离
- // FUNCTION :在编辑图形时,椭圆
- // NOTE :
- // RETURN :距离
- // ARGUMENTS:
- // I/O TYPE NAME EXPLANATION
- //
- // CREATE : FNST)handwolf 2004-4-13
- // UPDATE :
- // : Modify reason
- //////////////////////////////////////////////////////////////////////
- float GetDisFromPtToLine(CPoint point,CPoint ptLineS,CPoint ptLineE)
- {
- if(point==ptLineS||point==ptLineE||ptLineE==ptLineS){
- return 0;
- }
- if(ptLineS.x==ptLineE.x){
- return fabs(point.x-ptLineS.x);
- }
- float a,b;
- float x1,y1,x2,y2;
- //y=a*x+b;
- x1=ptLineS.x;
- x2=ptLineE.x;
- y1=ptLineS.y;
- y2=ptLineE.y;
- a=(y2-y1)/(x2-x1);
- b=y1-a*x1;
- //ax-y+b=0;
- float flRlt;
- flRlt=fabs(point.y-a*point.x-b)/sqrt(1+a*a);
- TRACE("Dis=%fn",flRlt);
- return flRlt;
- }
- float GetDisFromPtToLine(float x,float y,float Sx,float Sy,float Ex,float Ey)
- {
- if((fabs(x-Sx)<0.5&&fabs(y-Sy)<0.5)||(fabs(x-Ex)<0.5&&fabs(y-Ey)<0.5)||(fabs(Ex-Sx)<0.5&&fabs(Ey-Sy)<0.5)){
- return 0;
- }
- if(Sx==Ex){
- return fabs(x-Sx);
- }
- float a,b;
- float x1,y1,x2,y2;
- //y=a*x+b;
- x1=Sx;
- x2=Ex;
- y1=Sy;
- y2=Ey;
- a=(y2-y1)/(x2-x1);
- b=y1-a*x1;
- //ax-y+b=0;
- float flRlt;
- flRlt=fabs(y-a*x-b)/sqrt(1+a*a);
- TRACE("Dis=%fn",flRlt);
- return flRlt;
- }
- ////////////////////////////////////////////////////////////////////////////////
- float GetLineRate(float flx1,float fly1,float flx2,float fly2)
- {
- if(fabs(flx1-flx2)<0.1){
- return 1000;//
- }
- TRACE("Rate=%fn",(fly1-fly2)/(flx1-flx2));
- return (fly1-fly2)/(flx1-flx2);
- }
- float GetLineRate(CPoint ptS,CPoint ptE)
- {
- return GetLineRate(ptS.x , ptS.y ,ptE.x ,ptE.y );
- }
- ///////////////////////////////////////////////////////////////
- bool IsPtInRect(float left,float top,float right,float bottom,float x,float y)
- {
- if(x<left || x>right)
- return false;
- if(y<top || y>bottom)
- return false;
- return true;
- }
- /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
- // MODULE :GetShapeClass
- // ABSTRACT :Get a pointer point to a CShape type object By ClassName
- // FUNCTION :File->Import...
- // NOTE :
- // RETURN : a pointer point to a CShape type object
- // ARGUMENTS:
- // I/O TYPE NAME EXPLANATION
- // I char* pChClassName name of class
- // CREATE : FNST)handwolf 2004-4-14
- // UPDATE :
- // :
- //////////////////////////////////////////////////////////////////////
- class CShape;
- CShape* GetShapeClass(CString strClassName)
- {
- CRuntimeClass *pClass;
- CShape *pShape=NULL;
- AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
- // AfxLockGlobals(CRIT_RUNTIMECLASSLIST);
- for(pClass = pModuleState->m_classList;pClass!=NULL;pClass=pClass->m_pNextClass){
- if(strClassName.Compare(pClass->m_lpszClassName)==0){
- pShape=(CShape*)(pClass->CreateObject());
- }
- }
- return pShape;
- }
- ///////////////////////////////////////////////////////////////////////////////////