■················.
Q: Please explain to me the meaning of how the sine is calculated
table. It seems, as I understand it, the table is calculated
sint=round(sin(i/256)*127+128), whereas for example calculate
sine 30rp., that is sin(pi/6)? And in general, what kind of magical
numbers 127,128?
A: [Alexander Shulepov 2:5020/1737.25]
Let's start with the basics. In a circle there are 360 degrees, or 2*Pi. But the number
360 is somewhat inconvenient for us, because if we use
precalculate (that is, creating an array with sin/cos values in
each of 360 degrees) we will have to track situations when
the sine/cosine angle we want to find is outside
gap 0..360 degrees (actually 0..359, since 0 and
360 degrees are the same angle) and form this
the angle corresponding to it, lying in the indicated interval. How
can we avoid this? Yes, it’s very simple, let’s divide the circle not by 360
parts, but at 256, then the problem of tracking the value is removed
corner. It is enough for us to use the char type for the variable in
which stores the angle value. All checks are carried out
automatically and do not consume resources. Formulas for conversions
are derived simply. Now about the magic numbers... Almost
everywheretry to replace floating point numbers with numbers with
fixed point. If fixed point y us x.8, then all numbers with
floating point are multiplied by 256. In the example with water, the array
height was of type unsigned char, that is, the maximum height is
255. This is where the numbers 127, 128 come from. 128 is the level
calm water, and 127 is the amplitude of wave oscillations. That's the end result
and the formula is: sintable[k]=sin(2*PI*k/256)*127+128;
Share your thoughts about the article