Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 469
sebp Avatar asked Aug 22 '10 17:08

sebp


2 Answers

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

like image 194
Tom Sirgedas Avatar answered Sep 22 '22 17:09

Tom Sirgedas


    for(int i=0;i<38;i++)
    {
        Points[i].x = m_Pos.x * (1 - (i/38.0)) + m_To.x * (i / 38.0);
        Points[i].y = m_Pos.y * (1 - (i/38.0)) + m_To.y * (i / 38.0);
        if(i == 0 || i == 37 || i == 19) dbg_msg("CLight","(%d)\nPos(%f,%f)\nTo(%f,%f)\nPoint(%f,%f)",i,m_Pos.x,m_Pos.y,m_To.x,m_To.y,Points[i].x,Points[i].y);
    }

prints:

[4c7cba40][CLight]: (0)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3376.000000,1808.000000)
[4c7cba40][CLight]: (19)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3388.425781,1767.357056)
[4c7cba40][CLight]: (37)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3400.851563,1726.714111)

which looks fine but then my program doesn't work :D. but your method works so thanks

like image 23
Shereef Marzouk Avatar answered Sep 21 '22 17:09

Shereef Marzouk