CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Jan 2012
    Posts
    49

    [RESOLVED] how to calculate x & y in a sine wave?

    Hello,

    I have the following initial code:

    Code:
    double objectAngle = -90.0f;
    double objectSpeed = 3.0f; // meters per second
    double objectDistance = 300.0f;
    double objectTime = 0.0f;
    double objectHeading = 90.0f;
    double elapsedTimePerCycle = 1.0f;
    My coordinate system looks like this:

    Code:
    	                        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

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by aseminov View Post
    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.

  3. #3
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    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?

    Thanks,
    Alex

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to calculate x & y in a sine wave?

    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.

  5. #5
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    Hello Monarch_Dodra,

    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.

    Thanks,
    Alex

  6. #6
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by aseminov View Post
    Hello,

    I have the following initial code:

    Code:
    double objectAngle = -90.0f;
    double objectSpeed = 3.0f; // meters per second
    double objectDistance = 300.0f;
    double objectTime = 0.0f;
    double objectHeading = 90.0f;
    double elapsedTimePerCycle = 1.0f;
    My coordinate system looks like this:

    Code:
                                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.

    For example, the following data
    Code:
    double objectAngle = -90.0f;
    double objectSpeed = 3.0f; // meters per second
    double objectDistance = 300.0f;
    double objectTime = 0.0f;
    double objectHeading = 90.0f;
    double elapsedTimePerCycle = 1.0f;
    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?

  7. #7
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    Hello PredicateNormative,

    I just want to be able to generate the x/y pairs in a sin wave pattern.

  8. #8
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to calculate x & y in a sine wave?

    Does that mean that your data can be modified? e.g:

    Code:
    double objectAngle = -90.0f;
    double objectSpeed = 30.0f; // meters per second
    double objectDistance = 0.0f;
    double objectTime = 0.0f;
    double objectHeading = 90.0f;
    double elapsedTimePerCycle = 1.0f;
    double accelerationDueToGravity = 3.0; //meters per second squared
    double centreOfGravityX= 0.0;
    double centerOfGravityY = 0.0;
    The above modifications would cause the object to oscillate around point (0,0) in a sine wave pattern.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,133

    Re: how to calculate x & y in a sine wave?

    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.

  10. #10
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    ok attached is the drawing of what I want to do.

    Thanks!
    Attached Images Attached Images

  11. #11
    Join Date
    Jan 2010
    Posts
    1,133

    Re: how to calculate x & y in a sine wave?

    Ok.

    Take a look at the sine function graph:


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

  12. #12
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    ok I will take a look at this.. do you have any example code for each second? like:

    new_x = ?
    new_y = ?

    Thanks!
    Alex

  13. #13
    Join Date
    Jan 2010
    Posts
    1,133

    Re: how to calculate x & y in a sine wave?

    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.

  14. #14
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    Yes that is exactly what I want!

    What is the 2.5 part in the calculation of x?

    Thanks so much for your help!

    Alex

  15. #15
    Join Date
    Jan 2010
    Posts
    1,133

    Re: how to calculate x & y in a sine wave?

    In my older post (the one where I rotated the image) I said that if you count the number of periods for the sine function you sketched, you get 2.5.

    Sin(x) is a periodic function - the same values repeat for every 360 degs (or 2*Pi radians).
    This is one period:


    In your case, that image repeats itself 2.5 times - so that's what's 2.5 for.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured