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).