90 y
|
|
|
+-180 -------------------- 0 x
|
|
|
-90
I want to calculate a new x,y such that it follows a sine wave like pattern until it gets to point (0,0). I want the width of the arc to go out to about -45 degrees, and the same on the other side to about -135 degrees.
How can I compute these new x,y coordinates in C++?
I want to calculate a new x,y such that it follows a sine wave like pattern until it gets to point (0,0). I want the width of the arc to go out to about -45 degrees, and the same on the other side to about -135 degrees.
I don't understand.
You want the formula that gives you the function you describe? The function you describe is y = -sin(x), so just do that.
sin can be found in <math.h> or <cmath>. Just remember that all C trig functions operate on radians.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Yes, but doesn't distance and play a key in the equation? Do I decrement the distance each calculation? How can I extend the x coordinate so it's around + or - 45 degrees from the y axis?
Can you provide some simple example C++ code so I can learn how this works?
You posted code, but what you are trying to do is not very clear.
If I understood correctly, you are trying to find the equation of a plot?
This plot describes the motion of an object?
And you want this object to cross the (0,0) coordinate at a given angle?
Please try to give a bit more background to what you are trying to do, and we'll have an easier time with the how.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
I am trying to calculate the x and y position of the object ever second. Sorry if I was not clear on that. I would also like to calculate the new distance based on the speed. I want the object to move toward point (0,0) in a sine wave pattern.
90 y
|
|
|
+-180 -------------------- 0 x
|
|
|
-90
I want to calculate a new x,y such that it follows a sine wave like pattern until it gets to point (0,0). I want the width of the arc to go out to about -45 degrees, and the same on the other side to about -135 degrees.
How can I compute these new x,y coordinates in C++?
Thanks!
Alex
Alex, what you are asking does not match the data you are supplying.
suggests that at t=0, the object is lying on the y axis at x=0, y=-300, and that the object is travelling on the y axis in a positive direction at a constant speed of 3m/s.
Therefore the data suggests that the points that you should plot are going to be
Code:
x = 0, y = -300
x = 0, y = -297
x = 0, y = -294
...
x = 0, y = 0
x = 0, y = 3
x = 0, y = 6
...
Newtons first law of motion states that an object will continue at a constant velocity, unless acted on by an external force.
There are no accelerations mentioned, or anything else in the data that would suggest that the object should oscillate around a point or have wave like behaviour. This information is missing from your description and is required in order to help you come up with a solution to your, as yet, unbound problem. Please can you explain why you think that the object should be moving in a sine wave pattern?
Yes, but, you must understand that "I just want to be able to generate the x/y pairs in a sin wave pattern" doesn't clearly describe what you want to do.
The best guess I can deduce from what you've posted thus far is that you want to construct a parametric curve that will increase or decrease it's distance from a fixed circle based on the angle between the radius and the x-axis, ranging in (-180, +180)?
This would produce a flower-like pattern.
Am I right?
P.S. Please don't double post.
P.P.S. As I said there - a simple sketch of the end result would help us understand; you can attach it here, or link to some online image repository.
Last edited by TheGreatCthulhu; April 24th, 2012 at 09:20 AM.
It has a period of 2Pi, (0-360 deg), so this just repeats left and right.
In your case, the easiest thing to do is to think of your curve being on a transformed coordinate system, and calculating x=f(y), instead of y=f(x).
By this, I don't mean an inverse function, because the curve you described is not a function (by definition, for each x, f(x) must have no more than one value in order to qualify as a function - that's why arcsin has a limited codomain.)
I'm saying to think of y as input, and x as output.
I rotated your sketch:
Note that the x-axis is oriented downwards. This means this is basically a sligtly modified x= -sin(y) function.
If you count, there are 2.5 periods from y1 to y2.
So you need to map [-2.5Pi, 0] range to the [y1, y2] range.
EDIT: by using the two point form for a linear map:
t = y*(y2-y1)/(2.5Pi) + y2 // ERROR - confused the axes and corresponding points...
EDIT1: Sry, made a mistake:
t = y * 2.5Pi/(-y1)
EDIT2: Sry, made a mistake, again - I said 2.5 periods, and the period is 2Pi, so: t = y * (2.5 * 2 * Pi) / (-y1)
Then you find x = -sin(t).
The sine function values range from -1 to 1, so to get the "peaks" at x2 and "valleys" at x1, you symply need to multiply the result to x2 (remember, it's upside-down).
Last edited by TheGreatCthulhu; April 24th, 2012 at 01:23 PM.
Reason: Again, the equation...
After re-reading your posts, it occurred to me that this better corresponds to what you're looking for, right?
If so, the principle is the same, you only need to scale the [-1,1] range returned by the sine function to the [-45, 45] range (just multiply by 45), and move the graph along the x-axis by subtracting -90 from the result.
Remember, the way I set it up, is that you're calculating x based on y.
So, x = f(y).
If you want to update per time step, then you can make your be a function of time t (note: this t (italic&blue) is not the same as the t (regular) in my previous post).
So, you now have:
x = f(y), which decides how far to the side the object will go, and
y = g(t), which tells where along the y-axis the object is;
Assuming that the y-speed is constant, you would calculate y like this:
y = y1 + v*t,
where v is y-speed, y1 is the starting position, and t is the time elapsed since the last step
Then you would calculate x, scaling the result by 45, and moving it along the x-axis by -90:
x = -sin( y * 2.5 * 2*Pi / -y1 ) * 45 - 90,
where y1 is your "start" (a negative value, see graph).
Note that this approach requires you to calculate y first.
P.S. With this setup, the object traveling at v of 1 unit per second will reach y = 0 when t reaches -y1.
You can modify this behavior by modifying the equation for y, or the value of v.
Last edited by TheGreatCthulhu; April 24th, 2012 at 01:59 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.