# Need Help Dividing a curve by Multiple Distances within Tolerance

I am working on an algorithm that seems like it shouldn’t be too hard to create but I keep getting stuck.

Inputs:

1. Curve
2. list of desired segment lengths

Output:

1. list of points at divisions along the input curve
2. list of distances between points (results of spacing after required adjustments)

Logic:
I am trying to discretize a curve into a polyline, but I want the segment lengths to be as close as the input lengths as possible. My thought is that I could start by dividing the curve by the number of segment lengths to get the right number of segments with incorrect lengths. Then, I want to move the divisions along the curve iteratively using weighted averages of the desired segment lengths until the changes to the points is less than some tolerance. The result should be a polyline with the correct number of segments with the lengths all a little stretched or compressed relative to the desired lengths input.

If I have a straight line with length 10 and my input segment lengths were ( 2, 2, 2, 3 ), the segments would all end up slightly larger than the input length to fill the 1 unit remainder.

I am having trouble thinking through the logic for balancing the lengths while still trying to stay as close to the desired lengths as possible. Does anyone have experience with this?

May be this help:

when you talk about lengths you mean the lengths of the output polyline-segments - which is more a distance between devision-points ?

Is it a open, planar curve ?
Do you need to handle closed / non-planar Curves as well ?

Your result will be identified by a factor, that multiplies all desired segment lengths.
It s a pity there is no “NextPointAtDistance” method for curves. (There is DivideEquidistance)

Start with one factorA to scale the desired segment lengths - factor must be to small - does not use the entire curve.
(Distance from start to endpoint of open Curve is a nice start (Line-Length) for the sum of all segment lengths)

Increase factorA by * 1.2 until you get two values: first factorA is to small, factorB is to big.

now start with nested intervals / recursion to find the final result within tolerance between factorA and factorB.
Nested intervals - Wikipedia / recursion.

does this help ? kind regards tom

you need a `While Loop` :
DividingMultipleDistancesbyTolerance_001.gh (17.6 KB)

Do you need want to measure along the curve (@saeed_hasan_zadeh ) example.
or by distance / polyline-Segment-length ? (white sketch)

I am measuring by distance not length (which makes it more complicated and interesting).
I have made my own GetPointAtDistanceFromPoint that will let me Divide a curve my a list of distances.

I have it working so that I will divide the curve by my lengths and return a remainder. I call this divide curve by list of distances method in a while loop. In the while loop, I get the returned remainder and divide that into the number of layers as a layer adjustment and try again. This method “works” but it doesn’t balance out the stretching or compressing of the segments very well… It also can get stuck stretching then compressing which kind of sucks.

Now, I am working on a different approach. This time, I am starting by dividing the curve into the correct number of segments without caring about the lengths. I am then going to start a while loop that moves the points between the segments by pushing the segments to be the desired lengths. This will stretch or compress the layers proportionally and I won’t get stuck in a forever while loop trying to minimize the remainder by stretching and compressing the segments.

if i where you, after explaining both `problem`,`input` and `output` properly then won’t explain my own wrong mind logic, else i will trap in a loop which another unknown new and interesting challenge appear.
your chance was @Tom_P got interested and was going to help to solve your problem fast and furious far more than enough.
good luck.