Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

Split line into multiple line

I have a one line. For this line I have start and end point. Now I want to split this line into multiple lines or number of points. How to do that?

The line looks likes below ..

enter image description here

Any idea?

Upvotes: 6

Views: 3078

Answers (1)

Jason
Jason

Reputation: 2743

Python solution:

def split(start, end, segments):
    x_delta = (end[0] - start[0]) / float(segments)
    y_delta = (end[1] - start[1]) / float(segments)
    points = []
    for i in range(1, segments):
        points.append([start[0] + i * x_delta, start[1] + i * y_delta])
    return [start] + points + [end]

print split([-10, 20], [20, 75], 5)

Output:

[[-10, 20], [-4.0, 31.0], [2.0, 42.0], [8.0, 53.0], [14.0, 64.0], [20, 75]]

Upvotes: 9

Related Questions