calcMinDist.py 893 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. import numpy
  3. class Point:
  4. def __init__(self, x, y):
  5. self.x = x
  6. self.y = y
  7. def euclideanDist(p1, p2):
  8. from math import sqrt
  9. return sqrt((p1.x-p2.x)**2 + (p1.y-p2.y)**2)
  10. def getMinDist(y, precision=0.001, startX=0, endX=3):
  11. """Get x of point on (x,x^2) that has minimal distance to given Point."""
  12. minDist = -1
  13. p1 = Point(0, y)
  14. for x in numpy.arange(startX, endX, precision):
  15. p2 = Point(x, x**2)
  16. dist = euclideanDist(p1, p2)
  17. if minDist == -1 or dist < minDist:
  18. minDist = dist
  19. #print(dist)
  20. else:
  21. if abs(i-minDist) <0.005:
  22. print("x: %s" % str(x))
  23. break
  24. return minDist
  25. for i in numpy.arange(0, 3, 0.01):
  26. minDist = getMinDist(i)
  27. if abs(i-minDist) < 0.005:
  28. print(i, minDist)
  29. #print(getMinDist(5, 0.00001, 2, 3))