1
$\begingroup$

UPDATE:

What I want is that given two points A and B, which are not in a straight line, I want a method thats gives me m and n. M and N are a couple of variables that adds to the previous coordinates. Therefore A' is (x0+m, y0+n). This method is going to be called a lot of times, to finally create a sinusoidal trajectory.

My main problem is that I don't really know how to put a sin equation with slope.

http://imageshack.us/photo/my-images/848/38386776.png/

I would be grateful if you could help me out with the pseudocode of the method thats obtains m and n values. This method has access to the actual coordinates.

Thank you very much.


OLD POST:

Ok...if I didn't explain my self... I have a particle in a point A (X0, Y0) and I want to move it in a small increment toward point B (X1, Y1). So my new point will be A' which will be at coordinates (X0+m, Y0+n).

I'm totally blocked trying to find a way of obtaining m and n in order to form a sinusoidal wave that moves from one point to another.

Please, notice that it moves in very small intervals, I mean m and n are really small. For example, if the distance between A and B is 1000px, the values for m and n would be between 1 and 5 px.

Thank you very much.

  • 0
    I know that there are pretty much sinusoidal wave. I don't actually require precission. I wanted an image to move forming a kind of wave, so any random sinusoidal should work...2012-04-10

3 Answers 3

1

Do you perhaps want to generate the x and y points for a sine wave, as a function of time, and then apply a linear/affine transformation?

A linear transformation includes rotation, shearing and stretching. An affine transformation can also include shifting the points up/down/left/right by a fixed amount.

enter image description here

The MATLAB code that produced the above figure is as follows. Most of it is declarations of the desired characteristics of the sine wave, its size, the rotation angles, and so on.

The core of what you need is just a matrix multiplication of the old point x1,y1 by a linear transformation matrix, which rotates, scales and skews, followed by adding the constant up/down/left/right shift. This gives the new transformed point x2,y2.

If you generalise to an "affine" transform, then you can do all of this in one 3x3 matrix multiplication. This is just as easy, but the maths is (very slightly) more complicated. For this example I'll stick with the "most obvious" way.

% Define sine wave amplitude and frequency frequency = 5; amplitude = 2;  % Calculate sine wave (x,y) points over time interval t t = 0:0.01:2*pi;  % vector of time values - 0 to 2*pi in steps of 0.01 x1 = t; y1 = amplitude * sin(frequency*t);  % Produce plot plot (x1,y1); title 'original' grid  % Define transform parameters scale = 5; % 5 times bigger  shift = [ -15           -10 ]; % shift 15 units left, and 10 units down  theta = pi/6; % 30 degrees  rotation_matrix = [ cos(theta), -sin(theta)                     sin(theta),  cos(theta)  ];  % Transform points via scaling, shifting, and rotation % make empty arrays to store new points in                 x2 = zeros(size(t)); y2 = zeros(size(t)); % Transform points one at a time for i = 1:length(t)     old_xy = [ x1(i)                y1(i) ];     % calculate transformed x and y      new_xy = ( rotation_matrix * old_xy * scale ) + shift;      % Store calculated values to vector     x2(i) = new_xy(1);     y2(i) = new_xy(2); end  % Make a pretty picture figure; plot(x2,y2); set(gca,'XLim',[-20,20],'YLim',[-20,20]) title 'transformed' grid 

The ingredients for what you want are here: you just have to figure out how to get the sine wave to start and stop at your desired endpoints. I leave this exercise in trigonometry up to you.

(Note: If you can't figure out how to find the transformation that will give a sine wave starting a point A and ending at point B, it would be better to ask math.stackexchange.com instead of StackOverflow - that would be a geometry question, not a programming question. Do post links between the related questions, though.)


Edit 2: Slightly incorrect use of mathematical terms - translation is not a linear transformation; it's actually an affine transform, where affine transforms are the more general version of linear transforms.

  • 0
    This can work perfectly for me. Thank you very much.2012-04-10
0

The final endpoint doesn't matter. That's when you stop plotting when X >= ?? The increment is an angle, whether it's worth plotting, is the deltaX + deltaY >= 1, unless it's time based as well in which case you might want to show no "visible" change in the time slice.

  • 0
    It would be polite to say why whoever marked me down did so....2012-04-10
0

It sounds like you're looking for smooth half-sine motion from A to B, eg 0.5 + 0.5*sin(p), where p is is -pi/2 to +pi/2. If not you can pick whatever porion of a sine you wish by changing the start and end phases. The "fineness" of the points is set by the len.

void buildPointSet(int X0, int Y0, int X1, int Y1, int** points, int len) {    int i;    double xm, ym, p, pinc;    double pi = 3.1415926536;    double pstart = -pi/2.0, pend = pi/2.0, p;     pinc = (pend - pstart) / ((double)(len - 1));    for (i = 0, p = pstart; i < len; i++, p += pinc)    {       double fraction = 0.5 + ( 0.5 * sin(p) );       xm = ( (1.0 - fraction) * ((double)X0) ) + ( fraction * ((double)X1) );       ym = ( (1.0 - fraction) * ((double)Y0) ) + ( fraction * ((double)Y1) );       points[i][0] = (int)xm;       points[i][1] = (int)ym;    } // end for } 
  • 0
    This is closer to what I actually want to do. However, I want a sloped sine. Check this out: http://imageshack.us/photo/my-images/848/38386776.png/ Do you have any clue a$b$out how to work it out??2012-04-10