def combine(lines, increment): # we return a new PolyLine which is a composite (summed horizontally) of inputs composite = PolyLine() # find the range defined by the curves minY = None maxY = None for l in lines: minY = PolyLine.min(minY, l.min_y()) maxY = PolyLine.max(maxY, l.max_y()) # special case if the lines are already horizontal or None if minY == maxY: minSumX = None maxSumX = None for line in lines: minX = None maxX = None for point in line.points: minX = PolyLine.min(minX, point.x) maxX = PolyLine.max(maxX, point.x) minSumX = PolyLine.sum(minSumX, minX) maxSumX = PolyLine.sum(maxSumX, maxX) composite.add(Point(minSumX, minY)) if minX != maxX: composite.add(Point(maxSumX, maxY)) return composite # create an array of ys in equal increments, with highest first # this is assuming that price decreases with increase in demand (buyers!) # but seems to work with multiple suppliers? ys = sorted(np.linspace(minY, maxY, num=increment), reverse=True) # print ys # print minY, maxY # now find the cumulative x associated with each y in the array # starting with the highest y for y in ys: xt = None for line in lines: x = line.x(y, left=np.nan) # print x, y if x is not None: xt = x if xt is None else xt + x composite.add(Point(xt, y)) return composite
def test_poly_line_min_second_none(): min = PolyLine.min(1,None) assert min == 1
def test_poly_line_min_first_none(): min = PolyLine.min(None,2) assert min == 2
def test_poly_line_min(): min = PolyLine.min(1,2) assert min == 1
def test_poly_line_min_second_none(): min = PolyLine.min(1, None) assert min == 1
def test_poly_line_min_first_none(): min = PolyLine.min(None, 2) assert min == 2
def test_poly_line_min(): min = PolyLine.min(1, 2) assert min == 1