开发者

How to calculate the Manhattan Distance with SQL function / procedure

开发者 https://www.devze.com 2023-01-24 00:07 出处:网络
Does anybody have a simple function / store procedure on how to calcula开发者_如何学运维te the Manhattan Distance when given two XY coordinate pairs?It\'s just the sum of the difference between coordi

Does anybody have a simple function / store procedure on how to calcula开发者_如何学运维te the Manhattan Distance when given two XY coordinate pairs?


It's just the sum of the difference between coordinate pairs:

|x2 - x1| + |y2 - y1|

This hardly warrants a stored procedure.


How about

CREATE OR REPLACE FUNCTION MANHATTAN_DISTANCE(X1 NUMBER,
                                              Y1 NUMBER,
                                              X2 NUMBER,
                                              Y2 NUMBER)
  RETURN NUMBER IS
BEGIN
  RETURN ABS(X1 - X2) + ABS(Y1 - Y2);
END MANHATTAN_DISTANCE;

Share and enjoy.


This is easy. The Manhattan Distance is just the sum of the distances in each dimension, so:

SELECT ABS(x2 - x1) + ABS(y2 - y1) AS manhattan_distance FROM your_table
0

精彩评论

暂无评论...
验证码 换一张
取 消