Qbasicnews.com

Full Version: c++ sin wave??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
WTF :o i tried to draw a sin wave something like this in c++

Code:
int xpos=0

for (int i=0; i<=360; i++)
{
   putpixel (screen, xpos, 100+(sin(i)*50), color);
   xpos++;
}

and i discovered the wave only needs +- 257 degrees for a full cycle!!!!!

Now i know i normally sleep through maths, but aint it ment to have 360 degrees???
Quote:But what is that? There are some strange things in this piece of code. First of all, why does it say: while (angle < 2 * PI)? What does that mean? And why is the angle_increment such a low value? You would think that with such a low value the points on the circle would be very close to each other, but they aren't. How can we explain that?

The answer is that the sin and cos functions don't take regular degrees, which you might be used to, as an argument. There are 360 degrees in a full circle - this number is thought to have come from an old estimate of the number of days in the year, or to have been chosen because it has so many factors. But the sin and cos functions want radians, not degrees. There are 2 * PI radians in a circle, PI being a mathematical constant of roughly 3.1415927. So there are roughly 6.282 radians in a circle. Why do they make things so difficult, you may ask? Well, that is all figured out by mathematicians, and mathematicians are a mysterious kind of people.

To make our lives easier, we can calculate the number of degrees from the number of radians and vice versa. We do it as follows:

degrees = radians * 180 / PI;
radians = degrees * PI / 180;

They dont take regular degrees Wink

And uh, it looks like your using allegro, so question, did you
#include "math.h"? if not your using allegros sin+cos replacement where the use allegro degrees so you can do

allegro_degrees &= 0xFF; // keep the lowest 8 bits

To keep it from going over 255 rather than if < or if > etc
Well, they're morons. The fools who created 360 degress, that is.

It should be a power of 2.
I know about the radians conversions (needed in QB aswell).

What I didnt do is include math.h tho. so im using allegros substitues i guess. If i include mathsh.h will i be using the 'standard', if you will, c++ sin/cos?
yes, at least for that compiler.
Quote:
I know about the radians conversions (needed in QB aswell).

What I didnt do is include math.h tho. so im using allegros substitues i guess. If i include mathsh.h will i be using the 'standard', if you will, c++ sin/cos?

Yep, allegro degress are 0-255. fsin and fcos take 255 to and so do all allegro rotate funtions.

it's math.h, but i guess thats an intentianal typing error, and yes ti gives you 360degrees.
i dont get it... why would you want 255? or 360? if i want to moves .1 degrees to the left, will it jerk or move smoothly to the side. I'd use 1024, or 2048, or however high a multiple of 2 i needed.
Well, allegro uses the fixed data type for angles, so it has 256 degrees with 256 steps each degrees, which gives you an accuracy of 256*256 = 65536 steps in the whole circle.
oh. (i'd still prefer just 0-65535)