Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def remove_colinear( xy, threshold ):
  4. """
  5. This isn't a great algorithm but does a pretty good
  6. first-pass kind of job
  7.  
  8. Computes the area of triangle for 3 points. If area is too big then
  9. we keep the middle point, otherwise we move onto the next one.
  10. """
  11. retval = [xy[0]] # always keep the 1st point
  12. for ii in xrange( 2, len(xy) ): # Polygon must have 3 points!
  13.  
  14. x0 = retval[-1][0] # The last saved point
  15. y0 = retval[-1][1]
  16.  
  17. x2 = xy[ii][0] # The current end-point
  18. y2 = xy[ii][1]
  19.  
  20. x1 = xy[ii-1][0] # The middle point
  21. y1 = xy[ii-1][1]
  22.  
  23. area = np.abs(0.5*((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)))
  24. if area > threshold:
  25. # If area is too big, then keep last point
  26. retval.append( xy[ii-1] )
  27. return retval
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement