From 49049ca249bed89155a9a914a36ff7bbef6ce370 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Thu, 15 Feb 2018 16:13:32 -0800 Subject: [PATCH] Added binary search solution to case where input_to_graph_point doesn't take in a graph --- topics/number_line.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/topics/number_line.py b/topics/number_line.py index 30ad693a..9d02286c 100644 --- a/topics/number_line.py +++ b/topics/number_line.py @@ -219,7 +219,29 @@ class Axes(VGroup): return graph def input_to_graph_point(self, x, graph): - return self.coords_to_point(x, graph.underlying_function(x)) + if hasattr(graph, "underlying_function"): + return self.coords_to_point(x, graph.underlying_function(x)) + else: + #binary search + lh, rh = 0, 1 + while abs(lh - rh) > 0.001: + mh = np.mean([lh, rh]) + hands = [lh, mh, rh] + points = map(graph.point_from_proportion, hands) + lx, mx, rx = map(self.x_axis.point_to_number, points) + if lx <= x and rx >= x: + if mx > x: + rh = mh + else: + lh = mh + elif lx <= x and rx <= x: + return points[2] + elif lx >= x and rx >= x: + return points[0] + elif lx > x and rx < x: + lh, rh = rh, lh + return points[1] + class ThreeDAxes(Axes): CONFIG = {