opengl鼠标绘制直线及圆图元算法.ppt

上传人:本田雅阁 文档编号:2202363 上传时间:2019-03-03 格式:PPT 页数:12 大小:501.51KB
返回 下载 相关 举报
opengl鼠标绘制直线及圆图元算法.ppt_第1页
第1页 / 共12页
opengl鼠标绘制直线及圆图元算法.ppt_第2页
第2页 / 共12页
opengl鼠标绘制直线及圆图元算法.ppt_第3页
第3页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《opengl鼠标绘制直线及圆图元算法.ppt》由会员分享,可在线阅读,更多相关《opengl鼠标绘制直线及圆图元算法.ppt(12页珍藏版)》请在三一文库上搜索。

1、opengl鼠标绘制直线及圆,opengl鼠标绘制直线及圆介绍,图形学的重要算法使用鼠标左键、右键操作。 opengl鼠标绘制包括直线DDA算法,Bresenham算法,OpenGL画法,以及圆,椭圆的画法。 此外,此程序还可用于三角形的平移、旋转、缩放操作。,源代码 头文件介绍,#include 测试传入表达式的真假值 #include 包含数学函数 #include 包含输入输出流 #include 包含数据结构和算法的模板 #include 将解压opengl文件放入程序中 using namespace std; 配合#include 注释:可以将#include 替换成#includ

2、e ,typedef vector list; vector ls, cs; int old_x, old_y, new_x, new_y, cx, cy, cr; int mouse_type; /* breseman画线算法 */ void line(int x1, int y1, int x2, int y2) glBegin(GL_POINTS); if (x1=x2 if (dx=dy) ,利用breseman算法,进行画直线的子文件编辑。,定义:直线的终点起点,圆的圆心和半径。,/* 中点圆算法 */ void circle(int cx, int cy, int r) int x

3、=0, y=r; float p=1.25-r; while (x=y) glBegin(GL_POINTS); glVertex2i(cx+x, cy+y); glVertex2i(cx-x, cy+y); glVertex2i(cx-x, cy-y); glVertex2i(cx+x, cy-y); glVertex2i(cx+y, cy+x); glVertex2i(cx-y, cy+x); glVertex2i(cx-y, cy-x); glVertex2i(cx+y, cy-x); glEnd(); if (p0) +x; p+=2*x+1; else p+=2*x-2*y+5; +

4、x; -y; ,进行画圆的子文件编辑。,void draw() int i; for (i=0; ils.size(); +i) line(lsi0, lsi1, lsi2, lsi3); for (i=0; ics.size(); +i) circle(csi0, csi1, csi2); if(mouse_type = 1) line(old_x, old_y, new_x, new_y); if(mouse_type = 3) circle(cx, cy, cr); void display() glClear(GL_COLOR_BUFFER_BIT); glColor3i(0, 0,

5、0); draw(); glutSwapBuffers(); ,进行画圆操作的编辑,展示画出的圆,void mouseclick( int button, int state, int x, int y ) cout“(“x“,“y“)“; switch ( button ) case GLUT_LEFT_BUTTON: if ( state = GLUT_DOWN ) cout“左键被按下“; old_x = x; old_y = 600-y; mouse_type = 1; else if ( state = GLUT_UP ) cout“左键被松开“; cout“右键被按下“; cx =

6、 x; cy = 600-y; mouse_type = 3; else if ( state = GLUT_UP ) cout“右键被松开“; ,定义鼠标:按下鼠标左键即调用画线算法;按下鼠标右键即调用画圆算法。,void init() glClearColor(1.0, 1.0, 1.0, 0.0); 背景颜色 glMatrixMode(GL_PROJECTION); gluOrtho2D(-1.0, 600, 0.0, 601); 改变数值画椭圆 int main(int argc, char* argv) glutInit( ,背景颜色的调试,可变数值画椭圆,运行结果,The end,Thanks,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1