From 04112de29701267ec045ca29a7ef31946caad5f5 Mon Sep 17 00:00:00 2001 From: Jack Zhou Date: Mon, 4 Jun 2018 01:55:13 -0400 Subject: [PATCH] Approximate sqrt(2)/2 with ints to avoid floats --- src/MouseWrapper.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/MouseWrapper.cpp b/src/MouseWrapper.cpp index 06403449..082b1fde 100644 --- a/src/MouseWrapper.cpp +++ b/src/MouseWrapper.cpp @@ -112,24 +112,19 @@ uint8_t MouseWrapper_::acceleration(uint8_t cycles) { return i; } -static int8_t roundAwayFromZero(float value) { - if (value < 0.0f) { - return static_cast(value - 0.5f); - } - - return static_cast(value + 0.5f); -} - void MouseWrapper_::move(int8_t x, int8_t y) { int16_t moveX = 0; int16_t moveY = 0; static int8_t remainderX = 0; static int8_t remainderY = 0; - static const float HALF_SQRT_2 = 0.7071f; if (x != 0 && y != 0) { - x = roundAwayFromZero(HALF_SQRT_2 * x); - y = roundAwayFromZero(HALF_SQRT_2 * y); + // 99 / 140 closely approximates sqrt(2) / 2. + int8_t adjusted_x = x * 99 / 140; + int8_t adjusted_y = y * 99 / 140; + + x = (adjusted_x == 0 ? x : adjusted_x); + y = (adjusted_y == 0 ? y : adjusted_y); } if (x != 0) {