Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. from vectors import *
  2. from mpl_toolkits.mplot3d import Axes3D
  3. from matplotlib import cm
  4. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7.  
  8. # Given a line with coordinates 'start' and 'end' and the
  9. # coordinates of a point 'pnt' the proc returns the shortest
  10. # distance from pnt to the line and the coordinates of the
  11. # nearest point on the line.
  12. #
  13. # 1  Convert the line segment to a vector ('line_vec').
  14. # 2  Create a vector connecting start to pnt ('pnt_vec').
  15. # 3  Find the length of the line vector ('line_len').
  16. # 4  Convert line_vec to a unit vector ('line_unitvec').
  17. # 5  Scale pnt_vec by line_len ('pnt_vec_scaled').
  18. # 6  Get the dot product of line_unitvec and pnt_vec_scaled ('t').
  19. # 7  Ensure t is in the range 0 to 1.
  20. # 8  Use t to get the nearest location on the line to the end
  21. #    of vector pnt_vec_scaled ('nearest').
  22. # 9  Calculate the distance from nearest to pnt_vec_scaled.
  23. # 10 Translate nearest back to the start/end line.
  24. # Malcolm Kesson 16 Dec 2012
  25.  
  26. points=[(14,12,0),
  27.         (18,27,0),
  28.         (23,35,0),
  29.         (32,35,0),
  30.         (35,25,0),
  31.         (37,14,0),
  32.         (45,15,0),
  33.         (49,29,0),
  34.         (55,36,0)
  35.         ]
  36. goal=(55,36,0)
  37.  
  38. def pnt2line(pnt, start, end):
  39.     line_vec = vector(start, end)
  40.     pnt_vec = vector(start, pnt)
  41.     line_len = length(line_vec)
  42.     line_unitvec = unit(line_vec)
  43.     pnt_vec_scaled = scale(pnt_vec, 1.0/line_len)
  44.     t = dot(line_unitvec, pnt_vec_scaled)
  45.     if t < 0.0:
  46.         t = 0.0
  47.     elif t > 1.0:
  48.         t = 1.0
  49.     nearest = scale(line_vec, t)
  50.     dist = distance(nearest, pnt_vec)
  51.     nearest = add(nearest, start)
  52.     return (dist, nearest)
  53.  
  54. def getMinimumDistance(point):
  55.     shortestDistance=float("Inf")
  56.     for i in range(8):
  57.         distance=pnt2line(point,points[i],points[i+1])[0]
  58.         if shortestDistance>=distance:
  59.             shortestDistance=distance
  60.     return shortestDistance
  61.  
  62.  
  63. def show(x,y,z):
  64.     fig = plt.figure()
  65.     ax = fig.gca(projection='3d')
  66.     surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
  67.             linewidth=0, antialiased=False)
  68.     ax.set_zlim(0, 100)
  69.  
  70.     ax.zaxis.set_major_locator(LinearLocator(10))
  71.     ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
  72.  
  73.     fig.colorbar(surf, shrink=0.5, aspect=5)
  74.  
  75.     plt.show()
  76.     return 0
  77.  
  78. def calculateRiver():
  79.     river=[[getMinimumDistance([x,y,0])+distance((x,y,0),goal) for x in range(60)] for y in range(60)]
  80.     x = np.arange(0, 60, 1)
  81.     y = np.arange(0, 60, 1)
  82.     x, y = np.meshgrid(x, y)
  83.     #show(x,y,river)
  84.     return river
  85.  
  86. def getLowestPoint(current,river):
  87.     lowestPoint=current
  88.     for x in range(-1,2):
  89.         for y in range(-1,2):
  90.             next=(current[0]+x,current[1]+y)
  91.             print next,"=",river[next[0]][next[1]]
  92.             if river[next[0]][next[1]]<river[lowestPoint[0]][lowestPoint[1]]:
  93.                 lowestPoint=next
  94.     return lowestPoint
  95.  
  96.  
  97.  
  98. river=calculateRiver()
  99. currentPoint=points[0]
  100. nextPoint=getLowestPoint(currentPoint,river)
  101. while currentPoint[0]!=nextPoint[0] or currentPoint[1]!=nextPoint[1]:
  102.     print currentPoint, nextPoint
  103.     print river[currentPoint[0]][currentPoint[1]],river[nextPoint[0]][nextPoint[1]]
  104.     currentPoint=nextPoint
  105.     nextPoint=getLowestPoint(currentPoint,river)
  106.  
  107. print nextPoint
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement