开发者

How to calculate the Manhattan Distance with SQL function / procedure

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜