OpenGL仿作橡皮筋技术

来源:互联网 发布:mac 显示绝对路径 编辑:程序博客网 时间:2024/05/02 12:06

算是精仿吧,毕竟不一样,少了第二次点鼠标左键确认绘图,这个可以加上的:


#include <iostream>#include <windows.h>#include <GL/glut.h>using namespace std ;#pragma comment(lib, "glut32.lib")    int flag =0;int flagagain=0;POINT start,down; void init(void){ glClearColor(0.0, 0.0, 0.0, 0.0) ; glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) ;}void display(void) { glClear(GL_COLOR_BUFFER_BIT) ; if(flag) {glColor3f(1.0, 1.0, 1.0) ;glBegin(GL_LINES);GLfloat oldx = start.x*1.00/400;GLfloat oldy = start.y*1.00/400;GLfloat newx = down.x*1.00/400;GLfloat newy = down.y*1.00/400;glVertex2f(oldx,oldy);glVertex2f(newx,newy);glutSwapBuffers();   } glEnd() ; glFlush() ;}void motion(int x, int y) {if(!flag){start.x = x;start.y = 400-y;}down.x = x;down.y = 400-y;}void mouse(int button, int state, int x, int y)  {      if (button == GLUT_LEFT_BUTTON)      {          if (state == GLUT_DOWN)          {               glutMotionFunc(motion);//获得start flag=1;        }      }  }  int main(int argc, char **argv){ glutInit(&argc, argv) ; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);   glutInitWindowSize(400, 400) ; glutCreateWindow("I love OpenGl") ; init() ; glutDisplayFunc(display) ; glutMouseFunc(mouse); glutPassiveMotionFunc(motion); glutIdleFunc(display);   glutMainLoop();   return 0 ;}

//喏 空心圆


#include <iostream>#include <windows.h>#include <GL/glut.h>#include<math.h>using namespace std ;#define pi 3.1415926535843721#pragma comment(lib, "glut32.lib")    int flag =0;int flagagain=0;int n;POINT start,down; float GetLength(POINT start,POINT down){return sqrt((start.x*1.00/400-down.x*1.00/400)*(start.x*1.00/400-down.x*1.00/400)+(start.y*1.00/400-down.y*1.00/400)*(start.y*1.00/400-down.y*1.00/400));}void init(void){ glClearColor(0.0, 0.0, 0.0, 0.0) ; glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) ;}void display(void) { glClear(GL_COLOR_BUFFER_BIT) ; if(flag) {glColor3f(1.0, 1.0, 1.0) ;glBegin(GL_LINES);GLfloat oldx = start.x*1.00/400;GLfloat oldy = start.y*1.00/400;GLfloat newx = down.x*1.00/400;GLfloat newy = down.y*1.00/400;glVertex2f(oldx,oldy);glVertex2f(newx,newy);glEnd();glBegin(GL_LINE_STRIP); glColor3f(1.0, 1.0, 1.0) ;n = 100;float R = GetLength(start,down);//半径cout<<R<<endl;//输出半径for (int i = 0; i < n; i++){glVertex2f(R*sin(2 * pi / n*i)+oldx, R*cos(2 * pi / n*i)+oldy);//平移到  以原来鼠标 左 键  为原点}glEnd();glutSwapBuffers();   } glEnd() ; glFlush() ;}void motion(int x, int y) {if(!flag){start.x = x;start.y = 400-y;}down.x = x;down.y = 400-y;}void mouse(int button, int state, int x, int y)  {      if (button == GLUT_LEFT_BUTTON)      {          if (state == GLUT_DOWN)          {               glutMotionFunc(motion);//获得start flag=1;        }      }  }  int main(int argc, char **argv){ glutInit(&argc, argv) ; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);   glutInitWindowSize(400, 400) ; glutCreateWindow("I love OpenGl") ; init() ; glutDisplayFunc(display) ; glutMouseFunc(mouse); glutPassiveMotionFunc(motion); glutIdleFunc(display);   glutMainLoop();   return 0 ;}


0 0