There are two points - the initial and final. How to set a uniform zigzag movement from one to another?

By uniform is meant that the zigzag wave has a constant height and length, and is symmetric with respect to the straight line connecting these points (like in a sinusoid).

  • Specify what you mean by "uniform zigzag movement." - fori1ton
  • and then maybe a normal sine will work. - KoVadim
  • An ordinary sine wave would fit if the movement was relative to one of the axes, but two points are not always located on the same level. - Sugar Sugar
  • and who said that a sinusoid can only be relative to one axis. No one bothers her turn / stretch / trim. - KoVadim
  • @kovadim can not imagine how to do it. The coordinates of the sinewave are shifted along both axes, but by what value? - Sugar Sugar

3 answers 3

Very simple.

To begin with, we will consider a special case: movement from a point (0, 0) to a point (2*n*pi, 0) according to the formula

 x = t y = sin t 

Such a detour makes n oscillations an amplitude of 1. If we want to change the amplitude, we multiply by it: ( A is the amplitude)

 x = t y = A * sin t 

If we need a segment of length not 2*pi*n , but of a different length, we can “flatten” the curve as many times as necessary: ​​( l is the length of the segment)

 x = t * l / (2 * pi * n) y = A * sin t 

OK, if our segment should start at the point (x1, y1) , move our curve:

 x = x1 + t * l / (2 * pi * n) y = y1 + A * sin t 

Finally, we need the direction vector to be (x2 - x1, y2 - y1) , and for now we have (1, 0) . Let the rotation angle be alpha, then

 cos alpha = (x2 - x1)/l sin alpha = (y2 - y1)/l 

(we denote these values ca and sa ).

We get l = sqrt((x2 - x1)^2 + (y2 - y1)^2) .

Rotation matrix

  ca sa -sa ca 

So, we have:

 darg = t * l / (2 * pi * n) dval = A * sin t x = x1 + ca * darg + sa * dval y = y1 - sa * darg + ca * dval 

( t runs through 0..2*n*pi , where n is the number of zigzags).

For convenience, you can replace t -> t / (2*n*pi) , then our calculations will be written as

 darg = t * l dval = A * sin (2 * n * pi * t) x = x1 + ca * darg + sa * dval y = y1 - sa * darg + ca * dval 

(here t runs through the segment 0..1).

  • @ Sugar: Yeah. There is a formula for it. - VladD
  • everything figured out. Thanks for the help. And with the rotation of the object during the movement will not help? Initially, the object is turned to the side of the point - Sugar
  • The rotation is exactly the same: we multiply the coordinates by the rotation matrix. Rotate around the point (x0, y0) by the angle phi : xnew = x0 + (xold - x0) * cos (phi) + (yold - y0) * sin (phi) ynew = y0 - (xold - x0) * sin (phi ) + (yold - y0) * cos (phi) - VladD

look, there are 2 points (x1, y1) (x2, y2) you need to make N zigzags, while the x and y movement may be uneven (because we would not reach the uniform one)

 dx = (x2-x1) / N dy = (y2-y1) / N x = x1; y = y1; for (i=1; i<=2*N; i++){ if (i%2 == 0) x+=dx; else y+=dy; } 

ps as far as I understand the task

Added by

If you need completely uniform zigzags, then you need to draw a line from point to point, and perpendicular to it, build zigzags, take the same sinusoid, rotate the graph to this line, and draw the vertices of the zigzag at maximum and minimum points of the sinusoid.

Good luck :)

  1. take a line from point to point, divide it by N even equal segments (segment length = D), i.e. these will be high points
  2. on odd points we build a perpendicular from the line (on each of the points on even ones on one side on odd ones on the other), and the length should be = D * square root (2) . This will be the tops of the zigzag
  • Zigzag goes, but the ladder and not symmetrical with respect to a straight line - Sugar Suk

I here painted the principle and threw an approximate plan of the algorithm:

alt text

  1. find the length between the red dots
  2. divide the length by the number of zigzags (waves) on the right plus two segments and get the wavelength (along the green line)
  3. the top of the wave following the start point is half a wavelength removed
  4. the next vertices are calculated equal to the wavelength and their number minus one wave
  5. come to the finish point

    with rotated coordinates of points, it is necessary to recalculate them taking into account the angle of rotation.