Grasshopper

algorithmic modeling for Rhino

Views: 960

Replies to This Discussion

This is my code:
public Polyline tl(Polyline x, int count)
{
Line[] l = x.GetSegments();
List ptlist = new List();
for(int i = 0;i < l.Length;i++)
{
Point3d pt = l[i].PointAt(0.5);
ptlist.Add(pt);
}
ptlist.Add(ptlist[0]);
Polyline nl = new Polyline(ptlist);
int co = count - 1;
if(count > 0)
{
return tl(nl, co);
}
else
{
return x;
}
}

Are you trying to recursively "inscribe" polylines?

If so ... get this attached and have fun.

Attachments:

BTW: Get 3 entry level/primitive cases as well to get the gist of recursion.

Attachments:

BTW: Although nothing to do with inscribing ... given the opportunity get this recursion (entry level) test as well.

This case gives you a unique opportunity to win lot's of sardines provided that you can make Hilbert "cubes" instead of flat polylines .

Attachments:

Hello, here is a method for doing the same thing you want but with mesh, you can copy the logic, it makes a mesh sphere based on dodecaedron : 

private void RunScript(int nfaces, ref object A)
{

int n;
n = (int) (Math.Log10((double) nfaces / 20.0) / Math.Log10(4.0));

double t = (1.0 + Math.Sqrt(5.0)) / 2.0;
double c = Math.Sqrt(1 + (1.0 + Math.Sqrt(5.0)) * (1.0 + Math.Sqrt(5.0)) / 4.0);

//Icosaedron
Rhino.Geometry.Mesh mesh = new Rhino.Geometry.Mesh();
mesh.Vertices.Add(-1 / c, t / c, 0);
mesh.Vertices.Add(1 / c, t / c, 0);
mesh.Vertices.Add(-1 / c, -t / c, 0);
mesh.Vertices.Add(1 / c, -t / c, 0);
mesh.Vertices.Add(0, -1 / c, t / c);
mesh.Vertices.Add(0, 1 / c, t / c);
mesh.Vertices.Add(0, -1 / c, -t / c);
mesh.Vertices.Add(0, 1 / c, -t / c);
mesh.Vertices.Add(t / c, 0, -1 / c);
mesh.Vertices.Add(t / c, 0, 1 / c);
mesh.Vertices.Add(-t / c, 0, -1 / c);
mesh.Vertices.Add(-t / c, 0, 1 / c);

mesh.Faces.AddFace(0, 11, 5);
mesh.Faces.AddFace(0, 5, 1);
mesh.Faces.AddFace(0, 1, 7);
mesh.Faces.AddFace(0, 7, 10);
mesh.Faces.AddFace(0, 10, 11);

mesh.Faces.AddFace(1, 5, 9);
mesh.Faces.AddFace(5, 11, 4);
mesh.Faces.AddFace(11, 10, 2);
mesh.Faces.AddFace(10, 7, 6);
mesh.Faces.AddFace(7, 1, 8);

// 5 faces around point 3
mesh.Faces.AddFace(3, 9, 4);
mesh.Faces.AddFace(3, 4, 2);
mesh.Faces.AddFace(3, 2, 6);
mesh.Faces.AddFace(3, 6, 8);
mesh.Faces.AddFace(3, 8, 9);

// 5 adjacent faces
mesh.Faces.AddFace(4, 9, 5);
mesh.Faces.AddFace(2, 4, 11);
mesh.Faces.AddFace(6, 2, 10);
mesh.Faces.AddFace(8, 6, 7);
mesh.Faces.AddFace(9, 8, 1);

for (int i = 0; i < n; i++)
{
Rhino.Geometry.Mesh mesh_rec = new Rhino.Geometry.Mesh();
recursiveSubdivision(mesh, ref mesh_rec);
mesh = mesh_rec;
}


mesh.Vertices.CombineIdentical(true, true);
mesh.Normals.ComputeNormals();
mesh.Compact();
A = mesh;

}

// <Custom additional code>


public void recursiveSubdivision(Mesh mesh, ref Mesh mesh_rec)
{
int ind = 0;

for (int i = 0; i < mesh.Faces.Count; i++)
{
Point3f A = new Point3f(mesh.Vertices[mesh.Faces[i].A].X, mesh.Vertices[mesh.Faces[i].A].Y, mesh.Vertices[mesh.Faces[i].A].Z);
Point3f B = new Point3f(mesh.Vertices[mesh.Faces[i].B].X, mesh.Vertices[mesh.Faces[i].B].Y, mesh.Vertices[mesh.Faces[i].B].Z);
Point3f C = new Point3f(mesh.Vertices[mesh.Faces[i].C].X, mesh.Vertices[mesh.Faces[i].C].Y, mesh.Vertices[mesh.Faces[i].C].Z);

ind = mesh_rec.Vertices.Count;
mesh_rec.Vertices.Add(A);//0
mesh_rec.Vertices.Add(middle(A, B));
mesh_rec.Vertices.Add(B);
mesh_rec.Vertices.Add(middle(B, C));
mesh_rec.Vertices.Add(C);
mesh_rec.Vertices.Add(middle(C, A));

mesh_rec.Faces.AddFace(ind + 0, ind + 1, ind + 5);
mesh_rec.Faces.AddFace(ind + 1, ind + 3, ind + 5);
mesh_rec.Faces.AddFace(ind + 1, ind + 2, ind + 3);
mesh_rec.Faces.AddFace(ind + 5, ind + 3, ind + 4);
}
}


public Point3f middle(Point3f A, Point3f B)
{
Point3f AB = new Point3f();
float length;
AB.X = (A.X + B.X) / 2;
AB.Y = (A.Y + B.Y) / 2;
AB.Z = (A.Z + B.Z) / 2;

length = (float) Math.Sqrt(AB.X * AB.X + AB.Y * AB.Y + AB.Z * AB.Z);
AB.X = AB.X / length;
AB.Y = AB.Y / length;
AB.Z = AB.Z / length;
return AB;
}

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service