From cdfefa6c996b0e159d19a8a36ff4105bcf24b76c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Fri, 14 Sep 2018 14:40:31 +0200 Subject: [PATCH] Add octagonal approx back --- raycastlib.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 5615655..f78ba1a 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -35,20 +35,21 @@ #define UNITS_PER_SQUARE 128 typedef int16_t Unit; #define UNIT_INFINITY 32767; - #define USE_DIST_APPROX 1 + #define USE_DIST_APPROX 2 #endif #ifndef USE_COS_LUT #define USE_COS_LUT 0 /**< type of look up table for cos function: 0: none (compute) 1: 64 items - 2: 128 items - */ + 2: 128 items */ #endif #ifndef USE_DIST_APPROX -#define USE_DIST_APPROX 0 /** Whether to use distance approximation (1) or - real Euclidean distance (0, slower). */ +#define USE_DIST_APPROX 0 /** What distance approximation to use: + 0: none (compute full Euclidean distance) + 1: accurate approximation + 2: octagonal approximation (LQ) */ #endif #ifndef VERTICAL_FOV @@ -488,8 +489,15 @@ Unit dist(Vector2D p1, Vector2D p2) Unit dx = p2.x - p1.x; Unit dy = p2.y - p1.y; -#if USE_DIST_APPROX == 1 - // Euclidean distance approximation +#if USE_DIST_APPROX == 2 + // octagonal approximation + + dx = absVal(dx); + dy = absVal(dy); + + return dy > dx ? dx / 2 + dy : dy / 2 + dx; +#elif USE_DIST_APPROX == 1 + // more accurate approximation Unit a, b, result;