From f6d5e9337b1b4838dff44d70d8154bf31e7dfaf0 Mon Sep 17 00:00:00 2001 From: Divye Joshi Date: Fri, 30 Jan 2026 22:09:33 +0530 Subject: [PATCH] feat: implement 2D and 3D taxicab (Manhattan) metrics --- csa/geometry.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/csa/geometry.py b/csa/geometry.py index c83e470..81e46e2 100644 --- a/csa/geometry.py +++ b/csa/geometry.py @@ -169,3 +169,50 @@ def euclidMetric3d (g1, g2 = None): """ g2 = g1 if g2 == None else g2 return lambda i, j: euclidDistance3d (g1 (i), g2 (j)) + + +def taxicabDistance2d(p1, p2): + """Returns the taxicab (Manhattan) distance in 2D between two points + :param p1: The first point + :type p1: tuple (x, y) + :param p2: The second point + :type p2: tuple (x, y) + :return: The taxicab distance + :rtype: float + """ + return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) + +def taxicabMetric2d(g1, g2=None): + """Returns a taxicab metric for 2D points + :param g1: The first group of points + :type g1: callable + :param g2: The second group of points. If None, the first group of points is used + :type g2: callable + :return: A 2D taxicab metric function + :rtype: function + """ + g2 = g1 if g2 is None else g2 + return lambda i, j: taxicabDistance2d(g1(i), g2(j)) + +def taxicabDistance3d(p1, p2): + """Returns the taxicab (Manhattan) distance in 3D between two points + :param p1: The first point + :type p1: numpy.array((3)) + :param p2: The second point + :type p2: numpy.array((3)) + :return: The taxicab distance + :rtype: float + """ + return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) + abs(p1[2] - p2[2]) + +def taxicabMetric3d(g1, g2=None): + """Returns a taxicab metric for 3D points + :param g1: The first group of points + :type g1: callable + :param g2: The second group of points. If None, the first group of points is used + :type g2: callable + :return: A 3D taxicab metric function + :rtype: function + """ + g2 = g1 if g2 is None else g2 + return lambda i, j: taxicabDistance3d(g1(i), g2(j)) \ No newline at end of file