Shape.cpp
资源名称:44757463.rar [点击查看]
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:15k
源码类别:
绘图程序
开发平台:
Visual C++
- // Shape.cpp: implementation of the CShape class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "GraphSoft.h"
- #include "Shape.h"
- #include "math.h"
- #include "GraphDefines.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- IMPLEMENT_SERIAL( CShape, CObject, 0 )
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- unsigned long CShape::m_nObjectNum=0;
- CShape::CShape()
- {
- m_color=RGB(0,0,0);
- m_FillColor=RGB(255,255,255);
- m_nPenWidth = 1;
- m_flAngle=0;
- m_flScale=1;
- m_bSelect_flag=FALSE;
- m_nFillStyle=_shape_none_fill;
- m_nDrawPoints=1;
- //temp
- m_ptRotateCenterTemp.x=0;
- m_ptRotateCenterTemp.y=0;
- m_flAngleTemp=0;
- m_ptMagnifyCenterTemp.x=0;
- m_ptMagnifyCenterTemp.y=0;
- m_flScaleTemp=1;
- m_nSelectPtNum=-1;
- m_nSelectLineNum=-1;
- m_nID=++m_nObjectNum;
- }
- CShape::CShape(COLORREF color,int penWidth,float angle)
- {
- m_color=color;
- m_FillColor=RGB(255,255,255);
- m_nPenWidth=penWidth;
- m_flAngle=angle;
- m_flScale=1;
- m_bSelect_flag=FALSE;
- m_nFillStyle=_shape_none_fill;
- m_nDrawPoints=1;
- //temp
- m_ptRotateCenterTemp.x=0;
- m_ptRotateCenterTemp.y=0;
- m_flAngleTemp=0;
- m_ptMagnifyCenterTemp.x=0;
- m_ptMagnifyCenterTemp.y=0;
- m_flScaleTemp=1;
- m_nSelectPtNum=-1;
- m_nSelectLineNum=-1;
- m_nID=++m_nObjectNum;
- }
- CShape::CShape(CShape* const shape)
- {
- m_color=shape->m_color;
- m_FillColor=shape->m_FillColor;
- m_nPenWidth=shape->m_nPenWidth;
- m_flAngle=shape->m_flAngle;
- m_flScale=shape->m_flScale;
- m_bSelect_flag=shape->m_bSelect_flag;
- m_nFillStyle=shape->m_nFillStyle;
- m_nDrawPoints=shape->m_nDrawPoints;
- //temp
- m_ptRotateCenterTemp=shape->m_ptRotateCenterTemp;
- m_flAngleTemp=shape->m_flAngleTemp;
- m_ptMagnifyCenterTemp=shape->m_ptMagnifyCenterTemp;
- m_flScaleTemp=shape->m_flScaleTemp;
- m_nSelectPtNum=shape->m_nSelectPtNum;
- m_nSelectLineNum=shape->m_nSelectLineNum;
- m_nID=++m_nObjectNum;
- }
- CShape::~CShape()
- {
- }
- ////////////////////////////////////////////////////////////
- //Operations
- /////////////////////////////////////////////////////////////
- void CShape::Serialize( CArchive& ar )
- {
- // serialization of Base class
- CObject::Serialize( ar ) ;
- if ( ar.IsLoading() ){
- int nFillStyle;
- ar>>m_color;
- ar>>m_FillColor;
- ar>>m_nPenWidth;
- ar>>m_flAngle;
- ar>>m_flScale;
- ar>>nFillStyle;
- ar>>m_nDrawPoints;
- SetSelectFlag(FALSE);
- m_nFillStyle=(SHAPE_FILLSTYLE)nFillStyle;
- //temp
- m_ptRotateCenterTemp.x=0;
- m_ptRotateCenterTemp.y=0;
- m_flAngleTemp=0;
- m_ptMagnifyCenterTemp.x=0;
- m_ptMagnifyCenterTemp.y=0;
- m_flScaleTemp=1;
- m_nSelectPtNum=-1;
- m_nSelectLineNum=-1;
- } else {
- int nFillStyle=(int)m_nFillStyle;;
- ar<<m_color;
- ar<<m_FillColor;
- ar<<m_nPenWidth;
- ar<<m_flAngle;
- ar<<m_flScale;
- ar<<nFillStyle;
- ar<<m_nDrawPoints;
- }
- return ;
- }
- //////////////////////////////////////////////////////////////////////////////
- void CShape::Draw(CDC *pDC)
- {
- int penWidth=GetPenWidth();
- COLORREF color=GetColor();
- Draw(pDC,color,m_FillColor,penWidth);
- }
- void CShape::Draw(CDC *pDC,COLORREF color)
- {
- int penWidth=GetPenWidth();
- Draw(pDC,color,m_FillColor,penWidth);
- }
- void CShape::Draw(CDC *pDC,COLORREF color,int penWidth)
- {
- Draw(pDC,color,m_FillColor,penWidth);
- }
- void CShape::Draw(CDC *pDC,COLORREF color,COLORREF fillColor)
- {
- int penWidth=GetPenWidth();
- Draw(pDC,color,fillColor,penWidth);
- }
- void CShape::DrawCutToRect(CDC *pDC,CFlRect m_rectFrom,CRect m_rectTo)
- {
- int penWidth=GetPenWidth();
- COLORREF color=GetColor();
- DrawCutToRect(pDC,color,m_FillColor,penWidth,m_rectFrom,m_rectTo);
- }
- void CShape::DrawCutToRect(CDC *pDC,COLORREF color,CFlRect m_rectFrom,CRect m_rectTo)
- {
- int penWidth=GetPenWidth();
- DrawCutToRect(pDC,color,m_FillColor,penWidth,m_rectFrom,m_rectTo);
- }
- void CShape::DrawCutToRect(CDC *pDC,COLORREF color,int penWidth,CFlRect m_rectFrom,CRect m_rectTo)
- {
- DrawCutToRect(pDC,color,m_FillColor,penWidth,m_rectFrom,m_rectTo);
- }
- void CShape::DrawCutToRect(CDC *pDC,COLORREF color,COLORREF fillColor,CFlRect m_rectFrom,CRect m_rectTo)
- {
- int penWidth=GetPenWidth();
- DrawCutToRect(pDC,color,fillColor,penWidth,m_rectFrom,m_rectTo);
- }
- void CShape::InsertEdge(Edge *list,Edge *edge)
- {
- Edge *p,*q=list;
- p=q->next;
- while(p)
- {
- if(edge->x<p->x||edge->x+edge->dx<p->x+p->dx)
- p=0;
- else
- {
- q=p;
- p=p->next;
- }
- }
- edge->next=q->next;
- q->next=edge;
- }
- void CShape::BuildEdgeList(int cnt,POINT *pts,Edge *edges[],int nScanMin)
- {
- Edge *edge;
- POINT p1,p2;
- int i;
- p1=pts[cnt-1];
- for(i=0;i<cnt;i++)
- {
- p2=pts[i];
- if(p1.y!=p2.y)
- {
- edge=(Edge *)malloc(sizeof(Edge));
- edge->dx=-(float)(p2.x-p1.x)/(p2.y-p1.y);
- if(p1.y>p2.y)
- {
- edge->x=float(p1.x);
- edge->ymin=p2.y;
- InsertEdge(edges[p1.y-nScanMin],edge);
- }
- else
- {
- edge->x=float(p2.x);
- edge->ymin=p1.y;
- InsertEdge(edges[p2.y-nScanMin],edge);
- }
- }
- p1=p2;
- }
- }
- void CShape::BuildActiveList(int scan,Edge *active,Edge *edges[],int nScanMin)
- {
- Edge *p,*q;
- p=edges[scan-nScanMin]->next;
- while(p)
- {
- q=p->next;
- InsertEdge(active,p);
- p=q;
- }
- }
- void CShape::FillScan(CDC *pDC,int scan,Edge *active,COLORREF color)
- {
- Edge *p1,*p2;
- p1=active->next;
- while(p1)
- {
- p2=p1->next;
- pDC->MoveTo(int(p1->x+0.5),scan);
- pDC->LineTo(int(p2->x+0.5),scan);
- p1=p2->next;
- }
- }
- void CShape::DeleteAfter(Edge *q)
- {
- Edge *p=q->next;
- q->next=p->next;
- free(p);
- }
- void CShape::UpdateActiveList(int scan,Edge *active)
- {
- Edge *q=active,*p=active->next;
- while(p)
- {
- if(scan<=p->ymin)
- {
- p=p->next;
- DeleteAfter(q);
- }
- else
- {
- p->x=p->x+p->dx;
- q=p;
- p=p->next;
- }
- }
- }
- void CShape::AreaFill(CDC *pDC,int cnt,POINT *pts,COLORREF color)
- {
- Edge *active;
- int i,scan,scanmin=1000,scanmax=0;
- for(i=0;i<cnt;i++)
- {
- if(scanmax<pts[i].y) scanmax=pts[i].y;
- if(scanmin>pts[i].y) scanmin=pts[i].y;
- }
- pEdge *edges=(pEdge *)malloc((scanmax-scanmin+1)*sizeof(Edge));
- for(scan=scanmin;scan<=scanmax;scan++)
- {
- edges[scan-scanmin]=(Edge *)malloc(sizeof(Edge));
- edges[scan-scanmin]->next=0;
- }
- BuildEdgeList(cnt,pts,edges,scanmin);
- active=(Edge *)malloc(sizeof(Edge));
- active->next=0;
- for(scan=scanmax;scan>=scanmin;scan--)
- {
- BuildActiveList(scan,active,edges,scanmin);
- FillScan(pDC,scan,active,color);
- UpdateActiveList(scan-1,active);
- }
- free(active);
- for(scan=scanmin;scan<=scanmax;scan++)
- {
- free(edges[scan-scanmin]);
- }
- free(edges);
- }
- void CShape::DotLine(CDC *pDC,int x1,int y1,int x2,int y2,COLORREF color)
- {
- int i,dx,dy,di,const1,const2,inc,tmp;
- inc=2;
- i=0;
- dx=x2-x1;
- dy=y2-y1;
- if(fabs(dx)>fabs(dy))
- {
- if(dx<0)
- {
- tmp=x1;x1=x2;x2=tmp;
- tmp=y1;y1=y2;y2=tmp;
- dx=-dx;dy=-dy;
- }
- if(dy<0)
- {
- inc=-2;
- dy=-dy;
- }
- di=2*dy-dx;
- const1=2*(dy-dx);
- const2=2*dy;
- // for(i=0;i<m_nPenWidth;i++)
- // {
- // pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- // }
- while(x1<x2)
- {
- x1+=2;
- if(di>=0)
- {
- y1+=inc;
- di+=const1;
- }
- else
- {
- di+=const2;
- }
- // for(i=0;i<m_nPenWidth;i++)
- // {
- // pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- // }
- }
- }
- else
- {
- if(dy<0)
- {
- tmp=x1;x1=x2;x2=tmp;
- tmp=y1;y1=y2;y2=tmp;
- dx=-dx;dy=-dy;
- }
- if(dx<0)
- {
- inc=-2;
- dx=-dx;
- }
- di=2*dx-dy;
- const1=2*(dx-dy);
- const2=2*dx;
- // for(i=0;i<m_nPenWidth;i++)
- // {
- // pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- // }
- while(y1<y2)
- {
- y1+=2;
- if(di>=0)
- {
- x1+=inc;
- di+=const1;
- }
- else
- {
- di+=const2;
- }
- // for(i=0;i<m_nPenWidth;i++)
- // {
- // pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- // }
- }
- }
- }
- void CShape::BresenhamLine(CDC *pDC,CPoint m_pStart,CPoint m_pEnd,COLORREF color)
- {
- int x1=m_pStart.x,y1=m_pStart.y,
- x2=m_pEnd.x,y2=m_pEnd.y;
- int i,dx,dy,di,const1,const2,inc,tmp;
- inc=1;
- dx=x2-x1;
- dy=y2-y1;
- if(fabs(dx)>fabs(dy))
- {
- if(dx<0)
- {
- tmp=x1;x1=x2;x2=tmp;
- tmp=y1;y1=y2;y2=tmp;
- dx=-dx;dy=-dy;
- }
- if(dy<0)
- {
- inc=-1;
- dy=-dy;
- }
- di=2*dy-dx;
- const1=2*(dy-dx);
- const2=2*dy;
- for(i=0;i<m_nPenWidth;i++)
- {
- pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- }
- while(x1<x2)
- {
- x1++;
- if(di>=0)
- {
- y1+=inc;
- di+=const1;
- }
- else
- {
- di+=const2;
- }
- for(i=0;i<m_nPenWidth;i++)
- {
- pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- }
- }
- }
- else
- {
- if(dy<0)
- {
- tmp=x1;x1=x2;x2=tmp;
- tmp=y1;y1=y2;y2=tmp;
- dx=-dx;dy=-dy;
- }
- if(dx<0)
- {
- inc=-1;
- dx=-dx;
- }
- di=2*dx-dy;
- const1=2*(dx-dy);
- const2=2*dx;
- for(i=0;i<m_nPenWidth;i++)
- {
- pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- }
- while(y1<y2)
- {
- y1++;
- if(di>=0)
- {
- x1+=inc;
- di+=const1;
- }
- else
- {
- di+=const2;
- }
- for(i=0;i<m_nPenWidth;i++)
- {
- pDC->SetPixel(x1+i,y1,color);
- pDC->SetPixel(x1,y1+i,color);
- }
- }
- }
- }
- void CShape::BresenhamLine(CDC *pDC,int x1,int y1,int x2,int y2,COLORREF color)
- {
- CPoint p1,p2;
- p1.x=x1;
- p1.y=y1;
- p2.x=x2;
- p2.y=y2;
- BresenhamLine(pDC,p1,p2,color);
- }
- void CShape::BresenhamLine(CDC *pDC,int x1,int y1,int x2,int y2,COLORREF color,CRect rect)
- {
- int i,dx,dy,di,const1,const2,inc,tmp;
- inc=1;
- dx=x2-x1;
- dy=y2-y1;
- if(fabs(dx)>fabs(dy))
- {
- if(dx<0)
- {
- tmp=x1;x1=x2;x2=tmp;
- tmp=y1;y1=y2;y2=tmp;
- dx=-dx;dy=-dy;
- }
- if(dy<0)
- {
- inc=-1;
- dy=-dy;
- }
- di=2*dy-dx;
- const1=2*(dy-dx);
- const2=2*dy;
- for(i=0;i<m_nPenWidth;i++)
- {
- if(x1+i>=rect.left&&x1+i<=rect.right&&y1>=rect.top&&y1<=rect.bottom)
- pDC->SetPixel(x1+i,y1,color);
- if(x1>=rect.left&&x1<=rect.right&&y1+i>=rect.top&&y1+i<=rect.bottom)
- pDC->SetPixel(x1,y1+i,color);
- }
- while(x1<x2)
- {
- x1++;
- if(di>=0)
- {
- y1+=inc;
- di+=const1;
- }
- else
- {
- di+=const2;
- }
- for(i=0;i<m_nPenWidth;i++)
- {
- if(x1+i>=rect.left&&x1+i<=rect.right&&y1>=rect.top&&y1<=rect.bottom)
- pDC->SetPixel(x1+i,y1,color);
- if(x1>=rect.left&&x1<=rect.right&&y1+i>=rect.top&&y1+i<=rect.bottom)
- pDC->SetPixel(x1,y1+i,color);
- }
- }
- }
- else
- {
- if(dy<0)
- {
- tmp=x1;x1=x2;x2=tmp;
- tmp=y1;y1=y2;y2=tmp;
- dx=-dx;dy=-dy;
- }
- if(dx<0)
- {
- inc=-1;
- dx=-dx;
- }
- di=2*dx-dy;
- const1=2*(dx-dy);
- const2=2*dx;
- for(i=0;i<m_nPenWidth;i++)
- {
- if(x1+i>=rect.left&&x1+i<=rect.right&&y1>=rect.top&&y1<=rect.bottom)
- pDC->SetPixel(x1+i,y1,color);
- if(x1>=rect.left&&x1<=rect.right&&y1+i>=rect.top&&y1+i<=rect.bottom)
- pDC->SetPixel(x1,y1+i,color);
- }
- while(y1<y2)
- {
- y1++;
- if(di>=0)
- {
- x1+=inc;
- di+=const1;
- }
- else
- {
- di+=const2;
- }
- for(i=0;i<m_nPenWidth;i++)
- {
- if(x1+i>=rect.left&&x1+i<=rect.right&&y1>=rect.top&&y1<=rect.bottom)
- pDC->SetPixel(x1+i,y1,color);
- if(x1>=rect.left&&x1<=rect.right&&y1+i>=rect.top&&y1+i<=rect.bottom)
- pDC->SetPixel(x1,y1+i,color);
- }
- }
- }
- }
- int CShape::DrawLineToRect(CDC *pDC,POINT start,POINT end,CFlRect m_rectFrom,CRect m_rectTo)
- {
- int x1,y1,x2,y2,x3,y3,x,y,k;
- float A,B,C,D;
- int scale;
- int m_oldWidth;
- scale=((float)(m_rectTo.right-m_rectTo.left))/(m_rectFrom.right-m_rectFrom.left)+0.5;
- x1=start.x;
- y1=start.y;
- x2=end.x;
- y2=end.y;
- int dx=m_rectFrom.left -m_rectTo.left;
- int dy=m_rectFrom.top -m_rectTo.top;
- unsigned c,c1,c2;
- c1=PtStateToRect(x1,y1,m_rectFrom);c2=PtStateToRect(x2,y2,m_rectFrom);
- x3=x2;y3=y2;k=0;
- if(c1!=0) goto L4;
- x2=x1;y2=y1;
- L1: x1=x3;y1=y3;
- if(c2==0) goto L2;
- k=1;x3=x2;y3=y2;c1=c2;c2=0;goto L5;
- L2: m_oldWidth=m_nPenWidth;
- m_nPenWidth=scale*m_nPenWidth;
- if(k==1) BresenhamLine(pDC,(x1-dx)*scale,(y1-dy)*scale,(x2-dx)*scale,(y2-dy)*scale,m_color,m_rectTo);
- else BresenhamLine(pDC,(x2-dx)*scale,(y2-dy)*scale,(x1-dx)*scale,(y1-dy)*scale,m_color,m_rectTo);
- m_nPenWidth=m_nPenWidth/scale;
- m_nPenWidth=m_oldWidth;
- L3: return 0;
- L4: if((c1&c2)!=0) goto L3;
- L5: x=(x1+x2)/2;y=(y1+y2)/2;
- if(x==x1&&y==y1)
- L6: {x2=x;y2=y;goto L1;}
- if(x==x2&&y==y2) goto L6;
- c=PtStateToRect(x,y,m_rectFrom);
- if((c&c1)==0) {x2=x;y2=y;goto L5;}
- else {x1=x;y1=y;c1=c;goto L4;}
- }
- //////////////////////////////////////////////////////
- //function-----PtStateToRect
- //return value rtval
- //reval = 0------------in
- //reval & 1 != 0-------left
- //reval & 2 != 0-------right
- //reval & 4 != 0-------top
- //reval & 8 != 0-------bottom
- //////////////////////////////////////////////////////
- int CShape::PtStateToRect(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-----PtStateToRect
- //return value rtval
- //reval = 0------------in
- //reval & 1 != 0-------left
- //reval & 2 != 0-------right
- //reval & 4 != 0-------top
- //reval & 8 != 0-------bottom
- //////////////////////////////////////////////////////
- int CShape::PtStateToRect(CPoint point,CRect rectRes)
- {
- return PtStateToRect(point.x,point.y,rectRes);
- }
- ///////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
- // MODULE :ExPort
- // ABSTRACT :Export to a txt file
- // FUNCTION :File->Export...
- // NOTE :
- // RETURN :
- // ARGUMENTS:
- // I/O TYPE NAME EXPLANATION
- // O FILE* outStream Out put File
- // CREATE : FNST)handwolf 2004-4-14
- // UPDATE :
- // :
- //////////////////////////////////////////////////////////////////////
- void CShape::ExPort(FILE* outStream)//增加导出txt功能时用
- {
- fprintf( outStream, " %ld %ld %d %f %f %d %d ",m_color,
- m_FillColor, m_nPenWidth, m_flAngle,m_flScale,m_nFillStyle,m_nDrawPoints);
- }
- //////////////////////////////////////////////////////////////////////
- // 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 CShape::ImPort(FILE* inStream)
- {
- fscanf(inStream, "%ld %ld %d %f %f %d %d",&m_color, &m_FillColor, &m_nPenWidth, &m_flAngle,&m_flScale,&m_nFillStyle,&m_nDrawPoints);
- }