Sierpinski Gasket分形图的绘制

200792501.jpg


/**/ /* two-dimensional Sierpinski gasket          */
/**/ /* generated using randomly selected vertices */
/**/ /* and bisection                              */

#include 
< GL / glut.h >

void  myinit()
{

/**//* attributes */
      glClearColor(
1.01.01.01.0); /**//* white background */
      glColor3f(
1.00.00.0); /**//* draw in red */
}


void  reshape( int  w, int  h)
{
    glViewport(
0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(
0.050.00.050.0);/**//* set up viewing 50.0 x 50.0 camera coordinate window with origin lower left */

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}


void  display()
{
    
/**//* A triangle */
    GLfloat vertices[
3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};
    
int j, k;
    
int rand();       /**//* standard random number generator */
    GLfloat p[
2={7.55.0};  /**//* arbitrary initial point inside triangle */
    glClear(GL_COLOR_BUFFER_BIT);  
/**//*clear the window */
    glBegin(GL_POINTS);

/**//* compute and plot 5000 new points */

    
for( k=0; k<5000; k++)
    
{
         j
=rand()%3/**//* pick a vertex at random */

     
/**//* compute point halfway between selected vertex and old point */

         p[
0= (p[0]+vertices[j][0])/2.0;
         p[
1= (p[1]+vertices[j][1])/2.0;

     
/**//* plot new point */

         glVertex2fv(p);

    }

     glEnd();
     glFlush(); 
/**//* clear buffers */
 }


int  main( int  argc,  char **  argv)
{

/**//* standard GLUT initialization */
    glutInit(
&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE 
| GLUT_RGB); /**//* default, not needed */
    glutInitWindowSize(
500,500); /**//* 500 x 500 pixel window */
    glutInitWindowPosition(
0,0); /**//* place window top left on display */
    glutCreateWindow(
"Sierpinski Gasket"); /**//* window title */
    glutDisplayFunc(display); 
/**//* display callback invoked when window opened */
    glutReshapeFunc(reshape);
    myinit(); 
/**//* set attributes */
    glutMainLoop(); 
/**//* enter event loop */
}


200792502.jpg


/**/ /* recursive subdivision of triangle to form Sierpinski gasket */
/**/ /* number of recursive steps given on command line */

#include 
< GL / glut.h >

/**/ /* initial triangle */

GLfloat v[
3 ][ 2 ] = {{-1.0-0.58}{1.0-0.58}{0.01.15}} ;

int  n;

void  triangle( GLfloat  * a, GLfloat  * b, GLfloat  * c)

/**/ /* specify one triangle */
{
       glVertex2fv(a);
       glVertex2fv(b);
       glVertex2fv(c);
}


void  divide_triangle(GLfloat  * a, GLfloat  * b, GLfloat  * c,  int  m)
{

/**//* triangle subdivision using vertex numbers */

    GLfloat v0[
2], v1[2], v2[2];
    
int j;
    
if(m>0)
    
{
        
for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
        
for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
        
for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
        divide_triangle(a, v0, v1, m
-1);
        divide_triangle(c, v1, v2, m
-1);
        divide_triangle(b, v2, v0, m
-1);
    }

    
else triangle(a,b,c); /**//* draw triangle at end of recursion */
}



void  display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    divide_triangle(v[
0], v[1], v[2], n);
    glEnd();
    glFlush();
}


void  myinit()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(
-2.02.0-2.02.0);
    glMatrixMode(GL_MODELVIEW);
    glClearColor (
1.01.01.01.0);
    glColor3f(
0.0,0.0,1.0);
}


int  main( int  argc,  char   ** argv)
{
    n
=3/**//* or set number of subdivision steps here */
    glutInit(
&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE 
| GLUT_RGB);
    glutInitWindowSize(
500500);
    glutCreateWindow(
"Sierpinski Gasket");
    glutDisplayFunc(display);
    myinit();
    glutMainLoop();
}


200792503.jpg

/**/ /* recursive subdivision of a tetrahedron to form 3D Sierpinski gasket */
/**/ /* number of recursive steps given on command line */

#include 
< stdlib.h >
#include 
< GL / glut.h >

/**/ /* initial tetrahedron */

GLfloat v[
4 ][ 3 ] = {{0.00.01.0}{0.00.942809-0.33333},
      
{-0.816497-0.471405-0.333333}{0.816497-0.471405-0.333333}}
;

GLfloat colors[
4 ][ 3 =   {{1.00.00.0}{0.01.00.0},
                        
{0.00.01.0}{0.00.00.0}}
;

int  n;

void  triangle(GLfloat  * va, GLfloat  * vb, GLfloat  * vc)
{
       glVertex3fv(va);
       glVertex3fv(vb);
       glVertex3fv(vc);
}


void  tetra(GLfloat  * a, GLfloat  * b, GLfloat  * c, GLfloat  * d)
{
    glColor3fv(colors[
0]);
    triangle(a, b, c);
    glColor3fv(colors[
1]);
    triangle(a, c, d);
    glColor3fv(colors[
2]);
    triangle(a, d, b);
    glColor3fv(colors[
3]);
    triangle(b, d, c);
}


void  divide_tetra(GLfloat  * a, GLfloat  * b, GLfloat  * c, GLfloat  * d,  int  m)
{

    GLfloat mid[
6][3];
    
int j;
    
if(m>0)
    
{
        
/**//* compute six midpoints */

        
for(j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
        
for(j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
        
for(j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
        
for(j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
        
for(j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
        
for(j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;

        
/**//* create 4 tetrahedrons by subdivision */

        divide_tetra(a, mid[
0], mid[1], mid[2], m-1);
        divide_tetra(mid[
0], b, mid[3], mid[5], m-1);
        divide_tetra(mid[
1], mid[3], c, mid[4], m-1);
        divide_tetra(mid[
2], mid[4], d, mid[5], m-1);

    }

    
else(tetra(a,b,c,d)); /**//* draw tetrahedron at end of recursion */
}



void  display()
{
    glClear(GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    divide_tetra(v[
0], v[1], v[2], v[3], n);
    glEnd();
    glFlush();
}



void  myReshape( int  w,  int  h)
{
    glViewport(
00, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
if (w <= h)
        glOrtho(
-2.02.0-2.0 * (GLfloat) h / (GLfloat) w,
            
2.0 * (GLfloat) h / (GLfloat) w, -10.010.0);
    
else
        glOrtho(
-2.0 * (GLfloat) w / (GLfloat) h,
            
2.0 * (GLfloat) w / (GLfloat) h, -2.02.0-10.010.0);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();
}



int  main( int  argc,  char   ** argv)
{
    n
=3/**//* or enter number of subdivision steps here */
    glutInit(
&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE 
| GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(
500500);
    glutCreateWindow(
"3D Gasket");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glClearColor (
1.01.01.01.0);
    glutMainLoop();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值