Sierpinski triangle:
Sierpinski cushion:
It is a very interesting pattern with a long history. It has aroused great interest in the field of fractal ry. It is a geometric shape defined by recursion and random, in extreme conditions, its properties are not random:
Generation Method:
Generate Sierpinski fill padAlgorithmDescription:
(1) randomly select a vertex inside the triangle as the initial vertex.
(2) randomly select one of the three triangle vertices, find the midpoint between the vertex and the initial vertex, and draw the midpoint
(3) Use the midpoint in (2) as the initial point to (2)
Implementation Method (OpenGL C #):
1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 6 Using Tao. OpenGL; 7 Using Tao. freeglut; 8 Using Tao. Platform. windows; 9 10 Class Program 11 { 12 Static String Title = " Sierpinski Cushion " ; 13 /// <Summary> 14 /// Initialization 15 /// </Summary> 16 Static Void Myinit () 17 { 18 // Set attributes 19 Gl. glclearcolor ( 0.0f ,0.0f , 0.0f , 1.0f ); // Background Color 20 21 // Create View 22 Gl. glmatrixmode (GL. gl_projection ); 23 Gl. glloadidentity (); 24 Glu. gluortho2d ( 0.0 ,600.0 , 0.0 , 600.0 ); 25 } 26 /// <Summary> 27 /// Plotting Functions 28 /// </Summary> 29 Static Void Display () 30 { 31 // Define an arbitrary triangle on the zplane 32 Float [,] Vertices = New Float [ 3 , 3 ] { 0.0f , 0.0f ,0.0f },{ 300f, 600f, 0f}, {600f, 0f, 0f }}; 33 34 // Define the initial point position 35 Float [] P = New Float [ 3 ] { 7.5f , 5.0f , 0.0f }; 36 Random rand1 = New Random (); 37 38 Gl. glclear (GL. gl_color_buffer_bit ); 39 Gl. glcolor3f ( 1.0f , 0.5f , 0.5f ); // Color 40 41 Gl. glbegin (GL. gl_points ); 42 For ( Int K = 0 ; K < 50000 ; K ++ ) 43 { 44 Int J = rand1.next () % 3 ; 45 46 P [ 0 ] = (P [0 ] + Vertices [J, 0 ])/ 2 ; 47 P [ 1 ] = (P [ 1 ] + Vertices [J, 1 ])/ 2 ; 48 49 Gl. glvertex3fv (P ); 50 } 51 Gl. glend (); 52 53 Gl. glflush (); 54 } 55 56 57 Static Void Reshape ( Int W, Int H) 58 { 59 60 } 61 62 Static Void Main ( String [] ARGs) 63 { 64 GLUT. gluinit (); 65 GLUT. gluinitdisplaymode (GLUT. glu_rgba | GLUT. glu_single ); 66 GLUT. gluinitwindowposition ( 400 , 100 ); 67 GLUT. gluinitwindowsize ( 600 , 600 ); 68 GLUT. glucreatewindow (title ); 69 70 71 Myinit (); 72 GLUT. gludisplayfunc (Display ); 73 // GLUT. glureshapefunc (reshape ); 74 GLUT. glumainloop (); 75 } 76 }