views:

55

answers:

2

Hello,

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!

+3  A: 

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

Tom Sirgedas
Works like a charm, thanks!
sebp
A: 
    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

Shereef
You may want to use "i<=38" in your loop. This will give you 39 points, which divide your line into 38 equal segments. And, are you sure your code produces that output? I'm doubtful
Tom Sirgedas