实验三、画圆与凸多边形填充算法.doc

上传人:小** 文档编号:90992526 上传时间:2019-06-20 格式:DOC 页数:17 大小:76.66KB
返回 下载 相关 举报
实验三、画圆与凸多边形填充算法.doc_第1页
第1页 / 共17页
实验三、画圆与凸多边形填充算法.doc_第2页
第2页 / 共17页
实验三、画圆与凸多边形填充算法.doc_第3页
第3页 / 共17页
实验三、画圆与凸多边形填充算法.doc_第4页
第4页 / 共17页
实验三、画圆与凸多边形填充算法.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《实验三、画圆与凸多边形填充算法.doc》由会员分享,可在线阅读,更多相关《实验三、画圆与凸多边形填充算法.doc(17页珍藏版)》请在金锄头文库上搜索。

1、华北水利水电大学 计算机图形学 实验报告2017-2018学年 第一学期 2014级 计算机科学与技术 专业 指导老师 曹源昊 班级 2014157 学号 201415717 姓名 李卫朋 实验三、画圆与凸多边形填充算法1. 实验目的练习直线和圆的光栅扫描线算法,多边形的扫描转换算法。2. 实验内容和要求按要求完成以下三个作业。提交纸质实验报告,同时提交实验报告和源代码的电子版。(I). 利用Bresenham直线扫描算法绘制任意直线段。输入为起点坐标(x0,y0)和终点坐标(x1,y1)以及线宽w,利用Bresenham算法计算离散的近似像素点,并在OpenGL窗口中绘制该线段。要求绘制至少

2、五条线段,具有不同的斜率,不同的起点和终点,不同的线宽。不允许直接调用GL_LINES来实现。(II). 利用中点画圆算法绘制圆。输入为圆心(xc,yc)和圆的的半径r,利用中点画圆算法计算离散的近似像素点,并在OpenGL窗口中绘制。要求绘制至少四个圆,构成一个图案,比如奥迪车标或五环。(III). 实现多边形的扫描转换算法。输入一个凸多边形的顶点序列,利用活性边表计算每条扫描线上位于多边形内部的像素,并填充上一个新颜色,最终达到填充多边形内部的目的。建议:为了实现坐标点和像素的一一对应,建议坐标轴的范围和窗口像素宽高一致,比如:glutInitWindowSize(800, 600);/像

3、素宽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 = 3

4、00; / 屏幕显示宽高.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

5、- 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+) lineBre

6、s(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,

7、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

8、);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:screenP

9、t()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.

10、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,

11、 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.ge

12、ty();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();setPixe

13、l(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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号