Grasshopper

algorithmic modeling for Rhino

hi,
does some one know an easiest way for making an simple shifted list in c# than this way ?
(if i ' m not mistaken , i remember by example that python is able to catch an item value of -1 ...)

my present c# code :



private void RunScript(int ShiftVal, List Pts, ref object A)
{

//shifted list in2 step **
List outPts = new List();
//step1
for (int i = ShiftVal;(i <= Pts.Count - 1);i++)
{
outPts.Insert((i - ShiftVal), Pts[i]);
}
//step2
for (int i = 0;(i <= ShiftVal - 1);i++)
{
outPts.Insert(((Pts.Count ) - ShiftVal) + i, Pts[i]);
}
A = outPts;

}

Views: 1090

Replies to This Discussion

Hi Julz,

maybe you do not need to shift the list at all...
Would iterate through the right indices in the collection be good enough?
What would you like to do after this?

Do you need circular wrapping or sublist removal?
Btw, index wrapping also comes at a cost...

- Giulio
_____________________
giulio@mcneel.com
McNeel Europe, Barcelona
Hi Giulio,

it's just for a personal exercise in my free time (i'm learning c#^^),
i project after to iterate this code (if there is nothing else of shortest) through n branch of a DataTree with a pattern of 1,-1,1,-1... for the shift value, and maybe after that swap the matrice of my DattaTree ... the subject for me is to understand how can y play with shift action
but i'm all right in most case it's preferable to match directly an index for making a path.
Hi Julz,

this might be a method:
see also the remainder operator %.


static T[] CircularShift<T>(IList<T> input, int shift)
{
  int l = input.Count;

  if(shift < 0)
    shift = (shift % l) + l;

  T[] result = new T[l];
  for(int i = 0; i < l; i++)
  {
    result[i] = input[(shift + i) % l];
  }
  return result;
}
Attachments:
hey thanks Giulio for the "miam",
i will take look on it.
:)
the usefull modulo concept in action ^^ thanks Giulio !

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service