Move Point along multiple lines

Zoltan
Messages
6
Reaction score
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
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
 
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
Thread 'Imaginary Pythagorus'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...
Back
Top