#### Archived

This topic is now archived and is closed to further replies.

# Sine Function Calculate X,Y values?

This topic is 5467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I made an app that simulates like water ripples on a bitmap and id like to make a ripple in the water constantly move in a sine wave like matter. So it goes up and down left to right, I was wondering if anyone has a function to calculate a basic path for X,Y cordinates along a sine wave that would have the ablity to go back and forth? coder requires 0xf00d before continue().
Killer Eagle Software

##### Share on other sites
basicaly, you are having a counter that increments every tick.

every tick, you would set X to be sin(k*counter) and Y to cos(k*counter). k is a constant that you can play arount to see diferent results. The sin and cos function take inputs as radiants, and they wrap values arount (so passing sin(12432434) will acualy yeald sin(-1+(12432434%2)) because the domain is (-1,+1)).

This means that (depending on your k value) every tick, the X and Y will go back and forth, creating a circle if you plot a pixel eatch tick.

I hope that is what you are looking for

edit: try a k value of 200 or somthing because sin/cos output -1.0 to +1.0

[edited by - aftermath on October 6, 2003 10:57:20 PM]

##### Share on other sites
Initially, I would think to map pixel coordinates (x,y) in the source image to (x + A*cos(B*t), y + C*cos(D*t + E)) where A and C are scaling factors that determine how much the pixels move in each direction, t is the time, updated each time you redraw, B and D are time rate constants that adjust how fast the ripples move and E is a phase constant that you can use to adjust the shape of the movement of the pixels (E = 0 would move them in a line back and forth, E = Pi/2 would move them in a circle, E = -Pi/2 would move then in a circle in the opposite direction, etc).

However, this would lead to problems in that the destination image would have gaps between pixels in some cases where the density is lower due to the geometry of the mapping. Instead, you could scan over the destination image and use the mapping above (in reverse, ie. get source coords for given destination coords) to select source pixel to use, or possibly several pixels to average for the destination pixel. You would have to be careful to not attempt to sample the source image outside it''s defined area, which could possibly mean using the nearest edge pixel when the mapping fall outside the source image.

##### Share on other sites
heh

OnTimer(UINT nIDEvent){ UpdateDisplay(); x=sin(350*count); y=cos(350*count); CDC *pDC = GetDC(); pDC->Rectangle(x,y,x+10,y+10); ReleaseDC(pDC); count++;}
its not moving 0.o timer is set and all just not moving at all its stuck in the top corner

[edited by - DevLiquidKnight on October 6, 2003 11:36:24 PM]

##### Share on other sites
Tried (x + A*cos(B*t), y + C*cos(D*t + E)) lol.. not sure what values will fit on the screen cant see it its so huge of values

##### Share on other sites
AfTeRmAtH and I read your post differently. I''m not sure which of us (if either) is closer to what you want.

If you''re looking for a circular motion, AfTeRmAtH is mostly correct (except that the range is [-1, 1], and the domain is (-PI, PI]; the function repeats every 2PI, not every 2).

If you want a sin wave that extends across the whole bitmap and moves left or right, then you''re not looking for both X and Y ; you need to find Y for every X and T .

If T is time in frames, seconds, clock ticks, years, or whatever time unit you want to use, then the most general formula for a moving sine wave is:

Y(X, T) = A * sin[(B * X) - (C * T) - D] + E
A, B, C, D, and E are all constants you define (probably by experimentation).
--A defines the amplitude, how tall the wave is. Larger A means taller.
--B defines the period, how wide or narrow the wave is. Larger B means narrower.
--C defines the speed and direction of the wave. Higher abs(C) means a faster wave. If C is positive, the wave moves right (assuming positive X is to the right); if C is negative, the wave moves left.
--D is the amount the wave has moved before Frame 0. It''s the initial horizontal offset.
--E is the vertical offset of the wave.

You may not care about some of these constants; in that case, just omit them. If this isn''t what you wanted, I apologise.

##### Share on other sites
Never mind that. I misunderstood what you were doing. Sorry :/

##### Share on other sites
Anonymous Poster

Y(X, T) = A * sin[(B * X) - (C * T) - D] + E
now this is intresting its going up and down left to right 0.o really fast not really in a sine pattern but thats cuz the x is kinda weird? im just incremeting x by doing x++; it doesnt have the whole nice smooth effect really jumpy

[edited by - DevLiquidKnight on October 6, 2003 12:01:12 AM]

##### Share on other sites
quote:
Original post by DevLiquidKnight
Tried (x + A*cos(B*t), y + C*cos(D*t + E)) lol.. not sure what values will fit on the screen cant see it its so huge of values

I''m dumb. I forgot to put the x and y values into the actual cos functions... try this instead: (x + A*cos(B*t + F*x + G*y), y + C*cos(D*t + F*x + G*y + E)). Sorry about the even more constants...

A and C will determine the distance (in pixels) that a source imagine pixel will move in the x and y directions of the rippled image. Values of 1 to 20 or so might work. B and D will determine how fast the movement occurs, and depend on what you''re using to get the time values, so do some experimentation. E is a phase angle that works as I described above, and any value should work. F and G are the critical values I forgot last time, which affect how large the ripples are. If you set F = 2*Pi/10 and G = 0, then the ripples will have a wavelength of 10 pixels. This would mean that pixels in the output image will always be shifted from their source imagage locations by the same amount as the pixels 10 pixels away on either side. They will "move in phase". Hopefully I didn''t forget anything else, though it sounds like you might have gotten this working already anyway...

1. 1
2. 2
3. 3
Rutin
17
4. 4
5. 5
JoeJ
13

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007628
• ### Who's Online (See full list)

There are no registered users currently online

×