Grasshopper

algorithmic modeling for Rhino

Hello everyone,

I'm trying to switch the positions of two points in a list but it doesn't work. Tried already a few things however without results -.-

Let's say, there is a list with a few points:

List<Point3d> Locations = new List<Point3d>();

P1

P2

P3

P4

P5

Now, what I want to do is: take two random points and switch their positions.

For example: take P2 and P5, the List should then be like:

P1

P5

P3

P4

P2

Does anyone know how it works? You can take a look at the code on the picture added

Best
Tülin

Views: 569

Replies to This Discussion

copyLocations[index1] = copyLocations[index2];

copyLocations[index2] = copyLocations[index1];

that's the mistake. By the time the second line executes copyLocations[index1] is already equal to copyLocations[index2]. So all you're doing is copying the same value back from index1 to index2.

If you want to swap two values, you need to keep one tucked away in a safe place during the swap:

Point3d cache = list[index1];

list[index1] = list[index2];

list[index2] = cache;

If it's arrays then yes, pretty much the way you wrote:

Random random = new Random(1);

Point3d[][] population = new Point3d[50][];

for (int i = 0; i < population.Length; i++)

{

  population[i] = new Point3d[10];

  for (int j = 0; j < population[i].Length; j++)

  {

    double x = random.NextDouble();

    double y = random.NextDouble();

    double z = random.NextDouble();

    population[i][j] = new Point3d(x, y, z);

  }

}

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service