Grasshopper

algorithmic modeling for Rhino

Hi All, trying make similar script (like http://neoarchaic.net/2010/05/sort-points-by-distance/) in C# . Is working OK, but i still have some question :

1. How  i can optimize my script and make it more simplest way, for example i need to declare min distance (minDist) like huge number to avoid overlapping values. may be find way do not use second loop in script at all . In the neoarchaic's script has line 99  (j = pts.ClosestIndex(pt)) but i have no idea how to do it in C#. 

2. In a file i prepared GH+Hoopsnake and GH+Anemone solution for this script . Can i use  the same principle in C# (shipt list with wrap values )? This solution   I need for studying list operations in C#.

Thank you for helping . 

ps..My script: 

int num = x.Count;
double minDist,dist;
int minI = 0;
int i = 0;

Point3d pt;
List<Point3d> z = new List<Point3d>();

while (i < num) {
i++;
z.Add(x[minI]);
pt = x[minI];
minDist = 1000000000000;
x.RemoveAt(minI);

for (int n = 0; n < x.Count; n++){

dist = x[n].DistanceTo(pt);

if ( dist < minDist) {
minI = n;
minDist = dist;
}
}
}
A = z;

Views: 1392

Attachments:

Replies to This Discussion

Hi!

A translation of the VB example:

var j = default(int);
var pt = default(Point3d);
var pts = new Point3dList(x);
var ptList = new List<Point3d>();

pt = x[0]; // start with the first point in the list
// pt = p; // specify the start with a parameter

while (pts.Count() > 0)
{
  j = pts.ClosestIndex(pt);
  ptList.Add(pts[j]);
  pt = pts[j];
  pts.RemoveAt(j);
}

A = ptList;

You can access ClosestIndex if you use Rhino's Point3dList instead of List<Point3d>.

Use minDist = Double.MaxValue as a "big" value.

Take a look at the code of ClosestIndex. It does the same as your second loop:

double maxValue = double.MaxValue;
int ptIndex = -1;
int count = list.Count;

for (int i = 0; i < count; i++)
{
  Point3d pointd = list[i];
  double distSqr = (((pointd.X - testPoint.X) * (pointd.X - testPoint.X)) + ((pointd.Y - testPoint.Y) * (pointd.Y - testPoint.Y))) + ((pointd.Z - testPoint.Z) * (pointd.Z - testPoint.Z));
  if (distSqr == 0.0)
  {
    return i;
  }
  if (distSqr < maxValue)
  {
    maxValue = distSqr;
    ptIndex = i;
  }
}

return ptIndex;

Cheers!

Thank You  for detailed explanation, TJO !

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service