当前位置:文档之家› 图形学实验报告

图形学实验报告

山东建筑大学测绘地理信息学院实验报告(2016—2017学年第一学期)课程:计算机图形学专业:地理信息科学班级:地信141学生姓名:王俊凝学号:20140113010指实验一直线生成算法设计一、实验目的掌握基本图形元素直线的生成算法,利用编程语言C分别实现直线和圆的绘制算法。

二、实验任务在TurboC环境下开发出绘制直线和圆的程序。

三、实验仪器设备计算机。

四、实验方法与步骤1 运行TurboC编程环境。

2 编写Bresenham直线绘制算法的函数并进行测试。

3 编写中点圆绘制算法的函数并进行测试。

4 增加函数参数,实现直线颜色的设置。

提示:1. 编程时可分别针对直线和圆的绘制算法,设计相应的函数,例如void drawline(…)和void drawcircle(…),直线的两个端点可作为drawline的参数,圆的圆心和半径可作为drawcircle的参数。

2. 使用C语言编写一个结构体类型用来表示一个点,结构体由两个成员构成,x和y。

这样,在向函数传入参数时,可使用两个点类型来传参。

定义方法为:typedef struct{int x;int y;}pt2;此处,pt2就是定义的一个新的结构体数据类型,之后就可用pt2来定义其他变量,具体用法见程序模板。

3. 在main函数中,分别调用以上函数,并传入不同的参数,实现对直线的绘制。

4. 线的颜色也可作为参数传入,参数可采用TurboC语言中的预设颜色值,具体参见TurboC图形函数。

五、注意事项1 代码要求正确运行,直线和圆的位置应当为参数,实现可配置。

2 程序提交.c源文件,函数前和关键代码中增加注释。

程序模板#include <stdio.h>#include <graphics.h>typedef struct{int x;int y;}pt2;/*declare your drawing functions.*/void drawline(pt2 startpt,pt2 endpt,int color);void drawcircle(pt2 centerpt,int radius,int color);void circlePlotPoints(pt2 centerpt,int x,int y,int color);int main(){int color,radius;pt2 startpt,endpt,centerpt;/*initialize graphics driver.*/int gdriver=VGA;int gmode=VGAHI;initgraph(&gdriver, &gmode, "C:\\TC20\\BGI");/*start drawing.*/printf("Press enter to start drawing.");getchar();/*invoke your drawing functions one by one.*/ startpt.x=10;startpt.y=10;endpt.x=300;endpt.y=250;color=4;drawline(startpt,endpt,color);centerpt.x=200;centerpt.y=180;radius=145;color=8;drawcircle(centerpt,radius,color);/*end drawing.*/printf("Drawing is done, press enter to exit."); getchar();/*close graphics driver.*/closegraph();}/** draw line* startpt: the start point of the line* endpt: the end point of the line* color: the color of the line*/void drawline(pt2 startpt,pt2 endpt,int color){int dx=abs(startpt.x-endpt.x), dy=abs(startpt.y-endpt.y);int p=2*dy-dx;int twoDy=2*dy,twoDyDx=2*(dy-dx);int x,y,xEnd;if(startpt.x>endpt.x){x=endpt.x;y=endpt.y;xEnd=startpt.x;}else{x=startpt.x;y=startpt.y;xEnd=endpt.x;}putpixel(x,y,color);while(x<xEnd){x++;if(p<0)p+=twoDy;else{y++;p+=twoDyDx;}putpixel(x,y,color);}}/** draw circle* centerpt: the center of the circle* radius: the radius of the circle* color: the color of the circle*/void drawcircle(pt2 centerpt,int radius,int color) {int x=0;int y=radius;int p=1-radius;circlePlotPoints(centerpt,x,y,color);while(x<y){x++;if(p<0)p+=2*x+1;else{y--;p+=2*(x-y)+1;}circlePlotPoints(centerpt,x,y,color);}}void circlePlotPoints(pt2 centerpt,int x,int y,int color) {putpixel(centerpt.x+x,centerpt.y+y,color);putpixel(centerpt.x-x,centerpt.y+y,color);putpixel(centerpt.x+x,centerpt.y-y,color);putpixel(centerpt.x-x,centerpt.y-y,color);putpixel(centerpt.x+y,centerpt.y+x,color);putpixel(centerpt.x-y,centerpt.y+x,color);putpixel(centerpt.x+y,centerpt.y-x,color);putpixel(centerpt.x-y,centerpt.y-x,color);}运行结果实验二多边形区域填充算法设计一、实验目的掌握多边形图形元素的填充算法,利用编程语言C实现多边形的区域填充算法。

二、实验任务在TurboC环境下开发出多边形区域填充的程序。

三、实验仪器设备计算机。

四、实验方法与步骤1 运行TurboC编程环境。

2 编写基于四连通区域的种子填充算法。

3 编写基于八连通区域的种子填充算法。

4 对填充函数进行测试。

提示:1. 编程中需要使用数据结构中的栈,可利用C语言线编写一个对应的数据结构,然后在填充函数中进行使用。

以下过程是对四连通区域填充过程的分解,可作为算法实现的参考。

2. 可将两种填充算法分别设计一个函数,例如RegionFill4(…)、RegionFill8(…),函数中间是作为参数输入的区域。

3. 区域的表示可以采用一维数组形式,数组类型是点类型(实验一中定义的结构体),依次将构成多边形的每个顶点的坐标创建点变量,并保存到一个一维数组中进行参数传递。

4. 在main函数中来生成一定的区域,并生成表示区域的一维数组,然后通过调用填充算法函数,完成区域填充。

#include <stdio.h>#include <graphics.h>void boundaryFill4(int x,int y,int fill,int boundary){int current;current=getpixel(x,y);if((current!=boundary)&&(current!=fill)){putpixel(x,y,fill);boundaryFill4(x+1,y,fill,boundary);boundaryFill4(x-1,y,fill,boundary);boundaryFill4(x,y+1,fill,boundary);boundaryFill4(x,y-1,fill,boundary);}}int main(){/*initialize graphics driver.*/int gdriver=VGA;int gmode=VGAHI;initgraph(&gdriver, &gmode, "C:\\TC20\\BGI");moveto(0,0);lineto(120,100);lineto(50,80);lineto(0,0);boundaryFill4(50,78,2,15);/*end drawing.*/printf("Drawing is done, press enter to exit.");getchar();/*close graphics driver.*/closegraph();}运行结果五、注意事项1 代码要求正确运行,填充颜色设置为函数的参数,实现可配置。

2 程序提交.c源文件,函数前和关键代码中增加注释。

实验三图形变换算法设计一、实验目的掌握图形变换的类型和算法,利用编程语言C,实现二维图形的平移、缩放、旋转和仿射变换算法。

二、实验任务在TurboC环境下开发出二维图形的平移、缩放、旋转和仿射变换算法。

三、实验仪器设备计算机。

四、实验方法与步骤1 运行TurboC编程环境。

2 编写二维图形的平移算法。

3 编写二维图形的固定缩放算法。

4 编写二维图形的固定点旋转算法。

5 编写二维图形的固定点缩放算法。

6 编写二维图形的复合变换算法,实现图形的平移、旋转和缩放算法。

7 编写二维图形的仿射变换算法。

提示:1. 针对每种变换算法,编写一个函数,并设计相应的参数。

参数可以使用实验一编写的点类型来构造。

2. 为更好地体现变换效果,在试验一编写的直线绘制算法基础上,再编写一个绘制多边形的算法函数,该函数只是将传入的多边形顶点依次进行直线绘制即可完成。

2. 在main函数中,通过组合调用以上函数,实现对二维多边形图形的几何变换。

相关主题