Advertisement
stream13

Convex Hull Visualisation

Jan 11th, 2016
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from random import randint
  4.  
  5.  
  6. def convex_hull(points):
  7.     """Computes the convex hull of a set of 2D points.
  8.  
  9.    Input: an iterable sequence of (x, y) pairs representing the points.
  10.    Output: a list of vertices of the convex hull in counter-clockwise order,
  11.      starting from the vertex with the lexicographically smallest coordinates.
  12.    Implements Andrew's monotone chain algorithm. O(n log n) complexity.
  13.    """
  14.  
  15.     # Sort the points lexicographically (tuples are compared lexicographically).
  16.     # Remove duplicates to detect the case we have just one unique point.
  17.     points = sorted(set(points))
  18.  
  19.     # Boring case: no points or a single point, possibly repeated multiple times.
  20.     if len(points) <= 1:
  21.         return points
  22.  
  23.     # 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.
  24.     # Returns a positive value, if OAB makes a counter-clockwise turn,
  25.     # negative for clockwise turn, and zero if the points are collinear.
  26.     def cross(o, a, b):
  27.         return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
  28.  
  29.     # Build lower hull
  30.     lower = []
  31.     for p in points:
  32.         while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:
  33.             lower.pop()
  34.         lower.append(p)
  35.  
  36.     # Build upper hull
  37.     upper = []
  38.     for p in reversed(points):
  39.         while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:
  40.             upper.pop()
  41.         upper.append(p)
  42.  
  43.     # Concatenation of the lower and upper hulls gives the convex hull.
  44.     # Last point of each list is omitted because it is repeated at the beginning of the other list.
  45.     return lower[:-1] + upper[:-1]
  46.  
  47.  
  48. def rlist():
  49.     retval = []
  50.     for j in xrange(1, 3):
  51.         for i in xrange((j+1)*5):
  52.             limit = j*100
  53.             retval.append((randint(limit, 1100-limit), randint(limit, 1100-limit)))
  54.     return retval
  55.  
  56.  
  57. def plot_list(lst, rim):
  58.     x_list = []
  59.     y_list = []
  60.     for i in xrange(len(lst)):
  61.         x_list.append(lst[i][0])
  62.         y_list.append(lst[i][1])
  63.     # fix, ax = plt.subplots()
  64.     plt.plot(x_list, y_list, 'ro')
  65.     plt.axis([0, 1100, 0, 1100])
  66.  
  67.     if rim:
  68.         for start, stop in zip(rim[:-1], rim[1:]):
  69.             x, y = zip(start, stop)
  70.             # print(str(x) + ' ' + str(y))
  71.             plt.plot(x, y, color=plt.cm.gist_ncar(1))
  72.         # print(str(rim[0]) + ' ' + str(rim[-1]))
  73.         x, y = zip(rim[0], rim[-1])
  74.         plt.plot(x,y)
  75.  
  76.  
  77.     plt.show()
  78.  
  79.  
  80. data = rlist()
  81.  
  82. filtered = convex_hull(data)
  83.  
  84. # print(data)
  85. # plot_list(data, None)
  86. # print(filtered)
  87. plot_list(data, filtered)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement