Periodically trim Polyline

Hi,
Is there anyway I can periodically trim a polyline?


I need the bit of polyline in white colour.

The order of domain start and end doesnt seem to matter. It trims the polyline from the lower value to higher value.
Any ideas on how to do a cyclical trim?

Thanks!

using this in the meantime https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.curve/trim#(interval)
and attempting to convert the resulting curve back into polyline.

Does this help?

yeah I am using Curve.Trim for now as I mentioned in my previous reply but would prefer not to switch type from Polyline to Curve if possible

Hint: A polyline is a list of point, so get the floor and ceils of your domain start and end, split into two lists and interpolate the points in between and append them as first and last points in your new two lists.

script is kinda buggy, but gives the direction
trim_polylines.gh (17.8 KB)

@dn.aur
I ended up creating a util method that works for me.
The grasshopper script you attached works similar to Polyline.Trim method from RhinoCommon but thanks for your reply!
Cheers

public static Polyline TrimPeriodicPolyline(Polyline polyline, double trimDomainStart, double trimDomainEnd)
  {
      if (!polyline.IsClosed) throw new Exception(" Input polyline must be closed.");

      var trimmedPolyline = new Polyline();
      var firstPoint = polyline.PointAt(trimDomainStart);
      trimmedPolyline.Add(firstPoint);

      var indexToAppend = Math.Ceiling(trimDomainStart);

      while (indexToAppend != Math.Ceiling(trimDomainEnd))
      {
          trimmedPolyline.Add(polyline.PointAt(indexToAppend));
          // cyclical increment for index
          indexToAppend = (indexToAppend + 1) % polyline.Count;
      }

      var lastPoint = polyline.PointAt(trimDomainEnd);
      trimmedPolyline.Add(lastPoint);

      // tolerance for method can be retrieved from document tolerance
      trimmedPolyline.RemoveNearlyEqualSubsequentPoints(1);

      return trimmedPolyline;
  }