哈 算是模仿屏保吧~ 不过都是自己思考哦。窗口 没有全屏……
随机种子没有设定,所以每次打开都是一个“随机”路线(真是随机中的不随机啊……)
然后是代码没有考虑效率,没有考虑易读性,没有考虑代码体积Q2¥#@……
#include <GL/glut.h>
#include <time.h>
#include <stdlib.h>
#define xpix 500
#define ypix 300
#define dotsize 2
#define NDOT 3
#define RCD 8
#define ATOB 5
/* ATOB 是为了使点与点之间的距离明显,所以把坐标实际放大,导致点显示范围缩小,
   而建立的一个比率,实际500个点的坐标,0-99 代表了总长,0-1里实际包括了5个像素 
*/
int winid;
struct {
  int x;
  int y;
} source[NDOT];
struct dots {
  float x,y;
  float xadd,yadd;
} dot[NDOT];
struct {
   struct {float x,y;} dot[NDOT];
} record[RCD];
float xadd,yadd;
void display(void)
{
   int i;
   static int rcd=0,clear=0;
   _sleep(30);
   if (clear==0) {glClear(GL_COLOR_BUFFER_BIT);clear+=1;}
   if (rcd<(RCD-1)) {rcd++;} else {rcd=0;}
   glBegin(GL_LINE_STRIP);
      if (record[rcd].dot[0].x!=-1) {
         glColor3f(0.0,0.0,0.0);
         for (i=0;i<NDOT;i++) {
          glVertex3f(record[rcd].dot.x,record[rcd].dot.y,0.0);
         }
         glVertex3f(record[rcd].dot[0].x,record[rcd].dot[0].y,0.0);
      }
      for (i=0;i<NDOT;i++) {
          glColor3f(dot.x/xpix*ATOB,dot.y/ypix*ATOB,0.6);
          glVertex3f(dot.x,dot.y,0.0);
          record[rcd].dot.x=dot.x;
          record[rcd].dot.y=dot.y;
      }
      glVertex3f(dot[0].x,dot[0].y,0.0);
   glEnd();
   glFlush();
}
void todo(void)
{
    int i;
    void set_xadd_yadd(int i);
    for (i=0;i<NDOT;i++)
    {
      if (source.x!=(int)dot.x) {
        dot.x+=dot.xadd;
      } else {
        if (source.y==(int)dot.y) {
           source.x=rand()%(xpix/ATOB);
           source.y=rand()%(ypix/ATOB);
           set_xadd_yadd(i);
        }
      }
      if (source.y!=(int)dot.y) {
        dot.y+=dot.yadd;
      }
    }
    glutPostRedisplay();
}
void set_xadd_yadd(int i)
{
     float deltax,deltay;
     deltax=source.x-dot.x;
     deltay=source.y-dot.y;
     if ((deltax*deltax)>(deltay*deltay)) {
        if (source.x>dot.x) {dot.xadd=1.0;} else {dot.xadd=-1.0;}
        dot.yadd=deltay/deltax*dot.xadd;
     } else {
        if (source.y>dot.y) {dot.yadd=1.0;} else {dot.yadd=-1.0;}
        dot.xadd=deltax/deltay*dot.yadd;
     }
}
void init(void)
{
     int i;
     glOrtho(0.0,xpix/ATOB,0.0,ypix/ATOB,0.0,1.0);
     glClearColor(0.0,0.0,0.0,0.0);
     glPointSize(dotsize);
     for (i=0;i<NDOT;i++) {
          source.x=xpix/2/ATOB;
          source.y=ypix/2/ATOB;
          dot.x=source.x;
          dot.y=source.y;
          record[0].dot.x=source.x;
          record[0].dot.y=source.y;
     }
     for (i=1;i<RCD;i++) {
         record.dot[0].x=-1;
     }
}
void hitkey(unsigned char key,int mousex,int mousey)
{
     switch (key)
     {
      case 'q':
           glutDestroyWindow(winid);
           exit(0);
     }
}
int main(int argc,char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
    glutInitWindowSize(xpix,ypix);
    glutInitWindowPosition(0,0);
    winid=glutCreateWindow("IF_STRIP_TRIANGLE");
    init();
    glClear(GL_COLOR_BUFFER_BIT);
    glutDisplayFunc(display);
    glutIdleFunc(todo);
    glutKeyboardFunc(hitkey);
    glutMainLoop();
    return 0;
}
|
|
评分
-
1
查看全部评分
-
|