Mercurial > imath
changeset 2:5ab4f6048553 draft
Fix the for loop in atan
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Wed, 21 Dec 2016 23:43:10 +0100 |
| parents | a56bbe3b03df |
| children | 230eac2a984f |
| files | imath.c |
| diffstat | 1 files changed, 12 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/imath.c Wed Dec 21 22:28:44 2016 +0100 +++ b/imath.c Wed Dec 21 23:43:10 2016 +0100 @@ -31,7 +31,6 @@ int16_t iatan2(int16_t y, int16_t x) { int16_t result = 0; uint16_t tx, ux, uy; - uint8_t i = 0; if (x == 0 && y == 0) { return 0; @@ -54,7 +53,7 @@ ux <<= 1; uy <<= 1; } - while (i < ITERATIONS) { + for (uint8_t i = 0; i < ITERATIONS; ++i) { if (uy > ux >> i) { tx = ux; ux += uy >> i; @@ -62,7 +61,7 @@ result += pgm_read_word(&ATAN[i]); } } - return result + (pgm_read_word(&ATAN[i - 1]) >> 1); + return result + (pgm_read_word(&ATAN[ITERATIONS - 1]) >> 1); } /* @@ -73,20 +72,21 @@ int16_t result = 0; int16_t x = SCALE; int16_t y = 0; - int16_t tx; + int16_t dx, dy, dr; for (uint8_t i = 0; i < ITERATIONS; ++i) { for (uint8_t j = 0; j < 2; ++j) { - tx = x; + dx = y >> i; + dy = -(x >> i); + dr = -pgm_read_word(&ATAN[i]); if (y < value) { - x -= y >> i; - y += tx >> i; - result += pgm_read_word(&ATAN[i]); - } else { - x += y >> i; - y -= tx >> i; - result -= pgm_read_word(&ATAN[i]); + dx = -dx; + dy = -dy; + dr = -dr; } + x += dx; + y += dy; + result += dr; } value += value >> (i << 1); }
