计算机图形学实验一.docx
《计算机图形学实验一.docx》由会员分享,可在线阅读,更多相关《计算机图形学实验一.docx(13页珍藏版)》请在冰豆网上搜索。
计算机图形学实验一
一、实验目的:
基本图形生成算法实验:
实现DDA、中点算法、Bresenham算法
二、实验内容
//DrawView.cpp文件
voidCDrawView:
:
OnDrawMidline()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_MIDLINE;
DrawType=2;
}
voidCDrawView:
:
OnDrawDdaline()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_DDALINE;
DrawType=1;
}
voidCDrawView:
:
OnDrawBshline()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_BSHLINE;
DrawType=3;
}
voidCDrawView:
:
OnDrawBshcircle()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_BSHCIRCLE;
DrawType=2;
}
voidCDrawView:
:
OnDrawMidcircle()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_MIDCIRCLE;
DrawType=1;
}
voidCDrawView:
:
OnDrawMidellipse()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_MIDELLIPSE;
DrawType=1;
}
//VC画线、VC画圆、VC画椭圆命令处理函数
voidCDrawView:
:
OnDrawVcline()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_VCLINE;
DrawType=0;
}
voidCDrawView:
:
OnDrawVccircle()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_VCCIRCLE;
DrawType=0;
}
voidCDrawView:
:
OnDrawVcellipse()
{
//TODO:
Addyourcommandhandlercodehere
CurrentDraw=DRAW_VCELLIPSE;
DrawType=0;
}
//shape.h
//以下为第二章所用的类定义
classCLine:
publicCShape
{
protected:
intm_drawtype;//
POINTm_pStart,m_pEnd;
CLine(){}
DECLARE_SERIAL(CLine)
public:
CLine(POINTpStart,POINTpEnd,intWidth,intStyle,COLORREFColor,intm_drawtype);//intm_drawtype
voidDrawing(CDC*pDC);
voidVCLine(CDC*pDC);//
voidSerialize(CArchive&ar);
};
classCCircle:
publicCShape
{
protected:
intm_drawtype;//
POINTm_pFP,m_pSP;
COLORREFm_FColor;
intm_FFlag,m_HFlag,m_HPattern;
CCircle(){}
DECLARE_SERIAL(CCircle)
public:
CCircle(POINTpFP,POINTpSP,intWidth,intStyle,COLORREFColor,
intm_FFlag,COLORREFFColor,intHFlag,intHPattern,intm_drawtype);//
voidDrawing(CDC*pDC);
voidSerialize(CArchive&ar);
voidVCCircle(CDC*pDC);//
};
classCEllipse:
publicCShape
{
protected:
intm_drawtype;//
POINTm_pFP,m_pSP;
COLORREFm_FColor;
intm_FFlag,m_HFlag,m_HPattern;
CEllipse(){}
DECLARE_SERIAL(CEllipse)
public:
CEllipse(POINTpFP,POINTpSP,intWidth,intStyle,COLORREFColor,
intm_FFlag,COLORREFFColor,intHFlag,intHPattern,intm_drawtype);
voidDrawing(CDC*pDC);
voidSerialize(CArchive&ar);
voidVCEllipse(CDC*pDC);
};
//实现文件shape.cpp
//CShape
IMPLEMENT_SERIAL(CShape,CObject,1)
voidCShape:
:
Serialize(CArchive&ar)
{
if(ar.IsStoring())
ar<else
ar>>m_pColor>>m_pWidth>>m_pStyle;
}
//CLine
IMPLEMENT_SERIAL(CLine,CShape,1)
CLine:
:
CLine(POINTpStart,POINTpEnd,intWidth,intStyle,COLORREFColor,intdrawtype)
{
m_pStart=pStart;m_pEnd=pEnd;
m_pWidth=Width;m_pStyle=Style;m_pColor=Color;
m_drawtype=drawtype;
}
voidCLine:
:
Drawing(CDC*pDC)
{
switch(m_drawtype)//
{
case0:
VCLine(pDC);
break;
case1:
DDALine(m_pStart.x,m_pStart.y,m_pEnd.x,m_pEnd.y,m_pColor,m_pWidth,pDC);
case2:
MidpointLine(m_pStart.x,m_pStart.y,m_pEnd.x,m_pEnd.y,m_pColor,m_pWidth,pDC);
break;
case3:
BresenhamLine(m_pStart.x,m_pStart.y,m_pEnd.x,m_pEnd.y,m_pColor,m_pWidth,pDC)
break;
}
}
//下面是VC的画线函数
voidCLine:
:
VCLine(CDC*pDC)
{
CPenNewPen,*pOldPen;
NewPen.CreatePen(m_pStyle,m_pWidth,m_pColor);
pOldPen=pDC->SelectObject(&NewPen);
pDC->MoveTo(m_pStart);
pDC->LineTo(m_pEnd);
pDC->SelectObject(pOldPen);
}
voidCLine:
:
Serialize(CArchive&ar)
{
if(ar.IsStoring())
ar<<else
ar>>m_pStart.x>>m_pStart.y
>>m_pEnd.x>>m_pEnd.y>>m_drawtype;//
CShape:
:
Serialize(ar);
}
//CCircle
IMPLEMENT_SERIAL(CCircle,CShape,1)
CCircle:
:
CCircle(POINTpFP,POINTpSP,intWidth,intStyle,COLORREFColor,
intFFlag,COLORREFFColor,intHFlag,intHPattern,intdrawtype)
{
m_pFP=pFP;m_pSP=pSP;
m_pWidth=Width;m_pStyle=Style;m_pColor=Color;
m_FFlag=FFlag;m_FColor=FColor;
m_HFlag=HFlag;m_HPattern=HPattern;
m_drawtype=drawtype;//
}
voidCCircle:
:
Drawing(CDC*pDC)
{
intxc,yc,r;
switch(m_drawtype)
{
case0:
VCCircle(pDC);
break;
case1:
xc=m_pFP.x;yc=m_pFP.y;
r=sqrt((m_pFP.x-m_pSP.x)*(m_pFP.x-m_pSP.x)+(m_pFP.y-m_pSP.y)*(m_pFP.y-m_pSP.y));
MidCircle(xc,yc,r,m_pColor,m_pWidth,pDC);
break;
case2:
xc=m_pFP.x;yc=m_pFP.y;
r=sqrt((m_pFP.x-m_pSP.x)*(m_pFP.x-m_pSP.x)+(m_pFP.y-m_pSP.y)*(m_pFP.y-m_pSP.y));
BresenhamCircle(xc,yc,r,m_pColor,m_pWidth,pDC);
break;
}
}
//下面是VC画圆函数
voidCCircle:
:
VCCircle(CDC*pDC)
{
CPenNewPen,*pOldPen;
NewPen.CreatePen(m_pStyle,m_pWidth,m_pColor);
pOldPen=pDC->SelectObject(&NewPen);
pDC->SelectStockObject(NULL_BRUSH);
CBrushNewBrush,*pOldBrush;
if(m_FFlag)
{
if(m_HFlag==0)
NewBrush.CreateSolidBrush(m_FColor);
else
NewBrush.CreateHatchBrush(m_HPattern,m_FColor);
pOldBrush=pDC->SelectObject(&NewBrush);
}
pDC->Ellipse(m_pFP.x,m_pFP.y,m_pSP.x,m_pSP.y);
pDC->SelectObject(pOldPen);
if(m_FFlag)pDC->SelectObject(pOldBrush);//
}
voidCCircle:
:
Serialize(CArchive&ar)
{
if(ar.IsStoring())
ar<<else
ar>>m_pFP.x>>m_pFP.y>>m_pSP.x>>m_pSP.y
>>m_FFlag>>m_FColor>>m_HFlag>>m_HPattern>>m_drawtype;
CShape:
:
Serialize(ar);
}
//CEllipse
IMPLEMENT_SERIAL(CEllipse,CShape,1)
CEllipse:
:
CEllipse(POINTpFP,POINTpSP,intWidth,intStyle,COLORREFColor,
intFFlag,COLORREFFColor,intHFlag,intHPattern,intdrawtype)
{
m_pFP=pFP;m_pSP=pSP;
m_pWidth=Width;m_pStyle=Style;m_pColor=Color;
m_FFlag=FFlag;m_FColor=FColor;
m_HFlag=HFlag;m_HPattern=HPattern;
m_drawtype=drawtype;//
}
voidCEllipse:
:
Drawing(CDC*pDC)
{
intxc,yc,a,b;
switch(m_drawtype)
{
case0:
VCEllipse(pDC);break;
case1:
xc=(m_pFP.x+m_pSP.x)/2;
yc=(m_pFP.y+m_pSP.y)/2;
a=abs(m_pFP.x-m_pSP.x)/2;
b=abs(m_pFP.y-m_pSP.y)/2;
MidEllipse(xc,yc,a,b,m_pColor,m_pWidth,pDC);
break;
}
}
//下面是VC的画椭圆函数
voidCEllipse:
:
VCEllipse(CDC*pDC)
{
//
CPenNewPen,*pOldPen;
NewPen.CreatePen(m_pStyle,m_pWidth,m_pColor);
pOldPen=pDC->SelectObject(&NewPen);
pDC->SelectStockObject(NULL_BRUSH);
CBrushNewBrush,*pOldBrush;
if(m_FFlag)
{
if(m_HFlag==0)
NewBrush.CreateSolidBrush(m_FColor);
else
NewBrush.CreateHatchBrush(m_HPattern,m_FColor);
pOldBrush=pDC->SelectObject(&NewBrush);
}
pDC->Ellipse(m_pFP.x,m_pFP.y,m_pSP.x,m_pSP.y);
pDC->SelectObject(pOldPen);
if(m_FFlag)pDC->SelectObject(pOldBrush);
}
voidCEllipse:
:
Serialize(CArchive&ar)
{
if(ar.IsStoring())
ar<<else
ar>>m_pFP.x>>m_pFP.y>>m_pSP.x>>m_pSP.y
>>m_FFlag>>m_FColor>>m_HFlag>>m_HPattern>>m_drawtype;
CShape:
:
Serialize(ar);
}
//BasicGraph.cpp
//下面是Bresenham的画线函数,请参照上面DDA和中点画线法,把它补充完整。
voidBresenhamLine(intx1,inty1,intx2,inty2,COLORREFcolor,intwidth,CDC*pDC)
{
intx,y,dx,dy,dk,i,s1,s2,temp,swap;
//请在此处补充代码
dx=abs(x2-x1);dy=abs(y2-y1);
x=x1;y=y1;
s1=Sign(x2-x1);s2=Sign(y2-y1);
if(dy>dx){temp=dx;dx=dy;dy=temp;swap=1;}
elseswap=0;
dk=2*dy-dx;
for(i=1;i<=dx;i++)
{
LineWidthProcess(x,y,abs(x2-x1),abs(y2-y1),color,width,pDC);
if(swap==1)y=y+s2;
elsex=x+s1;
dk=dk+2*dy;
if(dk>=0)
{
if(swap==1)x=x+s1;
elsey=y+s2;
dk-=2*dx;
}
}
intSign(intx)
{
if(x<0)return-1;
elseif(x==0)return0;
elsereturn1;
}
//下面是Bresenham画圆函数,请参照上面中点画圆函数,把它补充完整。
voidBresenhamCircle(intxc,intyc,intr,COLORREFcolor,intwidth,CDC*pDC)
{
intx,y,d;
x=0;y=r;
//请在此处补充代码
d=3-2*r;
while(x{
WholeCircle(xc,yc,x,y,color,width,pDC);
if(d<0)
d=d+4*x+6;
else
{
d=d+4*(x-y)+10;
y--;
}
x++;
}
if(x==y)WholeCircle(xc,yc,x,y,color,width,pDC);
}
//下面是中点画椭圆函数,请把它补充完整
voidMidEllipse(intxc,intyc,inta,intb,COLORREFcolor,intwidth,CDC*pDC)
{
intaa=a*a,bb=b*b;
inttwoaa=2*aa,twobb=2*bb;
intx=0,y=b;
intdx=0,dy=twoaa*y;
intd=(int)(bb+aa*(-b+0.25)+0.5);
WholeEllipse(xc,yc,x,y,color,width,pDC);
////请在此处补充代码
while(dx{x++;dx+=twobb;
if(d<0)d+=bb+dx;
else{y--;dy-=twoaa;d+=bb+dx-dy;}
WholeEllipse(xc,yc,x,y,color,width,pDC);
}
d=(int)(bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb+0.5);
while(y>0)
{y--;dy-=twoaa;
if(d>0)d+=aa-dy;
else{x++;dx+=twobb;d+=aa-dy+dx;}
WholeEllipse(xc,yc,x,y,color,width,pDC);
}
}
三、实验结果
四、实验总结
在本次实验中,我通过对课堂上所学知识的应用,学到了很多实践中的知识。
并且加深了我对课本知识的理解和认识,在实验过程中,更容易记忆和深入理解多种算法实现基本图形