changed 90° offset to hex, fixed potential bug in FX using sin/cos with incrementing number

- sin/cos calls with incrementing numbers can lead to bad outcomes, the functions (_approx or original sinf/cosf) return bad values for very large float inputs
This commit is contained in:
Damian Schneider 2024-11-27 21:53:32 +01:00
parent 1a8aaa3b26
commit 4cd0563a93
2 changed files with 5 additions and 4 deletions

View File

@ -5314,8 +5314,8 @@ uint16_t mode_2DJulia(void) { // An animated Julia set
reAl = -0.94299f; // PixelBlaze example
imAg = 0.3162f;
reAl += sin_t((float)strip.now/305.f)/20.f;
imAg += sin_t((float)strip.now/405.f)/20.f;
reAl += (float)sin16_t(strip.now * 34) / 655340.f;
imAg += (float)sin16_t(strip.now * 26) / 655340.f;
dx = (xmax - xmin) / (cols); // Scale the delta x and y values to our matrix size.
dy = (ymax - ymin) / (rows);
@ -6279,6 +6279,7 @@ uint16_t mode_2Dplasmarotozoom() {
}
}
*a -= 0.03f + float(SEGENV.speed-128)*0.0002f; // rotation speed
if(*a < -6283.18530718f) *a += 6283.18530718f; // 1000*2*PI, protect sin/cos from very large input float values (will give wrong results)
return FRAMETIME;
}

View File

@ -76,7 +76,7 @@ int16_t sin16_t(uint16_t theta) {
}
int16_t cos16_t(uint16_t theta) {
return sin16_t(theta + 16384); //cos(x) = sin(x+pi/2)
return sin16_t(theta + 0x4000); //cos(x) = sin(x+pi/2)
}
uint8_t sin8_t(uint8_t theta) {
@ -98,7 +98,7 @@ float sin_approx(float theta) {
float cos_approx(float theta) {
uint16_t scaled_theta = (int)(theta * (float)(0xFFFF / M_TWOPI)); // note: do not cast negative float to uint! cast to int first (undefined on C3)
int32_t result = sin16_t(scaled_theta + 16384);
int32_t result = sin16_t(scaled_theta + 0x4000);
float cos = float(result) / 0x7FFF;
return cos;
}