# Move Point along multiple lines

1. Jul 26, 2013

### Zoltan

Hi, i got problem i am trying to solve.

Basicly i got point moving along bunch of lines ( either rectangles/triangles it is custom, generated in code ).

I am using this equation to find out x,y coordinates for moving along specific line :
(its taken from code )

x = x1 + center.x + (x2 - x1) * p
y = y1 + center.y + (y2 - y1) * p

center is there just to adjust the whole shape
p is basicly the increment for moving the point on the line.

Now i got logic to move between the different lines, by just figuring out if the p is over 1 switch to next line. Thats all fine and dandy.

But here comes the problem. Each of the p increments are same each frame, i.e. 0.1,0.2,0.3,0.4 it means that no matter how long the line is it always takes same amount of time to finish. Because 0.1 distance represents 10% of length of the line, it would move 10% of the line distance.

Now i can fix this with just dividing the p basicly by the length of the line, thus all the movement on all lines is same. If you would have triangle with two realy long lines and one realy small. You would have realy fast movement on the two long lines and realy slow movement on the small with the 10% movement per frame. But thats what i dont want :) I want the speed to be same on each of the line while mainting same amount of time it took to finish movement on whole shape before normalising it.

I do that by dividing the increment by the line distance, its working fine, but the speed is mush slower obviously. ( Example, if you would have increment 1 unit per frame and lenght of line was 10 units, speed would be 0.1, but problem is you got 3 different lines and the speed needs to adjusted so that it takes same amount of time as before this lenght division )

I tried things like take circumference of the whole shape, dividing it by amount of sides and using it that value to multiple the speed. Wrong

I tried this equation when the shape was triangle :
(x/a + x/b +x/c) * y = 3x
x = increment
y = desired multiplication of the speed

it basicly says if u have 3 frames movement, one at each of the side of the triangle with the normalised increment, if you mulitple that by some value, you get equal amount to original increments in 3 frames 1 = 1. Wrong ! value doesnt work

So, i am not sure if its clear or not :) I am not too good in explaining stuff. But if you can help me with the problem in any way, i would very very grateful. Any suggestions welcomed. Thank you for your time taken to read it all this.

Zoltan

Edit:

I realised i probably should have factor how long the point is on each of the line so i adjusted the equation like this:

((x/a * a/t) + (x/b * b/t) + (x/c * c/t)) * y = x

t = circumference of the shape (triangle)

Still not working tho.

Last edited: Jul 26, 2013
2. Aug 1, 2013

### robotopia

There may be much better ways of doing this, but here's what I would do.

First, I would treat p as being the parameter over the whole path (i.e. p=0 means that the point is at the beginning of the first line, and p=1 means that the point is at the end of the last line). Then I would work out the values for p at which the point is at the intersection of two lines--i.e. when it switches lines. For example, suppose you had the triangle case, so there are three lines. Let their lengths be called L1, L2, L3. Then p switches from the first line to the second line at p1 = L1 / (L1+L2+L3), and from the second to the third line at p2 = (L1+L2) / (L1+L2+L3).

Now as p changes incrementally from 0 to 1, figure out which line segment it should be on by comparing it to p1 and p2, and use that line segment's values of x1, x2, y1, y2 in your calculation of the point's coordinates. Because now the limits of p for each line are different, you'll have to "recalculate" p so that it represents how far along just that line segment it is. I think you'll understand what I mean when you see the "code" below.

So, here is my "code", untested and unimplemented. Hope it's useful to you.

Code (Text):
// Given vertices (x1,y1), (x2,y2), (x3,y3), (x4,y4), representing three line segments
// from points 1 --> 2, 2 -->3, 3-->4.

L1 = sqrt((x2-x1)^2+(y2-y1)^2)
L2 = sqrt((x3-x2)^2+(y3-y2)^2)
L3 = sqrt((x4-x3)^2+(y4-y3)^2)

L = L1+L2+L3

p0 = 0
p1 = L1/L
p2 = (L1+L2)/L
p3 = 1

for loop: p from p0 to p3 (or 0 to 1) with whatever increment you want
if p0 <= p < p1:
p_lo = p0
p_hi = p1
x_lo = x0
x_hi = x1
y_lo = y0
y_hi = y1
else if p1 <= p < p2:
etc. // same as above but altered accordingly
else if p2 <= p <= p3:
etc.
end if

p_adj = (p - p_lo) / (p_hi - p_lo)  // So now p_adj is an 'adjusted' p that ranges from 0 to 1
// according to how far along this line segment is has progressed

x = x_lo + center.x + (x_hi - x_lo) * p_adj
y = y_lo + center.y + (y_hi - y_lo) * p_adj

// Display point, or whatever you want to do with it...

end for loop