Search code examples
geometrylinedata-partitioning

Partition line into equal parts


This is a geometry question.

I have a line between two points A and B and want separate it into k equal parts. I need the coordinates of the points that partition the line between A and B.

Any help is highly appreciated.

Thanks a lot!


Solution

  • You just need a weighted average of A and B.

    C(t) = A * (1-t) + B * t
    

    or, in 2-D

    Cx = Ax * (1-t) + Bx * t
    Cy = Ay * (1-t) + By * t    
    
    • When t=0, you get A.
    • When t=1, you get B.
    • When t=.25, you a point 25% of the way from A to B
    • etc

    So, to divide the line into k equal parts, make a loop and find C, for t=0/k, t=1/k, t=2/k, ... , t=k/k