计算机图形学-直线裁剪(Cohen-Sutherland编码裁剪算法)
来源:互联网 发布:好玩的网络手游 编辑:程序博客网 时间:2024/05/22 06:53
键盘输入c裁剪,输入r恢复
#include<gl/glut.h>#include<stdio.h>#include<stdlib.h>#define LEFT_EDGE 1#define RIGHT_EDGE 2#define BOTTOM_EDGE 4#define TOP_EDGE 8void LineGL(int x0, int y0, int x1, int y1){glBegin(GL_LINES);glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x0, y0);glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(x1, y1); glEnd();}struct Rectangle{float xmin, xmax, ymin, ymax;};Rectangle rect;int x0, y0, x1, y1;int CompCode(int x, int y, Rectangle rect){int code = 0x00;if (y < rect.ymin)code = code | 4;if (y>rect.ymax)code = code | 8;if (x>rect.xmax)code = code | 2;if (x<rect.xmin)code = code | 1;return code;}int cohensutherlandlineclip(Rectangle rect, int &x0, int &y0, int &x1, int &y1){int accept, done;float x, y;accept = 0;done = 0;int code0, code1, codeout;code0 = CompCode(x0, y0, rect);code1 = CompCode(x1, y1, rect);do{if (!(code0 | code1)){//整条线段在窗口内accept = 1;//取之done = 1;}else if (code0 & code1)//两个端点同在窗口一侧,弃之done = 1;else{//线段与窗口存在交点if (code0 != 0)codeout = code0;elsecodeout = code1; //求交点if (codeout&LEFT_EDGE){y = y0 + (y1 - y0)*(rect.xmin - x0) / (x1 - x0);x = (float)rect.xmin;}else if (codeout&RIGHT_EDGE){y = y0 + (y1 - y0)*(rect.xmax - x0) / (x1 - x0);x = (float)rect.xmax;}else if (codeout&BOTTOM_EDGE){x = x0 + (x1 - x0)*(rect.ymin - y0) / (y1 - y0);y = (float)rect.ymin;}else if (codeout&TOP_EDGE){x = x0 + (x1 - x0)*(rect.ymax - y0) / (y1 - y0);y = (float)rect.ymax;}//舍弃在窗口外的部分线段if (codeout == code0){x0 = x; y0 = y;code0 = CompCode(x0, y0, rect);}else{x1 = x; y1 = y;code1 = CompCode(x1, y1, rect);}}} while (!done);if (accept)LineGL(x0, y0, x1, y1);else{x0 = 0; y = 0; x1 = 0; y1 = 0;LineGL(x0, y0, x1, y1);}return accept;}void myDisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f, 0.0f, 0.0f);glRectf(rect.xmin, rect.ymin, rect.xmax, rect.ymax);LineGL(x0, y0, x1, y1);glFlush();}void Init(){glClearColor(0.0, 0.0, 0.0, 0.0);glShadeModel(GL_FLAT);rect.xmin = 100;rect.xmax = 300;rect.ymin = 100;rect.ymax = 300;x0 = 450, y0 = 0, x1 = 0, y1 = 450;printf("Press key 'c' to Clip!\nPress key 'r' to Restore!\n");}void Reshape(int w, int h){glViewport(0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);}void keyboard(unsigned char key, int x, int y){switch (key){case 'c':cohensutherlandlineclip(rect, x0, y0, x1, y1);glutPostRedisplay();break;case 'r':Init();glutPostRedisplay();break;case 'x':exit(0);break;default:break;}}int main(int argc, char *argv[]){glutInit(&argc, argv);glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);glutInitWindowPosition(100, 100);glutInitWindowSize(640,480);glutCreateWindow("Hello World!");Init();glutDisplayFunc(myDisplay);glutReshapeFunc(Reshape);glutKeyboardFunc(keyboard);glutMainLoop();return 0;}
阅读全文
0 0
- 计算机图形学-直线裁剪(Cohen-Sutherland编码裁剪算法)
- 计算机图形学 - 线段裁剪 - Cohen Sutherland算法
- [OpenGL]计算机图形学:直线裁剪算法中Cohen-Sutherland算法和Liang-Barsky算法
- opengl 直线裁剪Cohen-Sutherland算法
- Cohen-Sutherland直线段的裁剪算法
- Cohen-Sutherland裁剪算法
- Cohen-Sutherland裁剪算法
- Cohen-SutherLand 裁剪算法 (vc++)
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland算法线段裁剪
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland算法裁剪线段
- OPENGL—编码裁剪(Cohen-Sutherland)法裁剪图
- 直线矩形视口裁剪算法 Cohen–Sutherland algorithm
- Tubor C 直线裁剪算法 Cohen-Sutherland算法 (可运行代码)
- 计算机图形学 学习笔记(四):直线裁剪算法:Cohen-Suther land,中点分割法,Liang-Barsky
- Cohen Sutherland线段裁剪算法(C#实现)
- 算法学习(3):动态规划DP
- JQuerymobile实例源代码
- 一种微信直播H5直播与存储回放的HLS摄像机方案
- 16位遥感图像转8位显示 %2线性拉伸
- leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee
- 计算机图形学-直线裁剪(Cohen-Sutherland编码裁剪算法)
- GPX转换为GP5
- MYSQL 子查询返回多列显示
- 重识计算机【一】——分类
- Understanding the JVM(四)对象的访问定位
- 利用matlab做回归分析
- ubuntu遇到了 dpkg was interrupted, you must manually run 'dpkg..的问题
- 机器人这步生死棋,英特尔是如何走的
- 软银310亿美元收购芯片巨头ARM 或与物联网布局有关