Advertisement
Guest User

Untitled

a guest
May 26th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. {
  2. {type}
  3. {program}
  4. {code}
  5. {/*Generates a fractal geometry composed of regular tetrahedrons. In each iteration a new tetrahedron is placed at
  6. certain distance of each vertex of the tetrahedrons created in the previous iteration. The new tetrahedrons are
  7. scaled and rotated.
  8. Elements of the member "variable":
  9. Number of iterations must be greater than 0.
  10. Factor to be multiplied by the vector joining the center of a tetrahedron and a vertex so as to obtain the center
  11. position of the new tetrahedron.
  12. Scale factor for each iteration.
  13. Angle expressed in degrees that must be rotated each new tetrahedron.
  14. Optional list of colors to be distributed among the resulting vertices.*/
  15. (function Generator()|123|
  16. /*Number of iterations must be greater than 0.*/
  17. var nit = AppLai4d.variable[0];
  18. /*Distance factor.*/
  19. var kd = AppLai4d.variable[1];
  20. /*Scaling factor.*/
  21. var ks = AppLai4d.variable[2];
  22. /*Angle. Pass it to radians.*/
  23. var angle = AppLai4d.variable[3]*Math.PI/180;
  24. /*Optional list of colors to be distributed among the resulting vertices.*/
  25. var colors = AppLai4d.variable[4];
  26.  
  27. /*Auxiliary function for calculating the center of a set of points.*/
  28. function GetCenter()|123|
  29. var result = [0,0,0], i, nn = arguments.length;
  30. for(i = 0; i < nn; i++)|123|
  31. AppLai4d.VecAdd(arguments[i], result, result);
  32. |125|
  33. return AppLai4d.VecNum(result, 1/nn, result);
  34. |125|;
  35.  
  36. /*Arrays for faces and vertices*/
  37. var faces = [], verts = [];
  38. /*Begin iterations*/
  39. var i, j, v, e, nn, pc, nv, pv, h, vvv = [], axis;
  40. for(i = 0; i < nit; i++)|123|
  41. /*The new vertices of this iteration*/
  42. nv = [];
  43. if(i == 0)|123|
  44. /*Vertices of the initial tetrahedron for iteration 0*/
  45. nv.push([0,0,0]);
  46. nv.push([100,100,0]);
  47. nv.push([100,0,100]);
  48. nv.push([0,100,100]);
  49. |125|else|123|
  50. /*For each previous tetrahedron (4 vertices)*/
  51. nn = pv.length;
  52. for(j = 0; j < nn; j+=4)|123|
  53. /*The center of the tetrahedron*/
  54. pc = GetCenter(pv[j], pv[j+1], pv[j+2], pv[j+3]);
  55. /*for each vertex*/
  56. for(v = 0; v < 4; v++)|123|
  57. /*Offset vector with distance factor*/
  58. h = AppLai4d.VecSub(pv[j+v], pc, h);
  59. AppLai4d.VecNum(h, kd, h);
  60. AppLai4d.VecAdd(h, pc, h);
  61. /*Rotation axis*/
  62. axis = AppLai4d.VecSub(pv[j+(v+1)%4], pv[j+(v+2)%4], axis);
  63. /*Clone, scale, rotate and move the 4 vertices*/
  64. for(e = 0; e < 4; e++)|123|
  65. /*refer to the center*/
  66. vvv[e] = AppLai4d.VecSub(pv[j+e], pc);
  67. /*scale*/
  68. AppLai4d.VecNum(vvv[e], ks, vvv[e]);
  69. /*rotate*/
  70. AppLai4d.GRotate3D(axis, angle, vvv[e], vvv[e]);
  71. /*Move to the final position*/
  72. AppLai4d.VecAdd(vvv[e], h, vvv[e]);
  73. |125|
  74. /*Pass the new vertices to nv*/
  75. Array.prototype.push.apply(nv, vvv);
  76. vvv.length = 0;
  77. |125|
  78. |125|
  79. |125|
  80.  
  81. /*Pass the new vertices to the final set of vertices*/
  82. Array.prototype.push.apply(verts, nv);
  83. /*Auxiliary array for vertices of the previous iteration*/
  84. pv = nv;
  85. |125|
  86.  
  87. /*Generate the faces for each defined tetrahedron*/
  88. nn = verts.length;
  89. for(j = 0; j < nn; j+=4)|123|
  90. for(v = 0; v < 4; v++)|123|
  91. faces.push([j+v, j+(v+1)%4, j+(v+2)%4]);
  92. |125|
  93. |125|
  94.  
  95. /*The result is a polynet.*/
  96. var result = ["type","polynet","vertices", verts, "faces", faces];
  97.  
  98. /*Distribute colors*/
  99. if(colors)|123|
  100. var vertcolors = [];
  101. nn = verts.length;
  102. for(i = 0; i < nn; i++)|123|
  103. vertcolors[i] = colors[i%colors.length];
  104. |125|
  105. result.push("vertex colors");
  106. result.push(vertcolors);
  107. |125|
  108.  
  109. return result;
  110. |125|)();}
  111. {variable}
  112. {
  113. {6}
  114. {1.25}
  115. {0.6}
  116. {0}
  117. {
  118. {{1}{0}{0}}
  119. {{0}{1}{0}}
  120. {{0}{0}{1}}
  121. {{1}{1}{0}}
  122. {{1}{0}{1}}
  123. {{0}{1}{1}}
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement