Move Point along multiple lines

In summary, the problem is that the point's movement along different line segments takes the same amount of time regardless of the length of those segments. To make the speed consistent, the length of the line segments must be taken into account. By treating p as the parameter over the whole path and using the lengths of the line segments, p can be adjusted to accurately represent the distance traveled on each line segment. This will ensure that the point moves at a consistent speed throughout the shape.
  • #1
Zoltan
6
0
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 really long lines and one really small. You would have really fast movement on the two long lines and really slow movement on the small with the 10% movement per frame. But that's what i don't 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 length 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 length 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 doesn't 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 realized 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:
Mathematics news on Phys.org
  • #2
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:
// 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
 

Related to Move Point along multiple lines

1. How can I move a point along multiple lines at once?

There are a few different ways to accomplish this depending on the software or programming language you are using. One approach is to use a loop to iterate through each line and update the point's coordinates accordingly.

2. Is it possible to move a point along curves as well as straight lines?

Yes, it is possible to move a point along curves using mathematical equations and algorithms. This may require additional calculations and programming compared to moving along straight lines.

3. Can I control the speed or rate of movement along the lines?

Yes, you can control the speed or rate of movement by adjusting the increment size or time interval in which the point's coordinates are updated. This can be done through code or settings in the software you are using.

4. Is it necessary to specify the starting or ending point for each line?

In most cases, yes, it is necessary to specify the starting and ending point for each line in order to accurately move the point along the desired path. However, some software or programming languages may have default settings or shortcuts for this task.

5. Are there any limitations to how many lines a point can move along at once?

The number of lines a point can move along at once may depend on the capabilities of the software or programming language being used. In general, there may be limitations on memory and processing power, but there are often ways to optimize and improve performance.

Similar threads

  • General Math
Replies
1
Views
1K
Replies
10
Views
2K
  • General Math
Replies
20
Views
1K
  • General Math
Replies
12
Views
1K
Replies
1
Views
796
  • General Math
Replies
3
Views
910
  • Precalculus Mathematics Homework Help
Replies
17
Views
1K
Replies
1
Views
968
  • Precalculus Mathematics Homework Help
Replies
7
Views
904
Back
Top