计算机图形学实验指导(含源码附报告模板)
计算机图形学实验指导
目录
实验1 直线的绘制2
实验2 圆和椭圆的绘制4
实验3 图形填充7
实验4 二维图形几何变换10
实验5 二维图形裁剪13
实验6 曲线生成算法的实现18
附录:实验报告模板20
实验1 直线的绘制直线直线用法或Besenham法实现直线的绘制# include
# include
void DDALine(int x0,int y0,int x1,int y1,int color)
{
int dx,dy,epsl,k;
float x,y,xIncre,yIncre;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
if(abs(dx)>abs(dy))
epsl=abs(dx);
else
epsl=abs(dy);
xIncre=(float)dx/(float)epsl;
yIncre=(float)dy/(float)epsl;
for(k=0;k<=epsl;k++)
{
putpixel((int)(x+0.5),(int)(y+0.5),4);
x+=xIncre;
y+=yIncre;
}
}
main(){
int gdriver ,gmode ;
gdriver = DETECT;
initgraph(&gdriver , &gmode ,"C:\\TC20\\BGI");
DDALine(0,0,35,26,4);
getch ( );
closegraph ( );
}
Bresenham:
#include
#include
void BresenhamLine(int x0,int y0,int x1,int y1,int color)
{
int x,y,dx,dy,e;
dx=x1-x0;
dy=y1-y0;
e=-dx;x=x0;y=y0;
while(x<=x1){
putpixel(x,y,color);
x++;
e=e+2*dy;
if(e>0){
y++;
e=e-2*dx;
}
}
}
main(){
int gdriver ,gmode ;
gdriver = DETECT;
initgraph(&gdriver , &gmode ,"c:\\TC20\\BGI");
BresenhamLine(0, 0 , 120, 200,5 );
getch ( );
closegraph ( );
}
实验2 圆和椭圆的绘制用Besenham)算法实现的绘制法#include
#include
void CirclePoint(int x,int y,int color)
{
putpixel(x+100,y+100,color);
putpixel(-x+100,y+100,color);
putpixel(x+100,-y+100,color);
putpixel(-x+100,-y+100,color);
putpixel(y+100,x+100,color);
putpixel(y+100,-x+100,color);
putpixel(-y+100,x+100,color);
putpixel(-y+100,-x+100,color);
}
void MidBresenhamCircle(int r,int color)
{
int x,y,d;
x=0;y=r;d=1-r;
while(x<=y){
CirclePoint(x,y,color);
if(d<0) d+=2*x+3;
else{
d+=2*(x-y)+5;
y--;
}
x++;
}
}
main( )
{
int gdriver ,gmode ;
gdriver = DETECT;
initgraph(&gdriver , &gmode ,"c:\\tc20\\bgi" );
MidBresenhamCircle(50,6);
getch ( );
closegraph ( );
}
实验截图:
椭圆:
#include