From 70d9514881620ed3398004d5a01ba97d46d00f0d Mon Sep 17 00:00:00 2001 From: deskull Date: Thu, 18 Apr 2019 13:59:43 +0900 Subject: [PATCH] =?utf8?q?[Refactor]=20#37353=20distance()=20=E3=82=92=20g?= =?utf8?q?eometry.c/h=20=E3=81=B8=E7=A7=BB=E5=8B=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/externs.h | 1 - src/geometry.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/geometry.h | 1 + src/grid.c | 41 ----------------------------------------- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/externs.h b/src/externs.h index 3cea29d7e..0ebb5c539 100644 --- a/src/externs.h +++ b/src/externs.h @@ -415,7 +415,6 @@ extern void dump_yourself(FILE *fff); /* grids.c */ -extern POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2); extern bool los(POSITION y1, POSITION x1, POSITION y2, POSITION x2); extern void update_local_illumination(POSITION y, POSITION x); extern bool player_can_see_bold(POSITION y, POSITION x); diff --git a/src/geometry.c b/src/geometry.c index 8307ba5da..9688a9bcf 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -1,6 +1,46 @@ #include "angband.h" #include "floor.h" + +/*! + * @brief 2点間の距離をニュートン・ラプソン法で算出する / Distance between two points via Newton-Raphson technique + * @param y1 1点目のy座標 + * @param x1 1点目のx座標 + * @param y2 2点目のy座標 + * @param x2 2点目のx座標 + * @return 2点間の距離 + */ +POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2) +{ + POSITION dy = (y1 > y2) ? (y1 - y2) : (y2 - y1); + POSITION dx = (x1 > x2) ? (x1 - x2) : (x2 - x1); + + /* Squared distance */ + POSITION target = (dy * dy) + (dx * dx); + + /* Approximate distance: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2 */ + POSITION d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1)); + + POSITION err; + + /* Simple case */ + if (!dy || !dx) return d; + + while (1) + { + /* Approximate error */ + err = (target - d * d) / (2 * d); + + /* No error - we are done */ + if (!err) break; + + /* Adjust distance */ + d += err; + } + + return d; +} + /*! * @brief プレイヤーから指定の座標がどの方角にあるかを返す / * Convert an adjacent location to a direction. diff --git a/src/geometry.h b/src/geometry.h index 742a84a56..747b16047 100644 --- a/src/geometry.h +++ b/src/geometry.h @@ -23,6 +23,7 @@ extern DIRECTION coords_to_dir(POSITION y, POSITION x); #define PROJECT_LOS 0x8000 /*!< 遠隔攻撃特性: / */ extern sint project_path(u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flg); +extern POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2); extern bool projectable(POSITION y1, POSITION x1, POSITION y2, POSITION x2); extern void scatter(POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION d, BIT_FLAGS mode); extern void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2); diff --git a/src/grid.c b/src/grid.c index c19e34701..2b25e3a94 100644 --- a/src/grid.c +++ b/src/grid.c @@ -793,47 +793,6 @@ void place_bound_perm_wall(grid_type *g_ptr) place_solid_perm_grid(g_ptr); } - -/*! - * @brief 2点間の距離をニュートン・ラプソン法で算出する / Distance between two points via Newton-Raphson technique - * @param y1 1点目のy座標 - * @param x1 1点目のx座標 - * @param y2 2点目のy座標 - * @param x2 2点目のx座標 - * @return 2点間の距離 - */ -POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2) -{ - POSITION dy = (y1 > y2) ? (y1 - y2) : (y2 - y1); - POSITION dx = (x1 > x2) ? (x1 - x2) : (x2 - x1); - - /* Squared distance */ - POSITION target = (dy * dy) + (dx * dx); - - /* Approximate distance: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2 */ - POSITION d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1)); - - POSITION err; - - /* Simple case */ - if (!dy || !dx) return d; - - while (1) - { - /* Approximate error */ - err = (target - d * d) / (2 * d); - - /* No error - we are done */ - if (!err) break; - - /* Adjust distance */ - d += err; - } - - return d; -} - - /*! * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap * @param g_ptr マス構造体の参照ポインタ -- 2.11.0