Approximate sqrt(2)/2 with ints to avoid floats

pull/365/head
Jack Zhou 6 years ago
parent 3805b53cad
commit 04112de297
No known key found for this signature in database
GPG Key ID: C2E29FDA9C9261E1

@ -112,24 +112,19 @@ uint8_t MouseWrapper_::acceleration(uint8_t cycles) {
return i; return i;
} }
static int8_t roundAwayFromZero(float value) {
if (value < 0.0f) {
return static_cast<int8_t>(value - 0.5f);
}
return static_cast<int8_t>(value + 0.5f);
}
void MouseWrapper_::move(int8_t x, int8_t y) { void MouseWrapper_::move(int8_t x, int8_t y) {
int16_t moveX = 0; int16_t moveX = 0;
int16_t moveY = 0; int16_t moveY = 0;
static int8_t remainderX = 0; static int8_t remainderX = 0;
static int8_t remainderY = 0; static int8_t remainderY = 0;
static const float HALF_SQRT_2 = 0.7071f;
if (x != 0 && y != 0) { if (x != 0 && y != 0) {
x = roundAwayFromZero(HALF_SQRT_2 * x); // 99 / 140 closely approximates sqrt(2) / 2.
y = roundAwayFromZero(HALF_SQRT_2 * y); 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) { if (x != 0) {

Loading…
Cancel
Save