import numpy as np # calculate the distance between two points in 2d,3d ... nD def distanceND(pA, pB, nD = None): return np.linalg.norm(np.array(pA) - np.array(pB)) distance2D = distanceND distance3D =distanceND
from math import sqrt, pow- import numpy as np
- # calculate the distance between two points in 2d,3d ... nD
- def distanceND(pA, pB, nD = None):
return sqrt(sum(pow(a-b,2) for a,b in zip(pA,pB)))- return np.linalg.norm(np.array(pA) - np.array(pB))
- distance2D = distanceND
- distance3D =distanceND
from math import sqrt def distance2D_check(pA, pB): if pA == pB: return 0 xA, yA = tuple(pA); xB, yB = tuple(pB) return sqrt((xA - xB)**2 + (yA - yB)**2) def distance3D_check(pA, pB): if pA == pB: return 0 xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB) return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2) def assertFuzzyEquals(actual, expected, msg=""): merr = 1e-10 inrange = abs(actual - expected) <= merr if (inrange == False): msg = "At 1e-10: Expected value must be {:.10f} but got {:.10f}" msg = msg.format(expected, actual) return Test.expect(inrange, msg) test.describe("Basic Tests for 2D") pA = [1, 1]; pB = [2, 2] assertFuzzyEquals(distance2D(pA, pB), 1.41421356237) pA = [0, 0]; pB = [5, 6] assertFuzzyEquals(distance2D(pA, pB), 7.81024967591) pA = [-5, 4]; pB = [-10, 6] assertFuzzyEquals(distance2D(pA, pB), 5.38516480713) pA = [-5.1, 4.0]; pB = [10.2, -6.3] assertFuzzyEquals(distance2D(pA, pB), 18.4439692041) test.describe("Basic Tests for 3D") pA = [1, 1, 1]; pB = [2, 2, 2] assertFuzzyEquals(distance3D(pA, pB), 1.73205080757) pA = [0, 0, 0]; pB = [7, 8, 9] assertFuzzyEquals(distance3D(pA, pB), 13.9283882772) pA = [-5, 4, 7]; pB = [-10, 6, -18] assertFuzzyEquals(distance3D(pA, pB), 25.5734237051) pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1] assertFuzzyEquals(distance3D(pA, pB), 18.5520888312) test.describe("Random Tests for 2D") test.describe("Integer coordinates") import random from random import randint for h in range(20): pA = [randint(-100, 100), randint(-100, 100)] pB = [randint(-100, 100), randint(-100, 100)] result = distance2D_check(pA, pB) res = distance2D(pA, pB) assertFuzzyEquals(res, result) test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result)) test.describe("Random Tests for 2D") test.describe("Decimal coordinates") for h in range(20): pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ] pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random()] result = distance2D_check(pA, pB) res = distance2D(pA, pB) assertFuzzyEquals(res, result) test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result)) test.describe("Random Tests for 3D") test.describe("Integer coordinates") for h in range(20): pA = [randint(-100, 100), randint(-100, 100), randint(-100, 100)] pB = [randint(-100, 100), randint(-100, 100), randint(-100, 100)] result = distance3D_check(pA, pB) res = distance3D(pA, pB) assertFuzzyEquals(res, result) test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result)) test.describe("Random Tests for 3D") test.describe("Decimal coordinates") for h in range(10): pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ] pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random(), randint(-100, 100) + random.random()] result = distance3D_check(pA, pB) res = distance3D(pA, pB) assertFuzzyEquals(res, result) test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
- from math import sqrt
- def distance2D_check(pA, pB):
- if pA == pB: return 0
- xA, yA = tuple(pA); xB, yB = tuple(pB)
- return sqrt((xA - xB)**2 + (yA - yB)**2)
- def distance3D_check(pA, pB):
- if pA == pB: return 0
- xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)
- return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
- def assertFuzzyEquals(actual, expected, msg=""):
- merr = 1e-10
- inrange = abs(actual - expected) <= merr
- if (inrange == False):
- msg = "At 1e-10: Expected value must be {:.10f} but got {:.10f}"
- msg = msg.format(expected, actual)
- return Test.expect(inrange, msg)
- test.describe("Basic Tests for 2D")
- pA = [1, 1]; pB = [2, 2]
- assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)
- pA = [0, 0]; pB = [5, 6]
- assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)
- pA = [-5, 4]; pB = [-10, 6]
- assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)
- pA = [-5.1, 4.0]; pB = [10.2, -6.3]
- assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)
- test.describe("Basic Tests for 3D")
- pA = [1, 1, 1]; pB = [2, 2, 2]
- assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)
- pA = [0, 0, 0]; pB = [7, 8, 9]
- assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)
- pA = [-5, 4, 7]; pB = [-10, 6, -18]
- assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)
- pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]
- assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)
- test.describe("Random Tests for 2D")
- test.describe("Integer coordinates")
- import random
- from random import randint
- for h in range(20):
- pA = [randint(-100, 100), randint(-100, 100)]
- pB = [randint(-100, 100), randint(-100, 100)]
- result = distance2D_check(pA, pB)
- res = distance2D(pA, pB)
- assertFuzzyEquals(res, result)
- test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
- test.describe("Random Tests for 2D")
- test.describe("Decimal coordinates")
- for h in range(20):
- pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]
- pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random()]
- result = distance2D_check(pA, pB)
- res = distance2D(pA, pB)
- assertFuzzyEquals(res, result)
- test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
- test.describe("Random Tests for 3D")
- test.describe("Integer coordinates")
- for h in range(20):
- pA = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]
- pB = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]
- result = distance3D_check(pA, pB)
- res = distance3D(pA, pB)
- assertFuzzyEquals(res, result)
- test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
- test.describe("Random Tests for 3D")
- test.describe("Decimal coordinates")
- for h in range(10):
- pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]
- pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random(), randint(-100, 100) + random.random()]
- result = distance3D_check(pA, pB)
- res = distance3D(pA, pB)
- assertFuzzyEquals(res, result)
- test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))