1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import math points = [ [10,10], [20,30], [30,12], [35,5], [40,22], [50,12], [80,40], ] newPoints = []
def pointDistance(point_a, point_b, point_c): k = (point_a[1]-point_c[1])/(point_a[0]-point_c[0]) b = (point_a[1] - k*point_a[0]) distance = abs(k*point_b[0]-point_b[1]+b)/(math.sqrt(math.pow(k, 2)+1)) return distance
def vacuate(checkPoints,threshold): if len(checkPoints) == 2: return checkPoints maxDistance = 0 index = 1 maxIndex = 1 while index < len(checkPoints) - 1: distance = pointDistance( checkPoints[0], checkPoints[index], checkPoints[len(checkPoints)-1] ) if distance > maxDistance: maxDistance = distance maxIndex = index index += 1 else: index += 1 if maxDistance >= threshold: result1 = vacuate(checkPoints[0:maxIndex+1], threshold) result2 = vacuate(checkPoints[maxIndex:len(checkPoints)], threshold) result = result1 + result2[1:] else: result = [checkPoints[0], checkPoints[len(checkPoints)-1]] return result threshold = 13 result = vacuate(points,threshold) print(result)
|