Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- {type}
- {program}
- {code}
- {/*Generates a fractal geometry composed of regular tetrahedrons. In each iteration a new tetrahedron is placed at
- certain distance of each vertex of the tetrahedrons created in the previous iteration. The new tetrahedrons are
- scaled and rotated.
- Elements of the member "variable":
- Number of iterations must be greater than 0.
- Factor to be multiplied by the vector joining the center of a tetrahedron and a vertex so as to obtain the center
- position of the new tetrahedron.
- Scale factor for each iteration.
- Angle expressed in degrees that must be rotated each new tetrahedron.
- Optional list of colors to be distributed among the resulting vertices.*/
- (function Generator()|123|
- /*Number of iterations must be greater than 0.*/
- var nit = AppLai4d.variable[0];
- /*Distance factor.*/
- var kd = AppLai4d.variable[1];
- /*Scaling factor.*/
- var ks = AppLai4d.variable[2];
- /*Angle. Pass it to radians.*/
- var angle = AppLai4d.variable[3]*Math.PI/180;
- /*Optional list of colors to be distributed among the resulting vertices.*/
- var colors = AppLai4d.variable[4];
- /*Auxiliary function for calculating the center of a set of points.*/
- function GetCenter()|123|
- var result = [0,0,0], i, nn = arguments.length;
- for(i = 0; i < nn; i++)|123|
- AppLai4d.VecAdd(arguments[i], result, result);
- |125|
- return AppLai4d.VecNum(result, 1/nn, result);
- |125|;
- /*Arrays for faces and vertices*/
- var faces = [], verts = [];
- /*Begin iterations*/
- var i, j, v, e, nn, pc, nv, pv, h, vvv = [], axis;
- for(i = 0; i < nit; i++)|123|
- /*The new vertices of this iteration*/
- nv = [];
- if(i == 0)|123|
- /*Vertices of the initial tetrahedron for iteration 0*/
- nv.push([0,0,0]);
- nv.push([100,100,0]);
- nv.push([100,0,100]);
- nv.push([0,100,100]);
- |125|else|123|
- /*For each previous tetrahedron (4 vertices)*/
- nn = pv.length;
- for(j = 0; j < nn; j+=4)|123|
- /*The center of the tetrahedron*/
- pc = GetCenter(pv[j], pv[j+1], pv[j+2], pv[j+3]);
- /*for each vertex*/
- for(v = 0; v < 4; v++)|123|
- /*Offset vector with distance factor*/
- h = AppLai4d.VecSub(pv[j+v], pc, h);
- AppLai4d.VecNum(h, kd, h);
- AppLai4d.VecAdd(h, pc, h);
- /*Rotation axis*/
- axis = AppLai4d.VecSub(pv[j+(v+1)%4], pv[j+(v+2)%4], axis);
- /*Clone, scale, rotate and move the 4 vertices*/
- for(e = 0; e < 4; e++)|123|
- /*refer to the center*/
- vvv[e] = AppLai4d.VecSub(pv[j+e], pc);
- /*scale*/
- AppLai4d.VecNum(vvv[e], ks, vvv[e]);
- /*rotate*/
- AppLai4d.GRotate3D(axis, angle, vvv[e], vvv[e]);
- /*Move to the final position*/
- AppLai4d.VecAdd(vvv[e], h, vvv[e]);
- |125|
- /*Pass the new vertices to nv*/
- Array.prototype.push.apply(nv, vvv);
- vvv.length = 0;
- |125|
- |125|
- |125|
- /*Pass the new vertices to the final set of vertices*/
- Array.prototype.push.apply(verts, nv);
- /*Auxiliary array for vertices of the previous iteration*/
- pv = nv;
- |125|
- /*Generate the faces for each defined tetrahedron*/
- nn = verts.length;
- for(j = 0; j < nn; j+=4)|123|
- for(v = 0; v < 4; v++)|123|
- faces.push([j+v, j+(v+1)%4, j+(v+2)%4]);
- |125|
- |125|
- /*The result is a polynet.*/
- var result = ["type","polynet","vertices", verts, "faces", faces];
- /*Distribute colors*/
- if(colors)|123|
- var vertcolors = [];
- nn = verts.length;
- for(i = 0; i < nn; i++)|123|
- vertcolors[i] = colors[i%colors.length];
- |125|
- result.push("vertex colors");
- result.push(vertcolors);
- |125|
- return result;
- |125|)();}
- {variable}
- {
- {6}
- {1.25}
- {0.6}
- {0}
- {
- {{1}{0}{0}}
- {{0}{1}{0}}
- {{0}{0}{1}}
- {{1}{1}{0}}
- {{1}{0}{1}}
- {{0}{1}{1}}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement