资源预览内容
第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
第9页 / 共17页
第10页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
华北水利水电大学 计算机图形学 实验报告2017-2018学年 第一学期 2014级 计算机科学与技术 专业 指导老师 曹源昊 班级 2014157 学号 201415717 姓名 李卫朋 实验三、画圆与凸多边形填充算法1. 实验目的练习直线和圆的光栅扫描线算法,多边形的扫描转换算法。2. 实验内容和要求按要求完成以下三个作业。提交纸质实验报告,同时提交实验报告和源代码的电子版。(I). 利用Bresenham直线扫描算法绘制任意直线段。输入为起点坐标(x0,y0)和终点坐标(x1,y1)以及线宽w,利用Bresenham算法计算离散的近似像素点,并在OpenGL窗口中绘制该线段。要求绘制至少五条线段,具有不同的斜率,不同的起点和终点,不同的线宽。不允许直接调用GL_LINES来实现。(II). 利用中点画圆算法绘制圆。输入为圆心(xc,yc)和圆的的半径r,利用中点画圆算法计算离散的近似像素点,并在OpenGL窗口中绘制。要求绘制至少四个圆,构成一个图案,比如奥迪车标或五环。(III). 实现多边形的扫描转换算法。输入一个凸多边形的顶点序列,利用活性边表计算每条扫描线上位于多边形内部的像素,并填充上一个新颜色,最终达到填充多边形内部的目的。建议:为了实现坐标点和像素的一一对应,建议坐标轴的范围和窗口像素宽高一致,比如:glutInitWindowSize(800, 600);/像素宽800,高600坐标系设定为:gluOrtho2D(-400, 400, -300, 300);/坐标轴x方向宽为800,y方向高为6003. 算法描述使用OpenGL进行画图,采用VS编程环境。4. 源程序代码(1)/ bresenham直线.cpp : 定义控制台应用程序的入口点。/#include stdio.h#include stdafx.h#include glut.h#include stdlib.h#include math.h#include iostreamusing namespace std;GLsizei winWidth = 400, winHeight = 300; / 屏幕显示宽高.int a100,b100,c100,d100,n=-1;void init( )glClearColor(1.0, 1.0, 1.0, 1.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0, 200.0, 0.0, 150.0);void lineBres(int x0,int y0,int xEnd,int yEnd) int dx = abs(xEnd - x0),dy = abs(yEnd - y0); int p = 2*dy - dx; int twoDy = 2*dy, twoDyMinusDx = 2*(dy - dx); int x,y;if(x0 xEnd) x = xEnd; y = yEnd; xEnd = x0;else x = x0; y = y0; glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); while(xxEnd) x+;if(p 0)p += twoDy;elsey+;p += twoDyMinusDx;glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); void displayFcn() glColor3f(1.0, 0.0, 0.0); for(int i=0;i=n;i+) lineBres(ai,bi,ci,di);glFlush();void winReshpeFcn(GLint newWidth, GLint newHeight)glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight);glClear(GL_COLOR_BUFFER_BIT);winWidth = newWidth;winHeight = newHeight;int _tmain(int argc, char* argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowPosition(100, 100);glutInitWindowSize(winWidth, winHeight);glutCreateWindow(bresenham直线);init();int h=1;while(h=1)printf(输入起点和终点的坐标);n+;scanf_s(%d %d %d %d,&an,&bn,&cn,&dn);glutDisplayFunc(displayFcn);printf(继续输入请按1,退出请按0);scanf_s(%d,&h);glutReshapeFunc(winReshpeFcn);glutMainLoop();return 0;(2)/ 中点画圆法.cpp : 定义控制台应用程序的入口点。/#include stdafx.h#include glut.h#include stdlib.h#include math.h#include iostreamusing namespace std;const GLdouble twoPi = 6.283185;GLsizei winWidth = 400, winHeight = 300; / 屏幕显示宽高.class screenPt public:screenPt()x = y = 0;GLint x, y;void setCoords(GLint xCoordValue, GLint yCorrdValue)x = xCoordValue;y = yCorrdValue;GLint getx() constreturn x;GLint gety() constreturn y;void incrementx()x+;void incrementy()y-;void init( )glClearColor(1.0, 1.0, 1.0, 1.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0, 200.0, 0.0, 150.0);void setPixel(GLint xCoord, GLint yCoord)glBegin(GL_POINTS);glVertex2i(xCoord, yCoord);glEnd();void circleMidpoint(GLint xc, GLint yc, GLint radius)screenPt circPt;GLint p = 1 - radius;/中点参数初值circPt.setCoords(0, radius);void circlePlotPoints(GLint, GLint, screenPt);circlePlotPoints(xc, yc, circPt);while (circPt.getx() circPt.gety()circPt.incrementx();if (p 0)p += 2 * circPt.getx() + 1;elsecircPt.incrementy();p += 2 * (circPt.getx() - circPt.gety() + 1;circlePlotPoints(xc, yc, circPt);void circlePlotPoints(GLint xc, GLint yc, screenPt circPt)setPixel(xc + circPt.getx(), yc + circPt.gety();setPixel(xc - circPt.getx(), yc + circPt.gety();setPixel(xc + circPt.getx(), yc - circPt.gety();setPixel(xc - circPt.getx(), yc - circPt.gety();setPixel(xc + circPt.gety(), yc + circPt.getx();setPixel(xc - circPt.gety(), yc + circPt.getx();setPixel(xc + circPt.gety(), yc - circPt.getx();setPixel(xc - circPt.gety(), yc - circPt.getx();void pieChart( )screenPt circCtr, piePt;GLint radius = winWidth / 16; circCtr.x = winWidth / 4; circCtr.y = winHeight / 1.5;circleMidpoint(circCtr.x, circCtr.y, radius); / 调用中点画圆方法 glColor3f(0.0, 0.0, 1.0); circleMidpoint(circCtr.x+60, circCtr.y, radius);glColor3f(0.0, 0.0, 0.0); circleMidpoint(circCtr.x+120, circCtr.y, radius); glColor3f(1.0, 1.0, 0.0); circleMidpoint(circCtr.x+30, circCtr.y-30, radius);glColor3f(0.0, 1.0, 0.0); circleMidpoint(circCtr.x+90, circCtr.y-30, radius);void d
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号