I want to make an object, which moves from x1,y1 to x2,y2 in a straight line, also make a sinus over the line (so the x,0 is the line itself, and cux,cury is the object. So the object will move as a sinus over the line. How do I do this in c++?
First, a check:
You have two mathematical functions:
y = a*x + b
y' = sin(x')
And you want the latter to compute in a frame of reference, where the former defines the x'-axis?
Plotting over range is simple: you generate in a loop consecutive values of x (with some arbitrary step-size) and evaluate y from it.
You must decide where on the a*x+b lies the origin of the new frame of reference, i.e. at which x the x' is 0. For any other x, the x' is distance along the line from the origin (but you can compute x' step-size from x step-size). The y' is then a translation along axis that is perpendicular to the a*x+b from the point (x,y).
The above is surely not the only method. There was no C++ code here yet, because the first step is to nail down in detail what you are doing.
Simply, atm I'm just increasing x or y or both with one or more pixels, in steps of the shortest.
Like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int startx = 0; //start at 0,0
int starty = 0;
int reldestx = 100; //Destination pos to draw a line to, relative to start pos!
int reldesty = 50; //Destination pos to draw a line to, relative to start pos! So 100,50
float curx = 0; //Current pos, relative to start pos, not moved!
float cury = 0; //Current pos, relative to start pos, not moved!
float xspeed = (abs(reldesty))>abs(reldestx))?x/y?y/x; //X step/speed
float yspeed = (abs(reldestx)>abs(reldesty))?y/x:x/y; //Y step/speed
for (;((((int)curx)<(startx+reldestx)) || (((int)cury)<(starty+reldesty)));) //Not done drawing yet?
{
curx += xspeed; //X Step/speed!
cury += yspeed; //Y Step/speed!
plot(startx+((int)curx),starty+((int)cury),pixelcolor); //Plot the pixel!
}
I want to know the position relative of every plotted pixel above, so for every calculated plot above, i need the relative position of the sinus pixel plot relative to the plotted pixel above. As the position is already known (simply use =sin(sqrt(pow(curx,2)+pow(cury,2))) or a combination of the two), how do i angle it according to the x and y position and the calculated sinus? (How to turn the sinus to make it twist straight arround the line, as if it was just a horizontal line (only turned some degrees))
Your loop end condition (loop could be a 'while') uses relative (curx) against absolute (startx+reldestx). A logical error which does not show as long as startx==0.
Option 1:
Your line has an angle. You could construct a 2x2 rotation matrix M from the angle and multiply a position vector.
1 2 3 4 5 6 7 8 9
constdouble lenx = sqrt( pow(reldestx,2)+pow(reldesty,2) );
constdouble angle = ...
constdouble M[][] = { {cos(angle), -sin(angle)}, {sin(angle), cos(angle)} };
// Note that you don't need the trigonometric functions, since you have reldest*
...
double x0 = k * lenx; // where k is in range [0.0, 1.0]
double y0 = sin( x0 );
double v0[2] = { x0, v0 };
double v[2] = M * v0; // the magick, simple to implement
Option 2:
The vector math that I mentioned in previous comment.