#### Archived

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

# Sine wave - matching start-end heights on grid

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

## Recommended Posts

Hi all, I have a grid over which I calculate a sine wave height for each coordinate using the following routine:
// setup our own grid and populate with sine wave points
int xx, zz;
float sinTmp;
float amplitude = 50.0f;
for ( int i = 0; i <= gsize; i++ ) {
for ( int j = 0; j <= gsize; j++ ) {
zz = i-(gsize/2); xx = j-(gsize/2);
sinTmp = ((float)xx*amplitude/360.0f)*2.0f*PI;
CPoint[i][j].Set((float)xx,(float)sin(sinTmp),(float)zz);
}
}


The sine wave looks fine when I plot it out, with each end being equi-distant from zero in the negative and positive directions. eg. 0.64 and -0.64 for left and right ends respectively. However, if I scroll the height values over the grid to give the effect of the grid undulating, the difference between the values at the two ends causes a noticeable glitch in the otherwise smooth scrolling sine. Anyone know how to calculate the grid such that both ends start and end at zero? I guess I could hack this by placing a zero height value in the first column of the grid and calculating over gsize-1, but it would be nice to get the correct formula. Cheers. <SPAN CLASS=editedby>[edited by - Fahrenheit451 on May 30, 2004 6:24:19 AM]</SPAN> [Edited by - Fahrenheit451 on January 7, 2006 11:06:27 AM]

##### Share on other sites
Well, sin(x) is zero when x = k*PI, k being some arbitrary integer. So simply make sure your units are a multiple of PI where ends meet. This means you won''t be able to use integers as loop variables, since they can never be equal to a multiple of PI. The exception is if you multiply by a special factor in the sine parameter, but that might be more difficult to manage. What I''d do is change the loop variables to floats, and increase them by fractions of PI (ex. PI/2, PI/4, PI/8) which would increase your resolution while ensuring that you can match ends. Depending on which you use, you will only be able to match ends every 2, 4, or 8 iterations, using the above increments, since that will be when the loop variables are multiples of PI.

Also, there are a few small issues with your code as it is. First, if you truly intend the variable amplitude to affect the amplitude of the sine wave, it needs to multiply the result of sin, and not the inner parameter. Right now, it''s increasing the frequency of the sine wave by a factor of 50. This might be the effect you want, but just letting you know

##### Share on other sites
My intentions are not to hijack this post, but it appears as if the question has been sufficiently answered: Zipster were you ever an avid poster on Qbasic.com?

• 9
• 13
• 41
• 15
• 14