《opengl鼠标绘制直线及圆图元算法源代码》由会员分享,可在线阅读,更多相关《opengl鼠标绘制直线及圆图元算法源代码(5页珍藏版)》请在金锄头文库上搜索。
1、#include #include #include #include #include using namespace std;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 & y1=y2) glVertex2i(x1, y1);return;if (x1x2) swap(x1,
2、 x2);swap(y1, y2);glVertex2d(x1, y1);int dy=abs(y2-y1);int dx=abs(x2-x1);if (dx=dy) int incy=(dy!=0?dy/(y2-y1):0);int p=2*dy-dx;int dy2=2*dy;int dd2=2*(dy-dx);int x, y=y1;for (x=x1+1; xy2) swap(x1, x2);swap(y1, y2);int incx=(dx!=0?dx/(x2-x1):0);int p=2*dx-dy;int dx2=2*dx;int dd2=2*(dx-dy);int x=x1,
3、y;for (y=y1+1; y=a | key=A | key=0)cout(key);display();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左键被松开;list l;l.push_back(old_x);l.
4、push_back(old_y);l.push_back(x);l.push_back(600-y);ls.push_back(l);mouse_type = 0;glutPostRedisplay();for(int i=0; ils.size(); +i)coutendlline_i: lsi0,lsi1 lsi2,lsi3;break;case GLUT_MIDDLE_BUTTON: break;case GLUT_RIGHT_BUTTON: if ( state = GLUT_DOWN ) cout右键被按下;cx = x;cy = 600-y;mouse_type = 3;else
5、if ( state = GLUT_UP ) cout右键被松开;list l;l.push_back(cx);l.push_back(cy);cr=int(sqrt(double(x-cx)*(x-cx)+(600-y-cy)*(600-y-cy)+0.5);l.push_back(cr);cs.push_back(l);mouse_type = 2;glutPostRedisplay();for(int i=0; ics.size(); +i)coutendlcircle_i: csi0,csi1 csi2;break;void screenmotion(int x, int y)if(m
6、ouse_type = 1)new_x = x;new_y = 600-y;coutendlnew-line:old_x,old_y new_x,new_yendl;else if(mouse_type = 3)cr=int(sqrt(double(x-cx)*(x-cx)+(600-y-cy)*(600-y-cy)+0.5);coutendlnew-circle: cx,cy crendl;glutPostRedisplay();void init() glClearColor(0.2, 0.5, 0.8, 0.0); /背景颜色glMatrixMode(GL_PROJECTION);glu
7、Ortho2D(-1.0, 600, 0.0, 601);int main(int argc, char* argv) glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowPosition(100, 50); /图框在屏幕的位置glutInitWindowSize(600, 600); /边框大小glutCreateWindow(图元算法); /图框边显示的字init();glutDisplayFunc(display);glutMotionFunc(screenmotion);glutKeyboardFunc( keyboard );glutMouseFunc( mouseclick );glutMainLoop();return 0;