Evaluate each successive point y value and cull if greater than previous?

Hi everyone,

I am a beginner to python and coding in general, and I was wondering how one could evaluate the y value of a point comparing it to its previous on the list and if greater than the previous eliminate it. The main goal would be this one:

Thanks for any info.

Perhaps something like the following? Let me know if it’s not clear…

import rhinoscriptsyntax as rs

pl=rs.GetObject("Select a polyline",4,True)
#check for polyline
if pl and rs.IsPolyline(pl):
    #get vertices
    pts=rs.PolylineVertices(pl)
    #create new list, add first point from orig polyline
    new_pts=[pts[0]]
    #get startying value for Y
    curr_y=pts[0].Y
    #loop through the polyline points starting at second point
    for i in range(1,len(pts)):
        #if Y value is less than or equal to previous, add point to new list
        if pts[i].Y<=curr_y:
            new_pts.append(pts[i])
            #update current Y value 
            curr_y=pts[i].Y
    #check if there are at least two points in new list, add new PL, delete old
    if len(new_pts)>1:
        rs.AddPolyline(new_pts)
        rs.DeleteObject(pl)

Hello Helvetosaur, thank you.

It works great. But I am afraid I had to change one rule: Instead of eliminating the point the best would be to move the peak point down in y, by the difference in height to the previous point + an user set value (the same for all):

Sorry for changing after you answered my initial inquiry. I hope its not much trouble.

.

This is certainly possible - just one thing, what happens if in lowering an intermediate point by your user-specified amount below the preceding one, it becomes lower than the next one? Lower that one as well and so on all the way down the line? That’s the only way I see it working consistently…

Something like this:

import rhinoscriptsyntax as rs

def RegularizePolyline():
    pl=rs.GetObject("Select a polyline",4,True)
    if not pl: return
    #check for polyline
    if rs.IsPolyline(pl):
        #get increment value
        inc=rs.GetReal("Lowering increment value?",minimum=0)
        if inc is None: return
        #get vertices
        pts=rs.PolylineVertices(pl)
        #get startying value for Y
        curr_y=pts[0].Y
        #loop through the polyline points starting at second point
        for i in range(1,len(pts)):
            if pts[i].Y<curr_y:
                #if Y value is less than previous, update Y and continue
                curr_y=pts[i].Y
            else:
                #lower current Y value by increment
                curr_y-=inc
                #set point's Y value to new current Y value and continue
                pts[i].Y=curr_y
        #Add new polyline using adjusted points
        rs.AddPolyline(pts)
        rs.DeleteObject(pl)
RegularizePolyline()

In this case as we’re not deleting any points, I can just modify the original points in the list and make a new polyline with them at the end. I also encapsulated it into a function definition to allow escaping if the user doesn’t enter a value for the increment.

Thank you Helvetosaur!

Indeed, I did not consider that option, but that would the correct follow up. Thanks again.